From d60da0567ac9420bd5b344508b0a8c1ac313dbfe Mon Sep 17 00:00:00 2001 From: brian Date: Wed, 15 Nov 2023 04:16:52 -0500 Subject: [PATCH] Worked on error messages/UI and deleting routes --- app/api/caddy/routes/route.ts | 7 +++---- app/global-error.tsx | 18 ++++++++++++++++++ components/RouteCards.tsx | 13 +++++++------ lib/serverActions.ts | 36 +++++++++++++++++------------------ lib/types.ts | 4 ++-- 5 files changed, 48 insertions(+), 30 deletions(-) create mode 100644 app/global-error.tsx diff --git a/app/api/caddy/routes/route.ts b/app/api/caddy/routes/route.ts index da7e0c6..59d24b6 100644 --- a/app/api/caddy/routes/route.ts +++ b/app/api/caddy/routes/route.ts @@ -4,6 +4,7 @@ import { NextResponse } from 'next/server'; export async function GET() { let caddyConfig = await getConfig(); + let routes: Array = []; caddyConfig.apps.http.servers.srv0.routes.forEach((route: Route) => { @@ -23,11 +24,9 @@ export async function DELETE(req: Request) { let routes: Route[] = await getRoutes(); let routesMap: Match[] = routes.map(r => r.match[0]); - console.log(route) + let index = routesMap.findIndex((r) => (r.host === route.match[0].host)) - let index = routesMap.findIndex((r) => (r.host[0] === route.match[0].host[0])) - - if(index === -1) return new NextResponse(JSON.stringify({error: 'Route not found'}), { + if(index = -1) return new NextResponse(JSON.stringify({error: 'Route not found'}), { status: 404, headers: { 'Content-Type': 'application/json', diff --git a/app/global-error.tsx b/app/global-error.tsx new file mode 100644 index 0000000..8b43970 --- /dev/null +++ b/app/global-error.tsx @@ -0,0 +1,18 @@ +'use client' + +export default function GlobalError({ + error, + reset, +}: { + error: Error & { digest?: string } + reset: () => void +}) { + return ( + + +

Something went wrong!

+ + + + ) +} \ No newline at end of file diff --git a/components/RouteCards.tsx b/components/RouteCards.tsx index 775ced9..05569f2 100644 --- a/components/RouteCards.tsx +++ b/components/RouteCards.tsx @@ -7,14 +7,15 @@ import { deleteRoute } from '@/lib/serverActions'; export default function RouteCards({ routes }: { routes: Array }) { - async function handleDelete(route: Route) { - await deleteRoute(route); + const handleDelete = async (route: Route) => { + let res = await deleteRoute(route); + console.log(res); } - + let handleClick = (route: Route) => { console.log(route); - } + } return ( routes.map((route, index) => ( @@ -32,8 +33,8 @@ export default function RouteCards({ routes }: { routes: Array }) {

{getRouteUpstreams(route)?.map((upstream => upstream.dial))}

- - + + diff --git a/lib/serverActions.ts b/lib/serverActions.ts index 6cf1c82..c43f007 100644 --- a/lib/serverActions.ts +++ b/lib/serverActions.ts @@ -1,10 +1,9 @@ 'use server'; export async function getConfig() { - const res = await fetch('http://192.168.1.69:2019/config/'); - - if (!res.ok) + const res = await fetch('http://192.168.1.69:2019/config/').catch(() => { throw new Error('Failed to fetch caddy config. Please check the network and try again.'); + }); let caddyConfig: CaddyConfig = await res.json(); @@ -12,11 +11,10 @@ } export async function getServer() { - const res = await fetch('http://localhost:3000/api/caddy/servers'); - - if (!res.ok) - throw new Error('Failed to fetch caddy config. Please check the network and try again.'); - + const res = await fetch('http://localhost:3000/api/caddy/servers').catch(() => { + throw new Error('Failed to fetch caddy servers. Please check the network and try again.'); + }); + let server: Server = await res.json(); return server; } @@ -29,10 +27,10 @@ }, body: JSON.stringify(route) - }) - - if (!res.ok) throw new Error('Failed to remove route. Please check the network and try again.'); - + }).catch((e) => { + throw new Error('Failed to remove route. Please check the network and try again.'); + }); + return res.json(); } @@ -41,15 +39,17 @@ headers: { 'Content-Type': 'application/json' }, - }).then(res => res.json()); - - if (!upstreams) throw new Error('Failed to fetch upstreams. Please check the network and try again.') - + }).then(res => res.json()).catch(() => { + throw new Error('Failed to fetch upstream requests. Please check the network and try again.'); + }); + return upstreams; } export async function getRoutes() { - let res = await fetch('http://localhost:3000/api/caddy/routes', { cache: 'no-cache'}); - if (!res.ok) throw new Error('Failed to fetch routes'); + 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.'); + }); + return await res.json(); } \ No newline at end of file diff --git a/lib/types.ts b/lib/types.ts index 51347e3..1549326 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -65,7 +65,7 @@ interface Apps { [key: string]: Server; }; }; - tls: { + tls?: { automation: { policies: Policies[]; }; @@ -86,7 +86,7 @@ interface Logging { } type CaddyConfig = { + admin?: Admin; apps: Apps; - admin: Admin; logging?: Logging; }; \ No newline at end of file