Files
Control-Center/go-backend/internal/router/router.go
Joshua cce3e061a7
Some checks failed
Dev Build / build-test (pull_request) Failing after 11m6s
Dev Build / build-test (push) Successful in 1m54s
CUB-127: implement Control Center CRUD API in Go
2026-05-06 17:29:44 -04:00

66 lines
2.5 KiB
Go

// Package router configures the chi router with all routes, middleware,
// and handler wiring for the Control Center API.
package router
import (
"net/http"
"time"
"code.cubecraftcreations.com/CubeCraft-Creations/Control-Center/go-backend/internal/handler"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
)
// New creates a fully-configured chi router with all API routes mounted.
func New(h *handler.Handler) *chi.Mux {
r := chi.NewRouter()
// ── Global middleware ──────────────────────────────────────────────────
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(30 * time.Second))
// ── CORS — permissive for development ──────────────────────────────────
r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type"},
ExposedHeaders: []string{"Link", "X-Total-Count"},
AllowCredentials: false,
MaxAge: 300,
}))
// ── Health check ───────────────────────────────────────────────────────
r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{"status":"ok"}`))
})
// ── API v1 routes ──────────────────────────────────────────────────────
r.Route("/api", func(api chi.Router) {
// Agents CRUD
api.Route("/agents", func(agents chi.Router) {
agents.Get("/", h.ListAgents) // GET /api/agents
agents.Post("/", h.CreateAgent) // POST /api/agents
agents.Get("/{id}", h.GetAgent) // GET /api/agents/{id}
agents.Put("/{id}", h.UpdateAgent) // PUT /api/agents/{id}
agents.Delete("/{id}", h.DeleteAgent) // DELETE /api/agents/{id}
agents.Get("/{id}/history", h.AgentHistory) // GET /api/agents/{id}/history
})
// Sessions
api.Get("/sessions", h.ListSessions)
// Tasks
api.Get("/tasks", h.ListTasks)
// Projects
api.Get("/projects", h.ListProjects)
})
return r
}