diff --git a/app/api/caddy/routes/route.ts b/app/api/caddy/routes/route.ts index 130386f..235378b 100644 --- a/app/api/caddy/routes/route.ts +++ b/app/api/caddy/routes/route.ts @@ -1,26 +1,9 @@ -// Import the NextJS API route handler +import { getConfig } from '@/lib/utils'; import { NextApiRequest } from 'next'; import { NextResponse } from 'next/server'; -async function getRoutes() { - 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; -} - -async function removeRoute(route: Route) { - const res = await fetch('http://192.168.1.69:2019/config/apps/http/servers/srv0/routes/0') -} - -// Define the route handler function export async function GET() { - let caddyConfig = await getRoutes(); + let caddyConfig = await getConfig(); let routes: Array = []; caddyConfig.apps.http.servers.srv0.routes.forEach((route: Route) => { diff --git a/app/page.tsx b/app/page.tsx index 9fbaf9a..fa88c46 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -1,7 +1,4 @@ import RouteCards from '@/components/RouteCards'; -import { Button } from '@/components/ui/button'; -import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' -import { getHosts, getRouteUpstreams } from '@/lib/utils'; async function getRoutes() { let res = await fetch('http://localhost:3000/api/caddy/routes'); diff --git a/components/RouteCards.tsx b/components/RouteCards.tsx index f40eb48..2ada0f3 100644 --- a/components/RouteCards.tsx +++ b/components/RouteCards.tsx @@ -1,15 +1,13 @@ "use client" -import { getHosts, getRouteUpstreams } from "@/lib/utils"; -import { Button } from "./ui/button"; +import { getHosts, getRouteUpstreams, getUpstreamRequests } from "@/lib/utils"; +import { Button } from '@/components/ui/button'; import { Card, CardHeader, CardTitle, CardDescription, CardContent, CardFooter } from "./ui/card"; -import { randomInt } from "crypto"; - export default function RouteCards({ routes }: { routes: Array }) { - let handleClick = () => { - console.log('click'); + let handleClick = (route: Route) => { + console.log(JSON.stringify(route)) } @@ -29,8 +27,8 @@ export default function RouteCards({ routes }: { routes: Array }) {

{getRouteUpstreams(route)?.map((upstream => upstream.dial))}

- - + + diff --git a/lib/types.ts b/lib/types.ts index d8a251b..e832bd9 100644 --- a/lib/types.ts +++ b/lib/types.ts @@ -15,10 +15,6 @@ interface Match { interface Handle { handler: string; upstreams?: Upstream[]; -} - -interface Handle { - handler: string; routes?: Route[]; } @@ -26,14 +22,23 @@ interface Upstream { dial: string; } +interface UpstreamRequest { + address: string; + num_requests: number; + fails: number; +} + interface Provider { api_token: string; name: string; } -interface Policies { - subjects: string[]; - issuer: Issuer; +interface DNS { + provider: Provider; +} + +interface Challenges { + dns: DNS; } interface Issuer { @@ -42,12 +47,14 @@ interface Issuer { module: string; } -interface Challenges { - dns: DNS; +interface Policies { + subjects: string[]; + issuer: Issuer; } -interface DNS { - provider: Provider; +interface Server { + listen: string[]; + routes: Route[]; } interface Apps { diff --git a/lib/utils.ts b/lib/utils.ts index 87cb5fc..92fe60b 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -29,15 +29,6 @@ export async function getConfig() { return caddyConfig; } -export async function getRoutes(index: number = 0) { - const res = await fetch(`http://192.168.1.69:2019:config/apps/http/servers/srv${index}/routes/`, { cache: 'no-store' }); - - if(!res.ok) - throw new Error('Failed to fetch routes. Please check the network and try again.'); - - return res.json(); -} - export async function removeRoute(route: Route, index: number = 0) { console.log(route); const res = await fetch(`http://192.168.1.69:2019/config/apps/http/servers/srv${index}/routes/`, { @@ -51,4 +42,16 @@ export async function removeRoute(route: Route, index: number = 0) { 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