img:first-child]:pt-0 data-[size=sm]:[--card-spacing:--spacing(3)] data-[size=sm]:has-data-[slot=card-footer]:pb-0 *:[img:first-child]:rounded-t-xl *:[img:last-child]:rounded-b-xl",
+ className
+ )}
+ {...props}
+ />
+ )
+}
+
+function CardHeader({ className, ...props }: React.ComponentProps<"div">) {
+ return (
+
+ )
+}
+
+function CardTitle({ className, ...props }: React.ComponentProps<"div">) {
+ return (
+
+ )
+}
+
+function CardDescription({ className, ...props }: React.ComponentProps<"div">) {
+ return (
+
+ )
+}
+
+function CardAction({ className, ...props }: React.ComponentProps<"div">) {
+ return (
+
+ )
+}
+
+function CardContent({ className, ...props }: React.ComponentProps<"div">) {
+ return (
+
+ )
+}
+
+function CardFooter({ className, ...props }: React.ComponentProps<"div">) {
+ return (
+
+ )
+}
+
+export {
+ Card,
+ CardHeader,
+ CardFooter,
+ CardTitle,
+ CardAction,
+ CardDescription,
+ CardContent,
+}
diff --git a/admin/src/components/ui/dialog.tsx b/admin/src/components/ui/dialog.tsx
new file mode 100644
index 0000000..9ddd059
--- /dev/null
+++ b/admin/src/components/ui/dialog.tsx
@@ -0,0 +1,160 @@
+"use client"
+
+import * as React from "react"
+import { Dialog as DialogPrimitive } from "@base-ui/react/dialog"
+import { cn } from "@/lib/utils"
+
+import { Button } from "@/components/ui/button"
+import { XIcon } from "lucide-react"
+
+function Dialog({ ...props }: DialogPrimitive.Root.Props) {
+ return
+}
+
+function DialogTrigger({ ...props }: DialogPrimitive.Trigger.Props) {
+ return
+}
+
+function DialogPortal({ ...props }: DialogPrimitive.Portal.Props) {
+ return
+}
+
+function DialogClose({ ...props }: DialogPrimitive.Close.Props) {
+ return
+}
+
+function DialogOverlay({
+ className,
+ ...props
+}: DialogPrimitive.Backdrop.Props) {
+ return (
+
+ )
+}
+
+function DialogContent({
+ className,
+ children,
+ showCloseButton = true,
+ ...props
+}: DialogPrimitive.Popup.Props & {
+ showCloseButton?: boolean
+}) {
+ return (
+
+
+
+ {children}
+ {showCloseButton && (
+
+ }
+ >
+
+ Close
+
+ )}
+
+
+ )
+}
+
+function DialogHeader({ className, ...props }: React.ComponentProps<"div">) {
+ return (
+
+ )
+}
+
+function DialogFooter({
+ className,
+ showCloseButton = false,
+ children,
+ ...props
+}: React.ComponentProps<"div"> & {
+ showCloseButton?: boolean
+}) {
+ return (
+
+ {children}
+ {showCloseButton && (
+ }>
+ Close
+
+ )}
+
+ )
+}
+
+function DialogTitle({ className, ...props }: DialogPrimitive.Title.Props) {
+ return (
+
+ )
+}
+
+function DialogDescription({
+ className,
+ ...props
+}: DialogPrimitive.Description.Props) {
+ return (
+
+ )
+}
+
+export {
+ Dialog,
+ DialogClose,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogOverlay,
+ DialogPortal,
+ DialogTitle,
+ DialogTrigger,
+}
diff --git a/admin/src/components/ui/input.tsx b/admin/src/components/ui/input.tsx
new file mode 100644
index 0000000..1158fea
--- /dev/null
+++ b/admin/src/components/ui/input.tsx
@@ -0,0 +1,19 @@
+import * as React from "react"
+import { Input as InputPrimitive } from "@base-ui/react/input"
+import { cn } from "@/lib/utils"
+
+function Input({ className, type, ...props }: React.ComponentProps<"input">) {
+ return (
+
+ )
+}
+
+export { Input }
diff --git a/admin/src/components/ui/label.tsx b/admin/src/components/ui/label.tsx
new file mode 100644
index 0000000..c9e74c0
--- /dev/null
+++ b/admin/src/components/ui/label.tsx
@@ -0,0 +1,17 @@
+import * as React from "react"
+import { cn } from "@/lib/utils"
+
+function Label({ className, ...props }: React.ComponentProps<"label">) {
+ return (
+
+ )
+}
+
+export { Label }
diff --git a/admin/src/components/ui/select.tsx b/admin/src/components/ui/select.tsx
new file mode 100644
index 0000000..1dee7e4
--- /dev/null
+++ b/admin/src/components/ui/select.tsx
@@ -0,0 +1,200 @@
+"use client"
+
+import * as React from "react"
+import { Select as SelectPrimitive } from "@base-ui/react/select"
+import { cn } from "@/lib/utils"
+import { ChevronDownIcon, CheckIcon, ChevronUpIcon } from "lucide-react"
+
+const Select = SelectPrimitive.Root
+
+function SelectGroup({ className, ...props }: SelectPrimitive.Group.Props) {
+ return (
+
+ )
+}
+
+function SelectValue({ className, ...props }: SelectPrimitive.Value.Props) {
+ return (
+
+ )
+}
+
+function SelectTrigger({
+ className,
+ size = "default",
+ children,
+ ...props
+}: SelectPrimitive.Trigger.Props & {
+ size?: "sm" | "default"
+}) {
+ return (
+
+ {children}
+
+ }
+ />
+
+ )
+}
+
+function SelectContent({
+ className,
+ children,
+ side = "bottom",
+ sideOffset = 4,
+ align = "center",
+ alignOffset = 0,
+ alignItemWithTrigger = true,
+ ...props
+}: SelectPrimitive.Popup.Props &
+ Pick<
+ SelectPrimitive.Positioner.Props,
+ "align" | "alignOffset" | "side" | "sideOffset" | "alignItemWithTrigger"
+ >) {
+ return (
+
+
+
+
+ {children}
+
+
+
+
+ )
+}
+
+function SelectLabel({
+ className,
+ ...props
+}: SelectPrimitive.GroupLabel.Props) {
+ return (
+
+ )
+}
+
+function SelectItem({
+ className,
+ children,
+ ...props
+}: SelectPrimitive.Item.Props) {
+ return (
+
+
+ {children}
+
+
+ }
+ >
+
+
+
+ )
+}
+
+function SelectSeparator({
+ className,
+ ...props
+}: SelectPrimitive.Separator.Props) {
+ return (
+
+ )
+}
+
+function SelectScrollUpButton({
+ className,
+ ...props
+}: React.ComponentProps
) {
+ return (
+
+
+
+ )
+}
+
+function SelectScrollDownButton({
+ className,
+ ...props
+}: React.ComponentProps) {
+ return (
+
+
+
+ )
+}
+
+export {
+ Select,
+ SelectContent,
+ SelectGroup,
+ SelectItem,
+ SelectLabel,
+ SelectScrollDownButton,
+ SelectScrollUpButton,
+ SelectSeparator,
+ SelectTrigger,
+ SelectValue,
+}
diff --git a/admin/src/components/ui/separator.tsx b/admin/src/components/ui/separator.tsx
new file mode 100644
index 0000000..ed5bcef
--- /dev/null
+++ b/admin/src/components/ui/separator.tsx
@@ -0,0 +1,22 @@
+import { Separator as SeparatorPrimitive } from "@base-ui/react/separator"
+import { cn } from "@/lib/utils"
+
+function Separator({
+ className,
+ orientation = "horizontal",
+ ...props
+}: SeparatorPrimitive.Props) {
+ return (
+
+ )
+}
+
+export { Separator }
diff --git a/admin/src/components/ui/sonner.tsx b/admin/src/components/ui/sonner.tsx
new file mode 100644
index 0000000..9280ee5
--- /dev/null
+++ b/admin/src/components/ui/sonner.tsx
@@ -0,0 +1,49 @@
+"use client"
+
+import { useTheme } from "next-themes"
+import { Toaster as Sonner, type ToasterProps } from "sonner"
+import { CircleCheckIcon, InfoIcon, TriangleAlertIcon, OctagonXIcon, Loader2Icon } from "lucide-react"
+
+const Toaster = ({ ...props }: ToasterProps) => {
+ const { theme = "system" } = useTheme()
+
+ return (
+
+ ),
+ info: (
+
+ ),
+ warning: (
+
+ ),
+ error: (
+
+ ),
+ loading: (
+
+ ),
+ }}
+ style={
+ {
+ "--normal-bg": "var(--popover)",
+ "--normal-text": "var(--popover-foreground)",
+ "--normal-border": "var(--border)",
+ "--border-radius": "var(--radius)",
+ } as React.CSSProperties
+ }
+ toastOptions={{
+ classNames: {
+ toast: "cn-toast",
+ },
+ }}
+ {...props}
+ />
+ )
+}
+
+export { Toaster }
diff --git a/admin/src/components/ui/switch.tsx b/admin/src/components/ui/switch.tsx
new file mode 100644
index 0000000..57c7c26
--- /dev/null
+++ b/admin/src/components/ui/switch.tsx
@@ -0,0 +1,29 @@
+import { Switch as SwitchPrimitive } from "@base-ui/react/switch"
+import { cn } from "@/lib/utils"
+
+function Switch({
+ className,
+ size = "default",
+ ...props
+}: SwitchPrimitive.Root.Props & {
+ size?: "sm" | "default"
+}) {
+ return (
+
+
+
+ )
+}
+
+export { Switch }
diff --git a/admin/src/components/ui/table.tsx b/admin/src/components/ui/table.tsx
new file mode 100644
index 0000000..c02e2af
--- /dev/null
+++ b/admin/src/components/ui/table.tsx
@@ -0,0 +1,115 @@
+"use client"
+
+import * as React from "react"
+import { cn } from "@/lib/utils"
+
+function Table({ className, ...props }: React.ComponentProps<"table">) {
+ return (
+
+ )
+}
+
+function TableHeader({ className, ...props }: React.ComponentProps<"thead">) {
+ return (
+
+ )
+}
+
+function TableBody({ className, ...props }: React.ComponentProps<"tbody">) {
+ return (
+
+ )
+}
+
+function TableFooter({ className, ...props }: React.ComponentProps<"tfoot">) {
+ return (
+ tr]:last:border-b-0",
+ className
+ )}
+ {...props}
+ />
+ )
+}
+
+function TableRow({ className, ...props }: React.ComponentProps<"tr">) {
+ return (
+
+ )
+}
+
+function TableHead({ className, ...props }: React.ComponentProps<"th">) {
+ return (
+ |
+ )
+}
+
+function TableCell({ className, ...props }: React.ComponentProps<"td">) {
+ return (
+ |
+ )
+}
+
+function TableCaption({
+ className,
+ ...props
+}: React.ComponentProps<"caption">) {
+ return (
+
+ )
+}
+
+export {
+ Table,
+ TableHeader,
+ TableBody,
+ TableFooter,
+ TableHead,
+ TableRow,
+ TableCell,
+ TableCaption,
+}
diff --git a/admin/src/components/ui/tabs.tsx b/admin/src/components/ui/tabs.tsx
new file mode 100644
index 0000000..841883a
--- /dev/null
+++ b/admin/src/components/ui/tabs.tsx
@@ -0,0 +1,79 @@
+import { Tabs as TabsPrimitive } from "@base-ui/react/tabs"
+import { cva, type VariantProps } from "class-variance-authority"
+import { cn } from "@/lib/utils"
+
+function Tabs({
+ className,
+ orientation = "horizontal",
+ ...props
+}: TabsPrimitive.Root.Props) {
+ return (
+
+ )
+}
+
+const tabsListVariants = cva(
+ "group/tabs-list inline-flex w-fit items-center justify-center rounded-lg p-[3px] text-muted-foreground group-data-horizontal/tabs:h-8 group-data-vertical/tabs:h-fit group-data-vertical/tabs:flex-col data-[variant=line]:rounded-none",
+ {
+ variants: {
+ variant: {
+ default: "bg-muted",
+ line: "gap-1 bg-transparent",
+ },
+ },
+ defaultVariants: {
+ variant: "default",
+ },
+ }
+)
+
+function TabsList({
+ className,
+ variant = "default",
+ ...props
+}: TabsPrimitive.List.Props & VariantProps) {
+ return (
+
+ )
+}
+
+function TabsTrigger({ className, ...props }: TabsPrimitive.Tab.Props) {
+ return (
+
+ )
+}
+
+function TabsContent({ className, ...props }: TabsPrimitive.Panel.Props) {
+ return (
+
+ )
+}
+
+export { Tabs, TabsList, TabsTrigger, TabsContent, tabsListVariants }
diff --git a/admin/src/components/ui/textarea.tsx b/admin/src/components/ui/textarea.tsx
new file mode 100644
index 0000000..9662e56
--- /dev/null
+++ b/admin/src/components/ui/textarea.tsx
@@ -0,0 +1,17 @@
+import * as React from "react"
+import { cn } from "@/lib/utils"
+
+function Textarea({ className, ...props }: React.ComponentProps<"textarea">) {
+ return (
+
+ )
+}
+
+export { Textarea }
diff --git a/admin/src/index.css b/admin/src/index.css
new file mode 100644
index 0000000..fb3c7e9
--- /dev/null
+++ b/admin/src/index.css
@@ -0,0 +1,130 @@
+@import "tailwindcss";
+@import "tw-animate-css";
+@import "shadcn/tailwind.css";
+@import "@fontsource-variable/geist";
+
+@custom-variant dark (&:is(.dark *));
+
+@theme inline {
+ --font-heading: var(--font-sans);
+ --font-sans: 'Geist Variable', sans-serif;
+ --color-sidebar-ring: var(--sidebar-ring);
+ --color-sidebar-border: var(--sidebar-border);
+ --color-sidebar-accent-foreground: var(--sidebar-accent-foreground);
+ --color-sidebar-accent: var(--sidebar-accent);
+ --color-sidebar-primary-foreground: var(--sidebar-primary-foreground);
+ --color-sidebar-primary: var(--sidebar-primary);
+ --color-sidebar-foreground: var(--sidebar-foreground);
+ --color-sidebar: var(--sidebar);
+ --color-chart-5: var(--chart-5);
+ --color-chart-4: var(--chart-4);
+ --color-chart-3: var(--chart-3);
+ --color-chart-2: var(--chart-2);
+ --color-chart-1: var(--chart-1);
+ --color-ring: var(--ring);
+ --color-input: var(--input);
+ --color-border: var(--border);
+ --color-destructive: var(--destructive);
+ --color-accent-foreground: var(--accent-foreground);
+ --color-accent: var(--accent);
+ --color-muted-foreground: var(--muted-foreground);
+ --color-muted: var(--muted);
+ --color-secondary-foreground: var(--secondary-foreground);
+ --color-secondary: var(--secondary);
+ --color-primary-foreground: var(--primary-foreground);
+ --color-primary: var(--primary);
+ --color-popover-foreground: var(--popover-foreground);
+ --color-popover: var(--popover);
+ --color-card-foreground: var(--card-foreground);
+ --color-card: var(--card);
+ --color-foreground: var(--foreground);
+ --color-background: var(--background);
+ --radius-sm: calc(var(--radius) * 0.6);
+ --radius-md: calc(var(--radius) * 0.8);
+ --radius-lg: var(--radius);
+ --radius-xl: calc(var(--radius) * 1.4);
+ --radius-2xl: calc(var(--radius) * 1.8);
+ --radius-3xl: calc(var(--radius) * 2.2);
+ --radius-4xl: calc(var(--radius) * 2.6);
+}
+
+:root {
+ --background: oklch(1 0 0);
+ --foreground: oklch(0.145 0 0);
+ --card: oklch(1 0 0);
+ --card-foreground: oklch(0.145 0 0);
+ --popover: oklch(1 0 0);
+ --popover-foreground: oklch(0.145 0 0);
+ --primary: oklch(0.205 0 0);
+ --primary-foreground: oklch(0.985 0 0);
+ --secondary: oklch(0.97 0 0);
+ --secondary-foreground: oklch(0.205 0 0);
+ --muted: oklch(0.97 0 0);
+ --muted-foreground: oklch(0.556 0 0);
+ --accent: oklch(0.97 0 0);
+ --accent-foreground: oklch(0.205 0 0);
+ --destructive: oklch(0.577 0.245 27.325);
+ --border: oklch(0.922 0 0);
+ --input: oklch(0.922 0 0);
+ --ring: oklch(0.708 0 0);
+ --chart-1: oklch(0.87 0 0);
+ --chart-2: oklch(0.556 0 0);
+ --chart-3: oklch(0.439 0 0);
+ --chart-4: oklch(0.371 0 0);
+ --chart-5: oklch(0.269 0 0);
+ --radius: 0.625rem;
+ --sidebar: oklch(0.985 0 0);
+ --sidebar-foreground: oklch(0.145 0 0);
+ --sidebar-primary: oklch(0.205 0 0);
+ --sidebar-primary-foreground: oklch(0.985 0 0);
+ --sidebar-accent: oklch(0.97 0 0);
+ --sidebar-accent-foreground: oklch(0.205 0 0);
+ --sidebar-border: oklch(0.922 0 0);
+ --sidebar-ring: oklch(0.708 0 0);
+}
+
+.dark {
+ --background: oklch(0.145 0 0);
+ --foreground: oklch(0.985 0 0);
+ --card: oklch(0.205 0 0);
+ --card-foreground: oklch(0.985 0 0);
+ --popover: oklch(0.205 0 0);
+ --popover-foreground: oklch(0.985 0 0);
+ --primary: oklch(0.922 0 0);
+ --primary-foreground: oklch(0.205 0 0);
+ --secondary: oklch(0.269 0 0);
+ --secondary-foreground: oklch(0.985 0 0);
+ --muted: oklch(0.269 0 0);
+ --muted-foreground: oklch(0.708 0 0);
+ --accent: oklch(0.269 0 0);
+ --accent-foreground: oklch(0.985 0 0);
+ --destructive: oklch(0.704 0.191 22.216);
+ --border: oklch(1 0 0 / 10%);
+ --input: oklch(1 0 0 / 15%);
+ --ring: oklch(0.556 0 0);
+ --chart-1: oklch(0.87 0 0);
+ --chart-2: oklch(0.556 0 0);
+ --chart-3: oklch(0.439 0 0);
+ --chart-4: oklch(0.371 0 0);
+ --chart-5: oklch(0.269 0 0);
+ --sidebar: oklch(0.205 0 0);
+ --sidebar-foreground: oklch(0.985 0 0);
+ --sidebar-primary: oklch(0.488 0.243 264.376);
+ --sidebar-primary-foreground: oklch(0.985 0 0);
+ --sidebar-accent: oklch(0.269 0 0);
+ --sidebar-accent-foreground: oklch(0.985 0 0);
+ --sidebar-border: oklch(1 0 0 / 10%);
+ --sidebar-ring: oklch(0.556 0 0);
+}
+
+@layer base {
+ * {
+ @apply border-border outline-ring/50;
+ }
+ body {
+ @apply bg-background text-foreground;
+ }
+ html {
+ @apply font-sans;
+ }
+}
\ No newline at end of file
diff --git a/admin/src/lib/utils.ts b/admin/src/lib/utils.ts
new file mode 100644
index 0000000..fed2fe9
--- /dev/null
+++ b/admin/src/lib/utils.ts
@@ -0,0 +1,6 @@
+import { clsx, type ClassValue } from 'clsx'
+import { twMerge } from 'tailwind-merge'
+
+export function cn(...inputs: ClassValue[]) {
+ return twMerge(clsx(inputs))
+}
diff --git a/admin/src/main.tsx b/admin/src/main.tsx
new file mode 100644
index 0000000..96e207c
--- /dev/null
+++ b/admin/src/main.tsx
@@ -0,0 +1,5 @@
+import { StrictMode } from 'react'
+import { createRoot } from 'react-dom/client'
+import './index.css'
+
+createRoot(document.getElementById('root')!).render()
diff --git a/admin/src/test/setup.ts b/admin/src/test/setup.ts
new file mode 100644
index 0000000..f79e1fc
--- /dev/null
+++ b/admin/src/test/setup.ts
@@ -0,0 +1,8 @@
+import '@testing-library/jest-dom/vitest'
+import { cleanup } from '@testing-library/react'
+import { afterEach } from 'vitest'
+
+afterEach(() => {
+ cleanup()
+ sessionStorage.clear()
+})
diff --git a/admin/tsconfig.app.json b/admin/tsconfig.app.json
new file mode 100644
index 0000000..60e9341
--- /dev/null
+++ b/admin/tsconfig.app.json
@@ -0,0 +1,27 @@
+{
+ "compilerOptions": {
+ "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
+ "target": "es2023",
+ "lib": ["ES2023", "DOM"],
+ "module": "esnext",
+ "types": ["vite/client", "vitest/globals", "@testing-library/jest-dom"],
+ "allowArbitraryExtensions": true,
+ "skipLibCheck": true,
+
+ /* Bundler mode */
+ "moduleResolution": "bundler",
+ "allowImportingTsExtensions": true,
+ "verbatimModuleSyntax": true,
+ "moduleDetection": "force",
+ "noEmit": true,
+ "jsx": "react-jsx",
+
+ /* Linting */
+ "noUnusedLocals": true,
+ "noUnusedParameters": true,
+ "erasableSyntaxOnly": true,
+ "paths": { "@/*": ["./src/*"] },
+ "noFallthroughCasesInSwitch": true
+ },
+ "include": ["src"]
+}
diff --git a/admin/tsconfig.json b/admin/tsconfig.json
new file mode 100644
index 0000000..ccf8818
--- /dev/null
+++ b/admin/tsconfig.json
@@ -0,0 +1,18 @@
+{
+ "files": [],
+ "references": [
+ {
+ "path": "./tsconfig.app.json"
+ },
+ {
+ "path": "./tsconfig.node.json"
+ }
+ ],
+ "compilerOptions": {
+ "paths": {
+ "@/*": [
+ "./src/*"
+ ]
+ }
+ }
+}
\ No newline at end of file
diff --git a/admin/tsconfig.node.json b/admin/tsconfig.node.json
new file mode 100644
index 0000000..8455dcb
--- /dev/null
+++ b/admin/tsconfig.node.json
@@ -0,0 +1,23 @@
+{
+ "compilerOptions": {
+ "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
+ "target": "es2023",
+ "lib": ["ES2023"],
+ "types": ["node"],
+ "skipLibCheck": true,
+
+ /* Bundler mode */
+ "module": "nodenext",
+ "allowImportingTsExtensions": true,
+ "verbatimModuleSyntax": true,
+ "moduleDetection": "force",
+ "noEmit": true,
+
+ /* Linting */
+ "noUnusedLocals": true,
+ "noUnusedParameters": true,
+ "erasableSyntaxOnly": true,
+ "noFallthroughCasesInSwitch": true
+ },
+ "include": ["vite.config.ts"]
+}
diff --git a/admin/vite.config.ts b/admin/vite.config.ts
new file mode 100644
index 0000000..e39c4f9
--- /dev/null
+++ b/admin/vite.config.ts
@@ -0,0 +1,25 @@
+///
+import path from 'node:path'
+import tailwindcss from '@tailwindcss/vite'
+import react from '@vitejs/plugin-react'
+import { defineConfig } from 'vite'
+
+// The Go binary embeds the build output from internal/adminui/dist.
+export default defineConfig({
+ plugins: [react(), tailwindcss()],
+ resolve: { alias: { '@': path.resolve(import.meta.dirname, 'src') } },
+ build: {
+ outDir: '../internal/adminui/dist',
+ emptyOutDir: false, // keeps .gitkeep; the build script removes stale files
+ },
+ server: {
+ // `npm run dev` talks to a daemon running on the default API port.
+ proxy: { '/api': 'http://127.0.0.1:8080' },
+ },
+ test: {
+ environment: 'jsdom',
+ globals: true,
+ setupFiles: ['./src/test/setup.ts'],
+ css: false,
+ },
+})
diff --git a/internal/adminui/adminui.go b/internal/adminui/adminui.go
new file mode 100644
index 0000000..1072dfa
--- /dev/null
+++ b/internal/adminui/adminui.go
@@ -0,0 +1,88 @@
+// Package adminui serves the admin single-page app built from /admin.
+package adminui
+
+import (
+ "bytes"
+ "embed"
+ "io/fs"
+ "net/http"
+ "path"
+ "strings"
+ "time"
+)
+
+// dist holds the Vite build. A fresh checkout contains only .gitkeep: the
+// daemon still builds and answers 503 until `make admin` has run.
+//
+//go:embed all:dist
+var dist embed.FS
+
+// Embedded returns the handler for the UI built into the binary.
+func Embedded() http.Handler {
+ sub, err := fs.Sub(dist, "dist")
+ if err != nil {
+ panic(err) // dist is a compile-time constant directory
+ }
+ return New(sub)
+}
+
+const csp = "default-src 'self'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; " +
+ "object-src 'none'; base-uri 'none'; frame-ancestors 'none'; form-action 'self'"
+
+// New serves a built single-page app from fsys. Unknown paths without a file
+// extension fall back to index.html so client-side routes survive a reload.
+func New(fsys fs.FS) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ h := w.Header()
+ h.Set("Content-Security-Policy", csp)
+ h.Set("X-Content-Type-Options", "nosniff")
+ h.Set("Referrer-Policy", "no-referrer")
+ h.Set("X-Frame-Options", "DENY")
+
+ if r.Method != http.MethodGet && r.Method != http.MethodHead {
+ h.Set("Allow", "GET, HEAD")
+ http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
+ return
+ }
+ p := path.Clean("/" + r.URL.Path)
+ if p == "/api" || strings.HasPrefix(p, "/api/") {
+ http.NotFound(w, r) // the API is mounted separately; never answer with HTML
+ return
+ }
+ if _, err := fs.Stat(fsys, "index.html"); err != nil {
+ http.Error(w, "the admin UI is not built: run `make admin` and rebuild the binary", http.StatusServiceUnavailable)
+ return
+ }
+
+ name := strings.TrimPrefix(p, "/")
+ if name == "" {
+ name = "index.html"
+ }
+ switch {
+ case name == "assets" || strings.HasPrefix(name, "assets/"):
+ // Vite fingerprints everything under assets/.
+ if !serve(w, r, fsys, name, "public, max-age=31536000, immutable") {
+ http.NotFound(w, r)
+ }
+ case serve(w, r, fsys, name, "no-cache"):
+ case path.Ext(name) != "":
+ http.NotFound(w, r)
+ default:
+ serve(w, r, fsys, "index.html", "no-cache")
+ }
+ })
+}
+
+// serve writes a regular file and reports whether it exists.
+func serve(w http.ResponseWriter, r *http.Request, fsys fs.FS, name, cache string) bool {
+ if info, err := fs.Stat(fsys, name); err != nil || info.IsDir() {
+ return false
+ }
+ b, err := fs.ReadFile(fsys, name)
+ if err != nil {
+ return false
+ }
+ w.Header().Set("Cache-Control", cache)
+ http.ServeContent(w, r, name, time.Time{}, bytes.NewReader(b))
+ return true
+}
diff --git a/internal/adminui/adminui_test.go b/internal/adminui/adminui_test.go
new file mode 100644
index 0000000..dac6041
--- /dev/null
+++ b/internal/adminui/adminui_test.go
@@ -0,0 +1,154 @@
+package adminui
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+ "testing/fstest"
+)
+
+func site() fstest.MapFS {
+ return fstest.MapFS{
+ "index.html": {Data: []byte("")},
+ "assets/app-abc123.js": {Data: []byte("console.log(1)")},
+ "assets/app-abc123.css": {Data: []byte("body{}")},
+ "favicon.svg": {Data: []byte("")},
+ }
+}
+
+func fetch(h http.Handler, method, path string) *httptest.ResponseRecorder {
+ rec := httptest.NewRecorder()
+ h.ServeHTTP(rec, httptest.NewRequest(method, path, nil))
+ return rec
+}
+
+func TestServesIndex(t *testing.T) {
+ rec := fetch(New(site()), "GET", "/")
+ if rec.Code != 200 || !strings.Contains(rec.Body.String(), `id=root`) {
+ t.Fatalf("%d %q", rec.Code, rec.Body.String())
+ }
+ if got := rec.Header().Get("Cache-Control"); got != "no-cache" {
+ t.Errorf("index must be revalidated, Cache-Control = %q", got)
+ }
+ if !strings.HasPrefix(rec.Header().Get("Content-Type"), "text/html") {
+ t.Errorf("content type = %q", rec.Header().Get("Content-Type"))
+ }
+}
+
+func TestHashedAssetsAreImmutable(t *testing.T) {
+ h := New(site())
+ js := fetch(h, "GET", "/assets/app-abc123.js")
+ if js.Code != 200 || js.Body.String() != "console.log(1)" {
+ t.Fatalf("%d %q", js.Code, js.Body.String())
+ }
+ if got := js.Header().Get("Cache-Control"); got != "public, max-age=31536000, immutable" {
+ t.Errorf("Cache-Control = %q", got)
+ }
+ if ct := js.Header().Get("Content-Type"); !strings.Contains(ct, "javascript") {
+ t.Errorf("js content type = %q", ct)
+ }
+ if ct := fetch(h, "GET", "/assets/app-abc123.css").Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/css") {
+ t.Errorf("css content type = %q", ct)
+ }
+}
+
+func TestOtherStaticFilesAreServedWithoutLongCaching(t *testing.T) {
+ rec := fetch(New(site()), "GET", "/favicon.svg")
+ if rec.Code != 200 || rec.Body.String() != "" {
+ t.Fatalf("%d %q", rec.Code, rec.Body.String())
+ }
+ if strings.Contains(rec.Header().Get("Cache-Control"), "immutable") {
+ t.Error("only hashed assets are immutable")
+ }
+}
+
+func TestClientRoutesFallBackToIndex(t *testing.T) {
+ h := New(site())
+ for _, p := range []string{"/lists", "/lists/12", "/lists/12/members", "/login"} {
+ rec := fetch(h, "GET", p)
+ if rec.Code != 200 || !strings.Contains(rec.Body.String(), "id=root") {
+ t.Errorf("%s: %d %q", p, rec.Code, rec.Body.String())
+ }
+ }
+}
+
+func TestMissingAssetsAre404NotHTML(t *testing.T) {
+ h := New(site())
+ for _, p := range []string{"/assets/missing.js", "/assets/nope/deeper.css", "/assets/", "/robots.js"} {
+ rec := fetch(h, "GET", p)
+ if rec.Code != 404 {
+ t.Errorf("%s: want 404, got %d", p, rec.Code)
+ }
+ if strings.Contains(rec.Body.String(), "id=root") {
+ t.Errorf("%s: a missing script must not be answered with the HTML shell", p)
+ }
+ }
+}
+
+func TestNeverAnswersForTheAPI(t *testing.T) {
+ h := New(site())
+ for _, p := range []string{"/api", "/api/", "/api/v1/lists", "/api/v2/x"} {
+ rec := fetch(h, "GET", p)
+ if rec.Code != 404 || strings.Contains(rec.Body.String(), "id=root") {
+ t.Errorf("%s: %d %q", p, rec.Code, rec.Body.String())
+ }
+ }
+}
+
+func TestOnlyReadMethods(t *testing.T) {
+ h := New(site())
+ for _, m := range []string{"POST", "PUT", "DELETE", "PATCH"} {
+ if rec := fetch(h, m, "/"); rec.Code != 405 {
+ t.Errorf("%s: %d", m, rec.Code)
+ }
+ }
+ if rec := fetch(h, "HEAD", "/"); rec.Code != 200 || rec.Body.Len() != 0 {
+ t.Errorf("HEAD: %d %q", rec.Code, rec.Body.String())
+ }
+}
+
+func TestSecurityHeaders(t *testing.T) {
+ for _, p := range []string{"/", "/lists/1", "/assets/app-abc123.js"} {
+ h := fetch(New(site()), "GET", p).Header()
+ csp := h.Get("Content-Security-Policy")
+ for _, want := range []string{"default-src 'self'", "frame-ancestors 'none'", "base-uri 'none'", "object-src 'none'"} {
+ if !strings.Contains(csp, want) {
+ t.Errorf("%s: CSP %q lacks %q", p, csp, want)
+ }
+ }
+ if h.Get("X-Content-Type-Options") != "nosniff" || h.Get("Referrer-Policy") != "no-referrer" || h.Get("X-Frame-Options") != "DENY" {
+ t.Errorf("%s: headers %v", p, h)
+ }
+ }
+}
+
+func TestTraversalAttemptsAreContained(t *testing.T) {
+ fs := site()
+ fs["secret.txt"] = &fstest.MapFile{Data: []byte("top secret")}
+ h := New(fstest.MapFS{"index.html": fs["index.html"]})
+ for _, p := range []string{"/../secret.txt", "/%2e%2e/secret.txt", "/assets/../../secret.txt", "//etc/passwd"} {
+ rec := fetch(h, "GET", p)
+ if strings.Contains(rec.Body.String(), "top secret") || strings.Contains(rec.Body.String(), "root:") {
+ t.Errorf("%s leaked a file", p)
+ }
+ }
+}
+
+func TestNotBuiltAnswers503WithAHint(t *testing.T) {
+ rec := fetch(New(fstest.MapFS{".gitkeep": {}}), "GET", "/")
+ if rec.Code != 503 || !strings.Contains(rec.Body.String(), "make admin") {
+ t.Fatalf("%d %q", rec.Code, rec.Body.String())
+ }
+ if rec := fetch(New(fstest.MapFS{}), "GET", "/lists"); rec.Code != 503 {
+ t.Fatalf("client route without a build: %d", rec.Code)
+ }
+}
+
+func TestEmbeddedHandlerWorksWithoutABuild(t *testing.T) {
+ // The embedded dist holds only .gitkeep in a fresh checkout; it must still compile and answer.
+ rec := fetch(Embedded(), "GET", "/")
+ if rec.Code != 200 && rec.Code != 503 {
+ t.Fatalf("status = %d", rec.Code)
+ }
+}
diff --git a/internal/adminui/dist/.gitkeep b/internal/adminui/dist/.gitkeep
new file mode 100644
index 0000000..e69de29
diff --git a/internal/app/app.go b/internal/app/app.go
index 55fc292..ea90216 100644
--- a/internal/app/app.go
+++ b/internal/app/app.go
@@ -14,6 +14,7 @@ import (
"github.com/emersion/go-smtp"
+ "codeberg.org/squidd/parsimony/internal/adminui"
"codeberg.org/squidd/parsimony/internal/api"
"codeberg.org/squidd/parsimony/internal/config"
"codeberg.org/squidd/parsimony/internal/ingest"
@@ -83,7 +84,11 @@ func Start(cfg config.Config, apiToken string) (*App, error) {
}
a.APIAddr = apiLn.Addr().String()
a.lns = append(a.lns, apiLn)
- a.web = &http.Server{Handler: api.New(st, apiToken, releaser{rt, relay}), ReadHeaderTimeout: 10 * time.Second}
+ // The admin UI and the API share a listener, so the UI needs no CORS.
+ mux := http.NewServeMux()
+ mux.Handle("/api/", api.New(st, apiToken, releaser{rt, relay}))
+ mux.Handle("/", adminui.Embedded())
+ a.web = &http.Server{Handler: mux, ReadHeaderTimeout: 10 * time.Second}
}
var pubLn net.Listener
diff --git a/internal/app/app_test.go b/internal/app/app_test.go
index 020331b..a931e22 100644
--- a/internal/app/app_test.go
+++ b/internal/app/app_test.go
@@ -349,3 +349,40 @@ func TestCloseDropsOpenConnections(t *testing.T) {
}
}
}
+
+func TestAdminUIAndAPIShareTheListener(t *testing.T) {
+ relayAddr, _ := startSink(t)
+ a, err := Start(testConfig(t, relayAddr), "tok")
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { a.Close() })
+ base := "http://" + a.APIAddr
+
+ // The API is not shadowed by the UI.
+ if code, body := fetch(t, "GET", base+"/api/v1/health"); code != 200 || !strings.Contains(body, `"status"`) {
+ t.Fatalf("health: %d %q", code, body)
+ }
+ if code, _ := fetch(t, "GET", base+"/api/v1/lists"); code != 401 {
+ t.Fatalf("api must still require a token, got %d", code)
+ }
+ if code, body := fetch(t, "GET", base+"/api/nope"); code == 200 || strings.Contains(strings.ToLower(body), "") {
+ t.Fatalf("%s: not the UI: %q", p, body)
+ }
+ case 503:
+ if !strings.Contains(body, "make admin") {
+ t.Fatalf("%s: 503 without a hint: %q", p, body)
+ }
+ default:
+ t.Fatalf("%s: status %d", p, code)
+ }
+ }
+}