diff --git a/app/api/caddy/config/route.ts b/app/api/caddy/config/route.ts index f129e3c..e912136 100644 --- a/app/api/caddy/config/route.ts +++ b/app/api/caddy/config/route.ts @@ -1,5 +1,5 @@ // Import the NextJS API route handler -import { getConfig } from '@/lib/utils'; +import { getConfig } from '@/lib/serverActions'; import { NextResponse } from 'next/server'; // Define the route handler function diff --git a/app/api/caddy/routes/requests/route.ts b/app/api/caddy/routes/requests/route.ts index d638df2..8b253f0 100644 --- a/app/api/caddy/routes/requests/route.ts +++ b/app/api/caddy/routes/requests/route.ts @@ -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 { NextResponse } from 'next/server'; diff --git a/app/api/caddy/routes/route.ts b/app/api/caddy/routes/route.ts index 2a87661..da7e0c6 100644 --- a/app/api/caddy/routes/route.ts +++ b/app/api/caddy/routes/route.ts @@ -1,4 +1,4 @@ -import { getConfig, getServer } from '@/lib/utils'; +import { getConfig, getRoutes, getServer } from '@/lib/serverActions'; import { NextApiRequest } from 'next'; import { NextResponse } from 'next/server'; @@ -19,20 +19,20 @@ export async function GET() { } export async function DELETE(req: Request) { - 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"), { - status: 200, + if(index === -1) return new NextResponse(JSON.stringify({error: 'Route not found'}), { + status: 404, headers: { 'Content-Type': 'application/json', }, - }); + }) const res = await fetch(`http://192.168.1.69:2019/config/apps/http/servers/srv0/routes/${index}`, { method: 'DELETE', @@ -41,8 +41,9 @@ export async function DELETE(req: Request) { }, body: JSON.stringify(route) }); + - return new NextResponse(JSON.stringify("Test"), { + return new NextResponse(JSON.stringify(JSON.stringify(res.body)), { status: 200, headers: { 'Content-Type': 'application/json', diff --git a/app/api/caddy/servers/[server]/route.ts b/app/api/caddy/servers/[server]/route.ts index 98724aa..ca5f785 100644 --- a/app/api/caddy/servers/[server]/route.ts +++ b/app/api/caddy/servers/[server]/route.ts @@ -1,4 +1,3 @@ -import { getServer } from "@/lib/utils"; import { NextResponse } from "next/server"; export async function GET(request: Request, {params}: {params: {server: string}}) { diff --git a/app/api/caddy/servers/route.ts b/app/api/caddy/servers/route.ts index 8754eb9..35464b3 100644 --- a/app/api/caddy/servers/route.ts +++ b/app/api/caddy/servers/route.ts @@ -1,4 +1,3 @@ -import { getServer } from "@/lib/utils"; import { NextResponse } from "next/server"; export async function GET(request: Request) { diff --git a/app/favicon.ico b/app/favicon.ico deleted file mode 100644 index 718d6fe..0000000 Binary files a/app/favicon.ico and /dev/null differ diff --git a/app/layout.tsx b/app/layout.tsx index 126a47c..4188188 100644 --- a/app/layout.tsx +++ b/app/layout.tsx @@ -6,8 +6,8 @@ import { ThemeProvider } from '@/components/theme-provider' const inter = Inter({ subsets: ['latin'] }) export const metadata: Metadata = { - title: 'Create Next App', - description: 'Generated by create next app', + title: 'Caddy Admin Panel', + description: 'A Caddy Admin Panel for the Caddy webserver and reverse proxy.', } export default function RootLayout({ diff --git a/app/page.tsx b/app/page.tsx index fa88c46..a119606 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,13 +1,6 @@ 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() { let routes: Array = await getRoutes(); diff --git a/components/RouteCards.tsx b/components/RouteCards.tsx index 1a05902..775ced9 100644 --- a/components/RouteCards.tsx +++ b/components/RouteCards.tsx @@ -1,15 +1,20 @@ "use client" -import { deleteRoute, getHosts, getRouteUpstreams, getUpstreamRequests } from "@/lib/utils"; import { Button } from '@/components/ui/button'; 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 }) { - let handleClick = (route: Route) => { - deleteRoute(route); + async function handleDelete(route: Route) { + await deleteRoute(route); } + + let handleClick = (route: Route) => { + console.log(route); + } return ( routes.map((route, index) => ( @@ -28,7 +33,7 @@ export default function RouteCards({ routes }: { routes: Array }) { - + diff --git a/lib/serverActions.ts b/lib/serverActions.ts new file mode 100644 index 0000000..6cf1c82 --- /dev/null +++ b/lib/serverActions.ts @@ -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(); + } \ No newline at end of file diff --git a/lib/utils.ts b/lib/utils.ts index 1e3c18c..ab45ac6 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -1,3 +1,4 @@ + import { type ClassValue, clsx } from "clsx" import { twMerge } from "tailwind-merge" @@ -16,56 +17,4 @@ export function getRouteType(route: Route) { export function getRouteUpstreams(route: Route, index: number = 0) { let test: Upstream[] = route.handle[index].routes?.flat()[index].handle[index].upstreams!; 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; } \ No newline at end of file