# Phase 8: REST API Layer **Status:** ⬜ Not Started **Parent plan:** [PLAN.md](./PLAN.md) **Domain:** backend ## Objective Wire up all REST API endpoints using chi router, connecting the store, deployer, and other services to HTTP handlers. ## Tasks - [ ] Task 1: Set up chi router with middleware (logging, recovery, CORS, JSON content-type) - [ ] Task 2: Implement project endpoints — GET/POST /api/projects, GET/PUT/DELETE /api/projects/:id - [ ] Task 3: Implement stage endpoints — POST /api/projects/:id/stages, PUT/DELETE /api/projects/:id/stages/:stage - [ ] Task 4: Implement instance endpoints — GET /api/projects/:id/stages/:stage/instances, POST (deploy), DELETE (remove) - [ ] Task 5: Implement instance control endpoints — POST .../instances/:iid/stop, start, restart - [ ] Task 6: Implement quick deploy endpoints — POST /api/deploy/inspect, POST /api/deploy/quick - [ ] Task 7: Implement registry endpoints — GET/POST /api/registries, PUT/DELETE /api/registries/:id, POST .../test - [ ] Task 8: Implement settings endpoints — GET/PUT /api/settings, GET /api/settings/webhook-url, POST .../regenerate - [ ] Task 9: Implement deploy history endpoints — GET /api/deploys, GET /api/deploys/:id/logs (SSE stub) - [ ] Task 10: Implement registry tags endpoint — GET /api/registries/:id/tags/:image - [ ] Task 11: Wire webhook handler into router — POST /api/webhook/:secret-uuid - [ ] Task 12: Wire everything in main.go — initialize all services, start HTTP server ## Files to Modify/Create - `internal/api/router.go` — chi router setup, middleware - `internal/api/projects.go` — project CRUD handlers - `internal/api/stages.go` — stage CRUD handlers - `internal/api/instances.go` — instance lifecycle handlers - `internal/api/deploys.go` — deploy + quick deploy handlers - `internal/api/registries.go` — registry CRUD + test + tags handlers - `internal/api/settings.go` — settings handlers - `internal/api/middleware.go` — middleware (logging, CORS, recovery) - `internal/api/response.go` — consistent API response helpers (envelope format) - `cmd/server/main.go` — full service wiring and HTTP server start ## Acceptance Criteria - All endpoints from the API spec in PLAN.md are implemented - Consistent JSON envelope response format (success, data, error, metadata) - CORS configured for frontend dev (localhost origins) - Proper HTTP status codes (200, 201, 400, 404, 500) - main.go starts a fully wired HTTP server ## Notes - Response envelope: `{"success": bool, "data": any, "error": string|null, "meta": {pagination}}` - CORS: allow all origins in dev, restrict in production (configurable later) - SSE for deploy logs is a stub in this phase — real implementation in Phase 11 - Quick deploy: /inspect pulls and inspects image, returns defaults; /quick creates project + deploys - All handlers should validate input and return 400 for bad requests ## Review Checklist - [ ] All tasks completed - [ ] All API endpoints from PLAN.md are covered - [ ] Consistent response format across all endpoints - [ ] Input validation on all POST/PUT handlers - [ ] No business logic in handlers (delegates to services) ## Handoff to Next Phase