Added support for Caddy healthchecks
This commit is contained in:
parent
3fb3f2f6d8
commit
a1a23e20b9
5 changed files with 38 additions and 50 deletions
|
|
@ -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<Route> = [];
|
||||
|
||||
caddyConfig.apps.http.servers.srv0.routes.forEach((route: Route) => {
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
|
|
|
|||
|
|
@ -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<Route> }) {
|
||||
|
||||
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<Route> }) {
|
|||
<p className="text-sm text-muted-foreground">{getRouteUpstreams(route)?.map((upstream => upstream.dial))}</p>
|
||||
</CardContent>
|
||||
<CardFooter className="flex justify-between">
|
||||
<Button variant="secondary" onClick={handleClick}>Edit</Button>
|
||||
<Button variant="destructive" onClick={handleClick}>Delete</Button>
|
||||
<Button onClick={() => {handleClick(route)}}>Edit</Button>
|
||||
<Button variant="destructive" onClick={() => {handleClick(route)}}>Edit</Button>
|
||||
</CardFooter>
|
||||
</Card>
|
||||
</div>
|
||||
|
|
|
|||
29
lib/types.ts
29
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 {
|
||||
|
|
|
|||
21
lib/utils.ts
21
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;
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue