diff --git a/README.md b/README.md index f70e5d2..b539789 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,9 @@ This project is built using the following technologies: - [ ] Dockerfile - [X] View all Routes - [X] Delete routes -- [ ] Add routes +- [X] Add routes - [X] Edit routes -- [ ] User accounts +- [ ] Authentication ## Getting Started diff --git a/app/api/caddy/routes/[route]/route.ts b/app/api/caddy/routes/[route]/route.ts index 77f9dba..70bae3d 100644 --- a/app/api/caddy/routes/[route]/route.ts +++ b/app/api/caddy/routes/[route]/route.ts @@ -7,7 +7,7 @@ export async function GET(request: Request, { params }: { params: { route: numbe const res = await fetch(`http://192.168.1.69:2019/config/apps/http/servers/srv0/routes/${slug}`, { cache: 'no-store' }); let route: Route = await res.json(); - if (!res.ok) onError('Failed to get route'); + if (!res.ok) return onError('Failed to get route'); return new NextResponse(JSON.stringify(route), { status: 200, @@ -30,9 +30,9 @@ export async function PATCH(request: Request, { params }: { params: { route: num body: JSON.stringify(route) }) - if (!res.ok) onError('Failed to edit route') + if (res.status != 200) return onError('Failed to edit route') - return new NextResponse(JSON.stringify({ error: false }), { + return new NextResponse(JSON.stringify(route), { status: 200, headers: { 'Content-Type': 'application/json', @@ -53,9 +53,9 @@ export async function PUT(request: Request, { params }: { params: { route: numbe body: JSON.stringify(route) }) - if (!res.ok) onError('Failed to add route'); + if (res.status != 200) return onError('Failed to add route'); - return new NextResponse(JSON.stringify({ error: false }), { + return new NextResponse(JSON.stringify(route), { status: 200, headers: { 'Content-Type': 'application/json', diff --git a/app/page.tsx b/app/page.tsx index 3da9275..1ea2e79 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -37,7 +37,29 @@ export default function Home() { diff --git a/components/RouteCards.tsx b/components/RouteCards.tsx index c6bc930..fd92a81 100644 --- a/components/RouteCards.tsx +++ b/components/RouteCards.tsx @@ -12,6 +12,7 @@ import { RouteDialog } from './RouteDialog'; export default function RouteCards({ routes }: { routes: Route[] }) { const [route, setRoute] = useState(); + const [open, setOpen] = useState(false); const { trigger: handleDelete } = useSWRMutation("/api/caddy/routes", deleteRoute, { onError: () => { @@ -37,7 +38,7 @@ export default function RouteCards({ routes }: { routes: Route[] }) { return ( <> - {route && } + {route && } { routes.map((route, index) => (
@@ -54,7 +55,10 @@ export default function RouteCards({ routes }: { routes: Route[] }) {

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

- + diff --git a/components/RouteDialog.tsx b/components/RouteDialog.tsx index e4d6f79..42ce5a3 100644 --- a/components/RouteDialog.tsx +++ b/components/RouteDialog.tsx @@ -5,17 +5,20 @@ import useSWRMutation from "swr/mutation"; import { toast } from "./ui/use-toast"; import { useFieldArray, useForm } from "react-hook-form"; import { get, set } from "lodash"; -import { Dialog, DialogContent } from "./ui/dialog"; +import { Dialog, DialogContent, DialogFooter } from "./ui/dialog"; import { Form, FormControl, FormField, FormItem, FormLabel } from "./ui/form"; import { Input } from "./ui/input"; import { Button } from "./ui/button"; import { DialogClose } from "@radix-ui/react-dialog"; +//TODO one day, I'll clean this up export function RouteDialog({ route, routesMap, open, onOpenChange }: { route: Route, routesMap: Route[], open: boolean, onOpenChange: (open: boolean) => void }) { let index = routesMap.indexOf(route); const [modifiedRoute, setRoute] = useState(route); + + const { trigger: pushUpdate } = useSWRMutation(`/api/caddy/routes`, updateRoute, { onError: (err) => { return toast({ @@ -33,12 +36,12 @@ export function RouteDialog({ route, routesMap, open, onOpenChange }: { route: R } }); - let validHosts: any[] = ["google.com", "test.com"]; - let validUpstreams: any[] = ["1.1.1.1", "69.126.24.175"]; + let validHosts: any[] = []; + let validUpstreams: any[] = []; let handler: string = "reverse_proxy"; if (index != -1) { - validHosts = getHosts(route)!; + validHosts = getHosts(modifiedRoute)!; validUpstreams = getRouteUpstreams(route).map((upstream) => upstream.dial); handler = getHandler(route)!; } @@ -68,32 +71,8 @@ export function RouteDialog({ route, routesMap, open, onOpenChange }: { route: R type: index == -1 ? 'PUT' : 'PATCH' } - update.route = { - "handle": [ - { - "handler": "subroute", - "routes": [ - { - "handle": [ - { - "handler": "reverse_proxy", - "upstreams": validUpstreams.map((upstream) => ({ "dial": upstream })) - } - ] - } - ] - } - ], - "match": [ - { - "host": validHosts - } - ], - "terminal": true - } - if (index == -1) update.index = routesMap.length - 1; - console.log(update.index) + console.log(JSON.stringify(update.route), update.index) pushUpdate(update); }) @@ -133,7 +112,7 @@ export function RouteDialog({ route, routesMap, open, onOpenChange }: { route: R Upstream {index + 1} - handleChange(value, `handle[0].routes[0].handle[0].upstreams[${index}].dial`)} /> + handleChange(value, `handle[0].routes[0].handle[0].upstreams[${index}].dial`)} /> )} @@ -149,17 +128,17 @@ export function RouteDialog({ route, routesMap, open, onOpenChange }: { route: R Host {index + 1} - handleChange(value, `match[0].host[${index}]`)} /> + handleChange(value, `match[0].host[${index}]`)} /> )} /> ))} - - + + + + +