Started working on route removal

This commit is contained in:
brian 2023-11-15 01:46:08 -05:00
parent 57f6a5c309
commit f8c49b1bc5
4 changed files with 48 additions and 13 deletions

View file

@ -19,9 +19,7 @@ export async function POST(req: Request) {
let routeUpstreams: Upstream[] = getRouteUpstreams(route); let routeUpstreams: Upstream[] = getRouteUpstreams(route);
let upstreamsRequested: UpstreamRequest[] = upstreams.filter(upstream => { let upstreamsRequested: UpstreamRequest[] = upstreams.filter(upstream => {
return routeUpstreams.find(routeUpstream => { return routeUpstreams.find(routeUpstream => routeUpstream.dial === upstream.address);
return routeUpstream.dial === upstream.address;
});
}); });
return new NextResponse(JSON.stringify(upstreamsRequested), { return new NextResponse(JSON.stringify(upstreamsRequested), {

View file

@ -1,4 +1,4 @@
import { getConfig } from '@/lib/utils'; import { getConfig, getServer } from '@/lib/utils';
import { NextApiRequest } from 'next'; import { NextApiRequest } from 'next';
import { NextResponse } from 'next/server'; import { NextResponse } from 'next/server';
@ -19,6 +19,29 @@ export async function GET() {
} }
export async function DELETE(req: Request) { export async function DELETE(req: Request) {
let route: Route = await req.json();
let routes: Server[] = await getServer();
let indexOfRoute = routes.findIndex((routeToFind: Route) => route.handle == routeToFind.handle);
console.log(indexOfRoute)
return new NextResponse(JSON.stringify("Test"), {
status: 200,
headers: {
'Content-Type': 'application/json',
},
});
const res = await fetch(`http://192.168.1.69:2019/config/apps/http/servers/srv0/routes/${index}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(route)
});
return new NextResponse(JSON.stringify("Test"), { return new NextResponse(JSON.stringify("Test"), {
status: 200, status: 200,
headers: { headers: {

View file

@ -1,13 +1,13 @@
"use client" "use client"
import { getHosts, getRouteUpstreams, getUpstreamRequests } from "@/lib/utils"; import { deleteRoute, getHosts, getRouteUpstreams, getUpstreamRequests } from "@/lib/utils";
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { Card, CardHeader, CardTitle, CardDescription, CardContent, CardFooter } from "./ui/card"; import { Card, CardHeader, CardTitle, CardDescription, CardContent, CardFooter } from "./ui/card";
export default function RouteCards({ routes }: { routes: Array<Route> }) { export default function RouteCards({ routes }: { routes: Array<Route> }) {
let handleClick = (route: Route) => { let handleClick = (route: Route) => {
console.log(JSON.stringify(route)) deleteRoute(route);
} }
@ -28,7 +28,7 @@ export default function RouteCards({ routes }: { routes: Array<Route> }) {
</CardContent> </CardContent>
<CardFooter className="flex justify-between"> <CardFooter className="flex justify-between">
<Button onClick={() => {handleClick(route)}}>Edit</Button> <Button onClick={() => {handleClick(route)}}>Edit</Button>
<Button variant="destructive" onClick={() => {handleClick(route)}}>Edit</Button> <Button variant="destructive" onClick={() => {handleClick(route)}}>Delete</Button>
</CardFooter> </CardFooter>
</Card> </Card>
</div> </div>

View file

@ -29,9 +29,23 @@ export async function getConfig() {
return caddyConfig; return caddyConfig;
} }
export async function removeRoute(route: Route, index: number = 0) { export async function getServer() {
console.log(route); const res = await fetch('http://localhost:3000/api/caddy/servers', { cache: 'no-store' });
const res = await fetch(`http://192.168.1.69:2019/config/apps/http/servers/srv${index}/routes/`, {
if (!res.ok)
throw new Error('Failed to fetch caddy config. Please check the network and try again.');
let server: Server = await res.json();
return server;
}
export async function deleteRoute(route: Route, index: number = 0) {
let server: Server = await getServer();
console.log(server)
return true;
const res = await fetch(`http://192.168.1.69:2019/config/apps/http/servers/srv${index}/routes/${index}`, {
method: 'DELETE', method: 'DELETE',
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
@ -39,7 +53,7 @@ export async function removeRoute(route: Route, index: number = 0) {
body: JSON.stringify(route) body: JSON.stringify(route)
}); });
if(!res.ok) throw new Error('Failed to remove route. Please check the network and try again.'); if (!res.ok) throw new Error('Failed to remove route. Please check the network and try again.');
return res.json(); return res.json();
} }
@ -51,7 +65,7 @@ export async function getUpstreamRequests(route: Route) {
}, },
}).then(res => res.json()); }).then(res => res.json());
if(!upstreams) throw new Error('Failed to fetch upstreams. Please check the network and try again.') if (!upstreams) throw new Error('Failed to fetch upstreams. Please check the network and try again.')
return upstreams; return upstreams;
} }