Added support for Caddy healthchecks

This commit is contained in:
brian 2023-11-15 00:52:21 -05:00
parent 3fb3f2f6d8
commit a1a23e20b9
5 changed files with 38 additions and 50 deletions

View file

@ -1,26 +1,9 @@
// Import the NextJS API route handler import { getConfig } from '@/lib/utils';
import { NextApiRequest } from 'next'; import { NextApiRequest } from 'next';
import { NextResponse } from 'next/server'; 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() { export async function GET() {
let caddyConfig = await getRoutes(); let caddyConfig = await getConfig();
let routes: Array<Route> = []; let routes: Array<Route> = [];
caddyConfig.apps.http.servers.srv0.routes.forEach((route: Route) => { caddyConfig.apps.http.servers.srv0.routes.forEach((route: Route) => {

View file

@ -1,7 +1,4 @@
import RouteCards from '@/components/RouteCards'; 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() { async function getRoutes() {
let res = await fetch('http://localhost:3000/api/caddy/routes'); let res = await fetch('http://localhost:3000/api/caddy/routes');

View file

@ -1,15 +1,13 @@
"use client" "use client"
import { getHosts, getRouteUpstreams } from "@/lib/utils"; import { getHosts, getRouteUpstreams, getUpstreamRequests } from "@/lib/utils";
import { Button } from "./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 { randomInt } from "crypto";
export default function RouteCards({ routes }: { routes: Array<Route> }) { export default function RouteCards({ routes }: { routes: Array<Route> }) {
let handleClick = () => { let handleClick = (route: Route) => {
console.log('click'); console.log(JSON.stringify(route))
} }
@ -29,8 +27,8 @@ export default function RouteCards({ routes }: { routes: Array<Route> }) {
<p className="text-sm text-muted-foreground">{getRouteUpstreams(route)?.map((upstream => upstream.dial))}</p> <p className="text-sm text-muted-foreground">{getRouteUpstreams(route)?.map((upstream => upstream.dial))}</p>
</CardContent> </CardContent>
<CardFooter className="flex justify-between"> <CardFooter className="flex justify-between">
<Button variant="secondary" onClick={handleClick}>Edit</Button> <Button onClick={() => {handleClick(route)}}>Edit</Button>
<Button variant="destructive" onClick={handleClick}>Delete</Button> <Button variant="destructive" onClick={() => {handleClick(route)}}>Edit</Button>
</CardFooter> </CardFooter>
</Card> </Card>
</div> </div>

View file

@ -15,10 +15,6 @@ interface Match {
interface Handle { interface Handle {
handler: string; handler: string;
upstreams?: Upstream[]; upstreams?: Upstream[];
}
interface Handle {
handler: string;
routes?: Route[]; routes?: Route[];
} }
@ -26,14 +22,23 @@ interface Upstream {
dial: string; dial: string;
} }
interface UpstreamRequest {
address: string;
num_requests: number;
fails: number;
}
interface Provider { interface Provider {
api_token: string; api_token: string;
name: string; name: string;
} }
interface Policies { interface DNS {
subjects: string[]; provider: Provider;
issuer: Issuer; }
interface Challenges {
dns: DNS;
} }
interface Issuer { interface Issuer {
@ -42,12 +47,14 @@ interface Issuer {
module: string; module: string;
} }
interface Challenges { interface Policies {
dns: DNS; subjects: string[];
issuer: Issuer;
} }
interface DNS { interface Server {
provider: Provider; listen: string[];
routes: Route[];
} }
interface Apps { interface Apps {

View file

@ -29,15 +29,6 @@ export async function getConfig() {
return caddyConfig; 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) { export async function removeRoute(route: Route, index: number = 0) {
console.log(route); console.log(route);
const res = await fetch(`http://192.168.1.69:2019/config/apps/http/servers/srv${index}/routes/`, { 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.'); if(!res.ok) throw new Error('Failed to remove route. Please check the network and try again.');
return res.json(); 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;
} }