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() {
let caddyConfig = await getConfig();
let routes: Array<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 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',

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,14 +7,15 @@ import { deleteRoute } from '@/lib/serverActions';
export default function RouteCards({ routes }: { routes: Array<Route> }) {
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<Route> }) {
<p className="text-sm text-muted-foreground">{getRouteUpstreams(route)?.map((upstream => upstream.dial))}</p>
</CardContent>
<CardFooter className="flex justify-between">
<Button onClick={() => {handleClick(route)}}>Edit</Button>
<Button variant="destructive" onClick={() => {handleDelete(route)}}>Delete</Button>
<Button onClick={() => { handleClick(route) }}>Edit</Button>
<Button variant="destructive" onClick={() => { handleDelete(route) }}>Delete</Button>
</CardFooter>
</Card>
</div>

View file

@ -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();
}

View file

@ -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;
};