diff --git a/app/api/caddy/routes/route.ts b/app/api/caddy/routes/route.ts index 59d24b6..c87c5a8 100644 --- a/app/api/caddy/routes/route.ts +++ b/app/api/caddy/routes/route.ts @@ -3,7 +3,7 @@ import { NextApiRequest } from 'next'; import { NextResponse } from 'next/server'; export async function GET() { - let caddyConfig = await getConfig(); + let caddyConfig: CaddyConfig = await getConfig(); let routes: Array = []; @@ -22,11 +22,14 @@ export async function GET() { export async function DELETE(req: Request) { let route: Route = await req.json(); let routes: Route[] = await getRoutes(); - let routesMap: Match[] = routes.map(r => r.match[0]); + let routesMap: Route[] = routes.map(r => r); + let hostsMap = routesMap.map(r => r.match[0].host); - let index = routesMap.findIndex((r) => (r.host === route.match[0].host)) - - if(index = -1) return new NextResponse(JSON.stringify({error: 'Route not found'}), { + let targetRoute: Route = routesMap.filter((r) => (JSON.stringify(r.match[0]) === JSON.stringify(route.match[0]))).values().next().value; + + let index = routesMap.indexOf(targetRoute); + + if(index == -1) return new NextResponse(JSON.stringify({error: 'Route not found'}), { status: 404, headers: { 'Content-Type': 'application/json', @@ -38,7 +41,7 @@ export async function DELETE(req: Request) { headers: { 'Content-Type': 'application/json' }, - body: JSON.stringify(route) + body: JSON.stringify({route, error: false}) }); diff --git a/lib/serverActions.ts b/lib/serverActions.ts index c43f007..0a9b647 100644 --- a/lib/serverActions.ts +++ b/lib/serverActions.ts @@ -1,7 +1,9 @@ 'use server'; export async function getConfig() { - const res = await fetch('http://192.168.1.69:2019/config/').catch(() => { + const res = await fetch('http://192.168.1.69:2019/config/', { + cache: 'no-cache', + }).catch(() => { throw new Error('Failed to fetch caddy config. Please check the network and try again.'); }); @@ -25,12 +27,14 @@ headers: { 'Content-Type': 'application/json' }, + cache: 'no-cache', body: JSON.stringify(route) }).catch((e) => { + console.log(e) throw new Error('Failed to remove route. Please check the network and try again.'); }); - + return res.json(); } @@ -47,7 +51,7 @@ } export async function getRoutes() { - let res = await fetch('http://localhost:3000/api/caddy/routes', { cache: 'no-cache'}).catch(() => { + let res = await fetch('http://localhost:3000/api/caddy/routes', { cache: 'no-cache' }).catch(() => { throw new Error('Failed to fetch routes. Please check the network and try again.'); });