diff --git a/app/api/caddy/routes/requests/route.ts b/app/api/caddy/routes/requests/route.ts index 90eb217..d638df2 100644 --- a/app/api/caddy/routes/requests/route.ts +++ b/app/api/caddy/routes/requests/route.ts @@ -19,9 +19,7 @@ export async function POST(req: Request) { let routeUpstreams: Upstream[] = getRouteUpstreams(route); let upstreamsRequested: UpstreamRequest[] = upstreams.filter(upstream => { - return routeUpstreams.find(routeUpstream => { - return routeUpstream.dial === upstream.address; - }); + return routeUpstreams.find(routeUpstream => routeUpstream.dial === upstream.address); }); return new NextResponse(JSON.stringify(upstreamsRequested), { diff --git a/app/api/caddy/routes/route.ts b/app/api/caddy/routes/route.ts index e3d4126..2a87661 100644 --- a/app/api/caddy/routes/route.ts +++ b/app/api/caddy/routes/route.ts @@ -1,4 +1,4 @@ -import { getConfig } from '@/lib/utils'; +import { getConfig, getServer } from '@/lib/utils'; import { NextApiRequest } from 'next'; import { NextResponse } from 'next/server'; @@ -19,6 +19,29 @@ export async function GET() { } 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"), { status: 200, headers: { diff --git a/components/RouteCards.tsx b/components/RouteCards.tsx index 2ada0f3..1a05902 100644 --- a/components/RouteCards.tsx +++ b/components/RouteCards.tsx @@ -1,13 +1,13 @@ "use client" -import { getHosts, getRouteUpstreams, getUpstreamRequests } from "@/lib/utils"; +import { deleteRoute, getHosts, getRouteUpstreams, getUpstreamRequests } from "@/lib/utils"; import { Button } from '@/components/ui/button'; import { Card, CardHeader, CardTitle, CardDescription, CardContent, CardFooter } from "./ui/card"; export default function RouteCards({ routes }: { routes: Array }) { let handleClick = (route: Route) => { - console.log(JSON.stringify(route)) + deleteRoute(route); } @@ -28,7 +28,7 @@ export default function RouteCards({ routes }: { routes: Array }) { - + diff --git a/lib/utils.ts b/lib/utils.ts index 92fe60b..1e3c18c 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -21,7 +21,7 @@ export function getRouteUpstreams(route: Route, index: number = 0) { export async function getConfig() { const res = await fetch('http://192.168.1.69:2019/config/', { cache: 'no-store' }); - if (!res.ok) + if (!res.ok) throw new Error('Failed to fetch caddy config. Please check the network and try again.'); let caddyConfig: CaddyConfig = await res.json(); @@ -29,9 +29,23 @@ export async function getConfig() { return caddyConfig; } -export async function removeRoute(route: Route, index: number = 0) { - console.log(route); - const res = await fetch(`http://192.168.1.69:2019/config/apps/http/servers/srv${index}/routes/`, { +export async function getServer() { + const res = await fetch('http://localhost:3000/api/caddy/servers', { cache: 'no-store' }); + + 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', headers: { 'Content-Type': 'application/json' @@ -39,7 +53,7 @@ export async function removeRoute(route: Route, index: number = 0) { 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(); } @@ -51,7 +65,7 @@ export async function getUpstreamRequests(route: Route) { }, }).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; } \ No newline at end of file