Added routes works
This commit is contained in:
parent
573085c596
commit
879964487c
3 changed files with 90 additions and 61 deletions
|
|
@ -1,67 +1,74 @@
|
|||
import { updateRoute } from "@/lib/serverActions";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export async function GET(request: Request, {params}: {params: {route: number}}) {
|
||||
const slug = params.route;
|
||||
const res = await fetch(`http://192.168.1.69:2019/config/apps/http/servers/srv0/routes/${slug}`, { cache: 'no-store' });
|
||||
let route: Route = await res.json();
|
||||
export async function GET(request: Request, { params }: { params: { route: number } }) {
|
||||
const slug = params.route;
|
||||
const res = await fetch(`http://192.168.1.69:2019/config/apps/http/servers/srv0/routes/${slug}`, { cache: 'no-store' });
|
||||
let route: Route = await res.json();
|
||||
|
||||
if (!res.ok)
|
||||
throw new Error('Failed to fetch caddy config. Please check the network and try again.');
|
||||
|
||||
return new NextResponse(JSON.stringify(route), {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
if (!res.ok)
|
||||
throw new Error('Failed to fetch caddy config. Please check the network and try again.');
|
||||
|
||||
return new NextResponse(JSON.stringify(route), {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function PATCH(request: Request, {params}: {params: {route: number}}) {
|
||||
const slug = params.route;
|
||||
let route: Route = await request.json();
|
||||
|
||||
const res = await fetch(`http://192.168.1.69:2019/config/apps/http/servers/srv0/routes/${slug}`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
cache: 'no-cache',
|
||||
body: JSON.stringify(route)
|
||||
})
|
||||
|
||||
if (!res.ok)
|
||||
throw new Error('Failed to fetch caddy config. Please check the network and try again.');
|
||||
|
||||
return new NextResponse(JSON.stringify({error: false}), {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function PUT(request: Request, {params}: {params: { route: number }}) {
|
||||
export async function PATCH(request: Request, { params }: { params: { route: number } }) {
|
||||
const slug = params.route;
|
||||
let route: Route = await request.json();
|
||||
console.log(route, slug)
|
||||
|
||||
const res = await fetch(`http://192.168.1.69:2019/config/apps/http/servers/srv0/routes/${slug}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
cache: 'no-cache',
|
||||
body: JSON.stringify(route)
|
||||
})
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
cache: 'no-cache',
|
||||
body: JSON.stringify(route)
|
||||
})
|
||||
|
||||
if (!res.ok)
|
||||
throw new Error('Failed to fetch caddy config. Please check the network and try again.');
|
||||
|
||||
return new NextResponse(JSON.stringify({error: false}), {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
if (!res.ok) return new NextResponse(JSON.stringify({ error: true }), {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
return new NextResponse(JSON.stringify({ error: false }), {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export async function PUT(request: Request, { params }: { params: { route: number } }) {
|
||||
const slug = params.route;
|
||||
let route: Route = await request.json();
|
||||
|
||||
const res = await fetch(`http://192.168.1.69:2019/config/apps/http/servers/srv0/routes/${slug}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
cache: 'no-cache',
|
||||
body: JSON.stringify(route)
|
||||
})
|
||||
|
||||
if (!res.ok) return new NextResponse(JSON.stringify({ error: true, message: "Could not save route" }), {
|
||||
status: 500,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
return new NextResponse(JSON.stringify({ error: false }), {
|
||||
status: 200,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -24,10 +24,8 @@ export async function DELETE(req: Request) {
|
|||
let route: Route = await req.json();
|
||||
let routes: Route[] = await getRoutes();
|
||||
let routesMap: Route[] = routes.map(r => r);
|
||||
let hostsMap = routesMap.map(r => r.match[0].host);
|
||||
|
||||
//CHANGE 1 TO 0
|
||||
let targetRoute: Route = routesMap.filter((r) => (JSON.stringify(r.match[0]) === JSON.stringify(route.match[0]))).values().next().value;
|
||||
let targetRoute: Route = routesMap.filter((r) => (JSON.stringify(r) === JSON.stringify(route))).values().next().value;
|
||||
|
||||
let index = routesMap.indexOf(targetRoute);
|
||||
|
||||
|
|
|
|||
|
|
@ -15,10 +15,10 @@ export function RouteDialog({ route, routesMap }: { route: Route, routesMap: Rou
|
|||
|
||||
const [modifiedRoute, setRoute] = useState(route);
|
||||
const { trigger: pushUpdate } = useSWRMutation(`/api/caddy/routes`, updateRoute, {
|
||||
onError: () => {
|
||||
onError: (err) => {
|
||||
return toast({
|
||||
title: "Error",
|
||||
description: "There was an error updating the route",
|
||||
description: "There was an error updating the route. " + err.message,
|
||||
variant: "destructive"
|
||||
});
|
||||
},
|
||||
|
|
@ -61,12 +61,36 @@ export function RouteDialog({ route, routesMap }: { route: Route, routesMap: Rou
|
|||
|
||||
let onSubmit = (() => {
|
||||
let update = {
|
||||
route: modifiedRoute as Route,
|
||||
route: modifiedRoute,
|
||||
index: index,
|
||||
type: index == -1 ? 'PUT' : 'PATCH'
|
||||
}
|
||||
|
||||
if (index == -1) update.index = routesMap.length;
|
||||
update.route = {
|
||||
"handle": [
|
||||
{
|
||||
"handler": "subroute",
|
||||
"routes": [
|
||||
{
|
||||
"handle": [
|
||||
{
|
||||
"handler": "reverse_proxy",
|
||||
"upstreams": validUpstreams.map((upstream) => ({ "dial": upstream }))
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"match": [
|
||||
{
|
||||
"host": validHosts
|
||||
}
|
||||
],
|
||||
"terminal": true
|
||||
}
|
||||
|
||||
if (index == -1) update.index = routesMap.length - 1;
|
||||
console.log(update.index)
|
||||
pushUpdate(update);
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue