Worked on error messages/UI and deleting routes

This commit is contained in:
brian 2023-11-15 04:16:52 -05:00
parent 8ca0a3aee1
commit d60da0567a
5 changed files with 48 additions and 30 deletions

View file

@ -4,6 +4,7 @@ import { NextResponse } from 'next/server';
export async function GET() { export async function GET() {
let caddyConfig = await getConfig(); let caddyConfig = await getConfig();
let routes: Array<Route> = []; let routes: Array<Route> = [];
caddyConfig.apps.http.servers.srv0.routes.forEach((route: Route) => { 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 routes: Route[] = await getRoutes();
let routesMap: Match[] = routes.map(r => r.match[0]); 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, status: 404,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',

18
app/global-error.tsx Normal file
View file

@ -0,0 +1,18 @@
'use client'
export default function GlobalError({
error,
reset,
}: {
error: Error & { digest?: string }
reset: () => void
}) {
return (
<html>
<body>
<h2>Something went wrong!</h2>
<button onClick={() => reset()}>Try again</button>
</body>
</html>
)
}

View file

@ -7,8 +7,9 @@ import { deleteRoute } from '@/lib/serverActions';
export default function RouteCards({ routes }: { routes: Array<Route> }) { export default function RouteCards({ routes }: { routes: Array<Route> }) {
async function handleDelete(route: Route) { const handleDelete = async (route: Route) => {
await deleteRoute(route); let res = await deleteRoute(route);
console.log(res);
} }

View file

@ -1,10 +1,9 @@
'use server'; 'use server';
export async function getConfig() { export async function getConfig() {
const res = await fetch('http://192.168.1.69:2019/config/'); const res = await fetch('http://192.168.1.69:2019/config/').catch(() => {
if (!res.ok)
throw new Error('Failed to fetch caddy config. Please check the network and try again.'); throw new Error('Failed to fetch caddy config. Please check the network and try again.');
});
let caddyConfig: CaddyConfig = await res.json(); let caddyConfig: CaddyConfig = await res.json();
@ -12,10 +11,9 @@
} }
export async function getServer() { export async function getServer() {
const res = await fetch('http://localhost:3000/api/caddy/servers'); 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.');
if (!res.ok) });
throw new Error('Failed to fetch caddy config. Please check the network and try again.');
let server: Server = await res.json(); let server: Server = await res.json();
return server; return server;
@ -29,9 +27,9 @@
}, },
body: JSON.stringify(route) body: JSON.stringify(route)
}) }).catch((e) => {
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();
} }
@ -41,15 +39,17 @@
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },
}).then(res => res.json()); }).then(res => res.json()).catch(() => {
throw new Error('Failed to fetch upstream requests. 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;
} }
export async function getRoutes() { export async function getRoutes() {
let res = await fetch('http://localhost:3000/api/caddy/routes', { cache: 'no-cache'}); let res = await fetch('http://localhost:3000/api/caddy/routes', { cache: 'no-cache'}).catch(() => {
if (!res.ok) throw new Error('Failed to fetch routes'); throw new Error('Failed to fetch routes. Please check the network and try again.');
});
return await res.json(); return await res.json();
} }

View file

@ -65,7 +65,7 @@ interface Apps {
[key: string]: Server; [key: string]: Server;
}; };
}; };
tls: { tls?: {
automation: { automation: {
policies: Policies[]; policies: Policies[];
}; };
@ -86,7 +86,7 @@ interface Logging {
} }
type CaddyConfig = { type CaddyConfig = {
admin?: Admin;
apps: Apps; apps: Apps;
admin: Admin;
logging?: Logging; logging?: Logging;
}; };