More on adding routes

This commit is contained in:
brian 2023-11-19 23:05:20 -05:00
parent 9729dd5f06
commit 573085c596
4 changed files with 32 additions and 24 deletions

View file

@ -32,7 +32,6 @@ export default function RouteCards({ routes }: { routes: Route[] }) {
let editRoute = (route: Route) => { let editRoute = (route: Route) => {
console.log(routes)
setRoute(route); setRoute(route);
} }
@ -44,8 +43,8 @@ export default function RouteCards({ routes }: { routes: Route[] }) {
<div key={index} className='p-2'> <div key={index} className='p-2'>
<Card key={index} className='w-[300px]'> <Card key={index} className='w-[300px]'>
<CardHeader> <CardHeader>
<CardTitle>{getHosts(route).values().next().value}</CardTitle> <CardTitle>{getHosts(route)!.values().next().value}</CardTitle>
{getHosts(route).map((route => <CardDescription key={route}>{route}</ CardDescription>))} {getHosts(route)!.map((route => <CardDescription key={route}>{route}</ CardDescription>))}
<CardDescription></CardDescription> <CardDescription></CardDescription>
</CardHeader> </CardHeader>
<CardContent> <CardContent>

View file

@ -1,5 +1,5 @@
import { updateRoute } from "@/lib/clientActions"; import { updateRoute } from "@/lib/clientActions";
import { getHosts, getRouteUpstreams } from "@/lib/utils"; import { getHandler, getHosts, getRouteUpstreams } from "@/lib/utils";
import { useState } from "react"; import { useState } from "react";
import useSWRMutation from "swr/mutation"; import useSWRMutation from "swr/mutation";
import { toast } from "./ui/use-toast"; import { toast } from "./ui/use-toast";
@ -36,9 +36,9 @@ export function RouteDialog({ route, routesMap }: { route: Route, routesMap: Rou
let handler: string = "reverse_proxy"; let handler: string = "reverse_proxy";
if (index != -1) { if (index != -1) {
validHosts = getHosts(route); validHosts = getHosts(route)!;
validUpstreams = getRouteUpstreams(route).map((upstream) => upstream.dial); validUpstreams = getRouteUpstreams(route).map((upstream) => upstream.dial);
handler = route?.handle[0]?.routes[0]?.handle[0]?.handler; handler = getHandler(route)!;
} }
const form = useForm({ const form = useForm({
@ -61,10 +61,11 @@ export function RouteDialog({ route, routesMap }: { route: Route, routesMap: Rou
let onSubmit = (() => { let onSubmit = (() => {
let update = { let update = {
route: modifiedRoute, route: modifiedRoute as Route,
index: index, index: index,
type: index == -1 ? 'PUT' : 'PATCH' type: index == -1 ? 'PUT' : 'PATCH'
} }
if (index == -1) update.index = routesMap.length; if (index == -1) update.index = routesMap.length;
console.log(update.index) console.log(update.index)
pushUpdate(update); pushUpdate(update);

View file

@ -3,26 +3,27 @@ interface Server {
routes: Route[]; routes: Route[];
} }
interface Route {
handle: Handle[];
match: Match[];
terminal?: boolean;
}
interface Match {
host: string[];
}
interface Handle {
handler: string;
upstreams: Upstream[];
routes: Route[];
}
interface Upstream { interface Upstream {
dial: string; dial: string;
} }
interface Handle {
handler: string;
upstreams?: Upstream[];
routes?: Route[];
}
interface Match {
host: string[];
}
interface Route {
handle: Handle[];
match?: Match[];
terminal?: boolean;
}
interface UpstreamRequest { interface UpstreamRequest {
address: string; address: string;
num_requests: number; num_requests: number;

View file

@ -15,6 +15,13 @@ export function getRouteType(route: Route) {
} }
export function getRouteUpstreams(route: Route, index: number = 0) { export function getRouteUpstreams(route: Route, index: number = 0) {
let upstreams: Upstream[] = route.handle[index].routes?.flat()[index].handle[index].upstreams; let upstreams: Upstream[] = route.handle[index].routes?.flat()[index].handle[index].upstreams!;
return upstreams; return upstreams;
} }
export function getHandler(route: Route) {
if(route.handle[0].routes) {
let handle: string = route?.handle[0]?.routes[0]?.handle[0]?.handler
return handle;
}
}