Moved around actions, and continued work on deleting
This commit is contained in:
parent
fc9d16fa72
commit
8ca0a3aee1
11 changed files with 81 additions and 79 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
// Import the NextJS API route handler
|
// Import the NextJS API route handler
|
||||||
import { getConfig } from '@/lib/utils';
|
import { getConfig } from '@/lib/serverActions';
|
||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
|
|
||||||
// Define the route handler function
|
// Define the route handler function
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { getConfig, getRouteUpstreams, getUpstreamRequests } from '@/lib/utils';
|
import { getUpstreamRequests } from '@/lib/serverActions';
|
||||||
|
import { getRouteUpstreams } from '@/lib/utils';
|
||||||
import { NextApiRequest } from 'next';
|
import { NextApiRequest } from 'next';
|
||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { getConfig, getServer } from '@/lib/utils';
|
import { getConfig, getRoutes, getServer } from '@/lib/serverActions';
|
||||||
import { NextApiRequest } from 'next';
|
import { NextApiRequest } from 'next';
|
||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
|
|
||||||
|
|
@ -19,20 +19,20 @@ export async function GET() {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function DELETE(req: Request) {
|
export async function DELETE(req: Request) {
|
||||||
|
|
||||||
let route: Route = await req.json();
|
let route: Route = await req.json();
|
||||||
let routes: Server[] = await getServer();
|
let routes: Route[] = await getRoutes();
|
||||||
|
let routesMap: Match[] = routes.map(r => r.match[0]);
|
||||||
|
|
||||||
let indexOfRoute = routes.findIndex((routeToFind: Route) => route.handle == routeToFind.handle);
|
console.log(route)
|
||||||
|
|
||||||
console.log(indexOfRoute)
|
let index = routesMap.findIndex((r) => (r.host[0] === route.match[0].host[0]))
|
||||||
|
|
||||||
return new NextResponse(JSON.stringify("Test"), {
|
if(index === -1) return new NextResponse(JSON.stringify({error: 'Route not found'}), {
|
||||||
status: 200,
|
status: 404,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
});
|
})
|
||||||
|
|
||||||
const res = await fetch(`http://192.168.1.69:2019/config/apps/http/servers/srv0/routes/${index}`, {
|
const res = await fetch(`http://192.168.1.69:2019/config/apps/http/servers/srv0/routes/${index}`, {
|
||||||
method: 'DELETE',
|
method: 'DELETE',
|
||||||
|
|
@ -42,7 +42,8 @@ export async function DELETE(req: Request) {
|
||||||
body: JSON.stringify(route)
|
body: JSON.stringify(route)
|
||||||
});
|
});
|
||||||
|
|
||||||
return new NextResponse(JSON.stringify("Test"), {
|
|
||||||
|
return new NextResponse(JSON.stringify(JSON.stringify(res.body)), {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
import { getServer } from "@/lib/utils";
|
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
export async function GET(request: Request, {params}: {params: {server: string}}) {
|
export async function GET(request: Request, {params}: {params: {server: string}}) {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
import { getServer } from "@/lib/utils";
|
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
export async function GET(request: Request) {
|
export async function GET(request: Request) {
|
||||||
|
|
|
||||||
BIN
app/favicon.ico
BIN
app/favicon.ico
Binary file not shown.
|
Before Width: | Height: | Size: 25 KiB |
|
|
@ -6,8 +6,8 @@ import { ThemeProvider } from '@/components/theme-provider'
|
||||||
const inter = Inter({ subsets: ['latin'] })
|
const inter = Inter({ subsets: ['latin'] })
|
||||||
|
|
||||||
export const metadata: Metadata = {
|
export const metadata: Metadata = {
|
||||||
title: 'Create Next App',
|
title: 'Caddy Admin Panel',
|
||||||
description: 'Generated by create next app',
|
description: 'A Caddy Admin Panel for the Caddy webserver and reverse proxy.',
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function RootLayout({
|
export default function RootLayout({
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,6 @@
|
||||||
import RouteCards from '@/components/RouteCards';
|
import RouteCards from '@/components/RouteCards';
|
||||||
|
import { getRoutes } from '@/lib/serverActions';
|
||||||
|
|
||||||
async function getRoutes() {
|
|
||||||
let res = await fetch('http://localhost:3000/api/caddy/routes');
|
|
||||||
if (!res.ok) throw new Error('Failed to fetch routes');
|
|
||||||
return await res.json();
|
|
||||||
}
|
|
||||||
function handleClick() {
|
|
||||||
console.log('click');
|
|
||||||
}
|
|
||||||
export default async function Home() {
|
export default async function Home() {
|
||||||
let routes: Array<Route> = await getRoutes();
|
let routes: Array<Route> = await getRoutes();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,21 @@
|
||||||
"use client"
|
"use client"
|
||||||
|
|
||||||
import { deleteRoute, getHosts, getRouteUpstreams, getUpstreamRequests } from "@/lib/utils";
|
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { Card, CardHeader, CardTitle, CardDescription, CardContent, CardFooter } from "./ui/card";
|
import { Card, CardHeader, CardTitle, CardDescription, CardContent, CardFooter } from "./ui/card";
|
||||||
|
import { getHosts, getRouteUpstreams } from '@/lib/utils';
|
||||||
|
import { deleteRoute } from '@/lib/serverActions';
|
||||||
|
|
||||||
export default function RouteCards({ routes }: { routes: Array<Route> }) {
|
export default function RouteCards({ routes }: { routes: Array<Route> }) {
|
||||||
|
|
||||||
let handleClick = (route: Route) => {
|
async function handleDelete(route: Route) {
|
||||||
deleteRoute(route);
|
await deleteRoute(route);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
let handleClick = (route: Route) => {
|
||||||
|
console.log(route);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
routes.map((route, index) => (
|
routes.map((route, index) => (
|
||||||
<div key={index}>
|
<div key={index}>
|
||||||
|
|
@ -28,7 +33,7 @@ export default function RouteCards({ routes }: { routes: Array<Route> }) {
|
||||||
</CardContent>
|
</CardContent>
|
||||||
<CardFooter className="flex justify-between">
|
<CardFooter className="flex justify-between">
|
||||||
<Button onClick={() => {handleClick(route)}}>Edit</Button>
|
<Button onClick={() => {handleClick(route)}}>Edit</Button>
|
||||||
<Button variant="destructive" onClick={() => {handleClick(route)}}>Delete</Button>
|
<Button variant="destructive" onClick={() => {handleDelete(route)}}>Delete</Button>
|
||||||
</CardFooter>
|
</CardFooter>
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
55
lib/serverActions.ts
Normal file
55
lib/serverActions.ts
Normal file
|
|
@ -0,0 +1,55 @@
|
||||||
|
'use server';
|
||||||
|
|
||||||
|
export async function getConfig() {
|
||||||
|
const res = await fetch('http://192.168.1.69:2019/config/');
|
||||||
|
|
||||||
|
if (!res.ok)
|
||||||
|
throw new Error('Failed to fetch caddy config. Please check the network and try again.');
|
||||||
|
|
||||||
|
let caddyConfig: CaddyConfig = await res.json();
|
||||||
|
|
||||||
|
return caddyConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
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.');
|
||||||
|
|
||||||
|
let server: Server = await res.json();
|
||||||
|
return server;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteRoute(route: Route, index: number = 0) {
|
||||||
|
let res = await fetch("http://localhost:3000/api/caddy/routes", {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body: JSON.stringify(route)
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!res.ok) throw new Error('Failed to remove route. Please check the network and try again.');
|
||||||
|
|
||||||
|
return res.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getUpstreamRequests(route: Route) {
|
||||||
|
let upstreams: UpstreamRequest[] = await fetch(`http://192.168.1.69:2019/reverse_proxy/upstreams`, {
|
||||||
|
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.')
|
||||||
|
|
||||||
|
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');
|
||||||
|
return await res.json();
|
||||||
|
}
|
||||||
53
lib/utils.ts
53
lib/utils.ts
|
|
@ -1,3 +1,4 @@
|
||||||
|
|
||||||
import { type ClassValue, clsx } from "clsx"
|
import { type ClassValue, clsx } from "clsx"
|
||||||
import { twMerge } from "tailwind-merge"
|
import { twMerge } from "tailwind-merge"
|
||||||
|
|
||||||
|
|
@ -17,55 +18,3 @@ export function getRouteUpstreams(route: Route, index: number = 0) {
|
||||||
let test: Upstream[] = route.handle[index].routes?.flat()[index].handle[index].upstreams!;
|
let test: Upstream[] = route.handle[index].routes?.flat()[index].handle[index].upstreams!;
|
||||||
return test;
|
return test;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getConfig() {
|
|
||||||
const res = await fetch('http://192.168.1.69:2019/config/', { cache: 'no-store' });
|
|
||||||
|
|
||||||
if (!res.ok)
|
|
||||||
throw new Error('Failed to fetch caddy config. Please check the network and try again.');
|
|
||||||
|
|
||||||
let caddyConfig: CaddyConfig = await res.json();
|
|
||||||
|
|
||||||
return caddyConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getServer() {
|
|
||||||
const res = await fetch('http://localhost:3000/api/caddy/servers', { cache: 'no-store' });
|
|
||||||
|
|
||||||
if (!res.ok)
|
|
||||||
throw new Error('Failed to fetch caddy config. Please check the network and try again.');
|
|
||||||
|
|
||||||
let server: Server = await res.json();
|
|
||||||
return server;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function deleteRoute(route: Route, index: number = 0) {
|
|
||||||
let server: Server = await getServer();
|
|
||||||
|
|
||||||
console.log(server)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
const res = await fetch(`http://192.168.1.69:2019/config/apps/http/servers/srv${index}/routes/${index}`, {
|
|
||||||
method: 'DELETE',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
body: JSON.stringify(route)
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!res.ok) throw new Error('Failed to remove route. Please check the network and try again.');
|
|
||||||
|
|
||||||
return res.json();
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getUpstreamRequests(route: Route) {
|
|
||||||
let upstreams: UpstreamRequest[] = await fetch(`http://192.168.1.69:2019/reverse_proxy/upstreams`, {
|
|
||||||
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.')
|
|
||||||
|
|
||||||
return upstreams;
|
|
||||||
}
|
|
||||||
Loading…
Add table
Reference in a new issue