API Internals
Go API Architecture
Internal structure of the Go control plane API.
Project Layout
api/
cmd/api/ # Entry point (main.go)
internal/
config/ # App configuration (env vars)
db/
migrations/ # SQL migrations (goose)
queries/ # sqlc query definitions
sqlc/ # Generated Go code
handler/ # HTTP handlers (chi router)
middleware/ # Auth, logging, CORS middleware
orchestrator/ # Nomad job management
runtime/ # RuntimeProvider interface + implementations
service/ # Business logic layer
caddy/ # Caddy Admin API clientRequest Flow
HTTP → chi router → middleware (auth, logging) → handler → service → db/orchestratorKey Patterns
- chi router — lightweight, stdlib-compatible HTTP router
- sqlc — type-safe SQL queries, no ORM
- pgx/v5 — PostgreSQL driver with connection pooling
- hashicorp/nomad/api — Nomad API client for job management
- Error wrapping —
fmt.Errorf("context: %w", err)everywhere
RuntimeProvider Interface
internal/runtime/provider.go defines the abstraction for different AI runtimes:
GetStatus()— health checkGetConfig()— read runtime configPatchConfig()— update runtime config- Channel management methods
Implementations:
openclaw.go— WebSocket-basedzeroclaw.go— HTTP REST-based