Fix build issues and optimize API routes
This commit is contained in:
parent
6638b6a93d
commit
30df0e67d4
4 changed files with 25 additions and 22 deletions
|
|
@ -1,8 +1,8 @@
|
|||
// Import the NextJS API route handler
|
||||
import { getConfig } from '@/lib/serverActions';
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
// Define the route handler function
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
export async function GET() {
|
||||
let caddyConfig = await getConfig();
|
||||
|
||||
|
|
|
|||
|
|
@ -25,18 +25,22 @@ export async function DELETE(req: Request) {
|
|||
let routes: Route[] = await getRoutes();
|
||||
let routesMap: Route[] = routes.map(r => r);
|
||||
|
||||
let targetRoute: Route = routesMap.filter((r) => (JSON.stringify(r) === JSON.stringify(route))).values().next().value;
|
||||
let targetRoute: Route = routesMap.filter((r) =>
|
||||
(JSON.stringify(r) === JSON.stringify(route))
|
||||
)[0]; // Use array indexing instead of iterator
|
||||
|
||||
let index = routesMap.indexOf(targetRoute);
|
||||
|
||||
revalidateTag('routes');
|
||||
|
||||
if(index == -1) return new NextResponse(JSON.stringify({error: true, message: "Route not found"}), {
|
||||
if (!targetRoute) {
|
||||
return new NextResponse(JSON.stringify({error: true, message: "Route not found"}), {
|
||||
status: 404,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
let index = routesMap.indexOf(targetRoute);
|
||||
|
||||
revalidateTag('routes');
|
||||
|
||||
const res = await fetch(`${process.env.CADDY_API}/config/apps/http/servers/srv0/routes/${index}`, {
|
||||
method: 'DELETE',
|
||||
|
|
@ -46,7 +50,6 @@ export async function DELETE(req: Request) {
|
|||
body: JSON.stringify({route, error: false})
|
||||
});
|
||||
|
||||
|
||||
return new NextResponse(JSON.stringify(JSON.stringify(res.body)), {
|
||||
status: 200,
|
||||
headers: {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
import { NextResponse } from "next/server";
|
||||
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
export async function GET(request: Request) {
|
||||
const res = await fetch(`${process.env.CADDY_API}/config/apps/http/servers/`, { cache: 'no-store' });
|
||||
|
||||
|
|
|
|||
|
|
@ -1,33 +1,31 @@
|
|||
import useSWR from "swr";
|
||||
|
||||
export async function useConfig() {
|
||||
export function useConfig() {
|
||||
const { data, error, isLoading } = useSWR('/api/caddy/servers', fetcher);
|
||||
return {
|
||||
data,
|
||||
error,
|
||||
isLoading,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function useServer() {
|
||||
export function useServer() {
|
||||
const { data, error, isLoading } = useSWR('/api/caddy/servers', fetcher);
|
||||
return {
|
||||
data,
|
||||
error,
|
||||
isLoading,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function useUpstreamRoutes(route: Route) {
|
||||
export function useUpstreamRoutes(route: Route) {
|
||||
const { data, error, isLoading } = useSWR(`${process.env.CADDY_API}/reverse_proxy/upstreams`, fetcher);
|
||||
return {
|
||||
data,
|
||||
error,
|
||||
isLoading,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
export function useRoutes() {
|
||||
const { data, error, isLoading } = useSWR('/api/caddy/routes', fetcher);
|
||||
return {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue