3.2 KiB
3.2 KiB
CaddyProxyManager+ Copilot Instructions
Big Picture
backend/cmd/apiloads config, opens SQLite, then hands off tointernal/serverwhere routes frominternal/api/routesare registered.internal/configrespectsCPM_ENV,CPM_HTTP_PORT,CPM_DB_PATH,CPM_FRONTEND_DIRand creates thedata/directory; lean on these instead of hard-coded paths.- All HTTP endpoints live under
/api/v1/*; keep new handlers insideinternal/api/handlersand register them viaroutes.Registersodb.AutoMigrateruns for their models. internal/serveralso mounts the built React app (viaattachFrontend) wheneverfrontend/distexists, falling back to JSON{"error": ...}for any/api/*misses.- Persistent types live in
internal/models; GORM auto-migrates them each boot, so evolve schemas there before touching handlers or the frontend.
Backend Workflow
- Run locally with
cd backend && go run ./cmd/api; run tests withgo test ./...(seeproxy_host_handler_test.gofor the in-memory SQLite/Gin harness pattern). - Handlers return structured errors using
gin.H{"error": "message"}and standard HTTP codes—mirror theProxyHostHandlerlifecycle for new CRUD endpoints. - UUIDs (
github.com/google/uuid) are generated server-side and exposed asuuidfields; clients never send numeric IDs. - Query lists sorted by
updated_at desc(see.Order("updated_at desc")inList); match that ordering for user-visible collections. - Long-running work must respect the graceful shutdown flow in
server.Run(ctx)—avoid background goroutines that ignore the context.
Frontend Workflow
- React 18 + Vite + React Query; start with
cd frontend && npm install && npm run devso Vite proxies/apicalls tohttp://localhost:8080(configured invite.config.ts). - Consolidate HTTP calls via
src/api/client.ts; wrap them in hooks undersrc/hooksand expose query keys like['proxy-hosts']to keep cache invalidation simple. - Screens live in
src/pagesand render insidecomponents/Layout; navigation + active styles rely on React Router +clsx, so extend thelinksarray instead of hard-coding routes elsewhere. - Forms follow
pages/ProxyHosts.tsx: localuseStateper field, submit viauseMutation, then reset state andinvalidateQueriesfor the affected list on success. - Styling remains a single
src/index.cssgrid/aside theme; keep additions lightweight and avoid new design systems until shadcn/ui lands.
Cross-Cutting Notes
- Run the backend before the frontend; React Query expects the exact JSON produced by GORM tags (snake_case), so keep API and UI field names aligned.
- When adding models, update both
internal/modelsand theAutoMigratecall insideinternal/api/routes/routes.go; register new Gin routes right after migrations for clarity. - Tests belong beside handlers (
*_test.go); reuse thesetupTestRouterhelper structure (in-memory SQLite, Gin router, httptest requests) for fast feedback. - The root
Dockerfileis still the legacy Python scaffold—do not assume it builds this stack until it is replaced with the Go/React pipeline. - Branch from
feature/**and targetdevelopment; CI currently lints/tests placeholders, so rungo test ./...andnpm run buildlocally before opening PRs.