66 lines
2.5 KiB
Go
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
|
|
}
|