Files
Extrudex/backend/internal/router/router.go
Joshua fca2ef5b84
Some checks failed
Dev Build / build-test (pull_request) Failing after 2m4s
CUB-113: implement core CRUD API endpoints
- Add dtos package with request/response structs
- Add repositories: Material, Filament, Printer, PrintJob, UsageLog
- Add services: FilamentService, PrinterService, PrintJobService
- Add handlers for all 5 resources with consistent error responses
- Wire all endpoints into Chi router under /api
- Validation on POST/PUT filament endpoints
- Filter/pagination support on list endpoints
- Soft-delete for filaments (DELETE /api/filaments/{id})
- go build ./... && go vet ./... → PASS
2026-05-06 14:24:58 -04:00

85 lines
3.4 KiB
Go

package router
import (
"net/http"
"time"
"github.com/CubeCraft-Creations/Extrudex/backend/internal/config"
"github.com/CubeCraft-Creations/Extrudex/backend/internal/handlers"
"github.com/CubeCraft-Creations/Extrudex/backend/internal/repositories"
"github.com/CubeCraft-Creations/Extrudex/backend/internal/services"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/jackc/pgx/v5/pgxpool"
)
// New creates and configures a Chi router with all middleware and handlers mounted.
func New(cfg *config.Config, dbPool *pgxpool.Pool) chi.Router {
r := chi.NewRouter()
// Middleware
r.Use(middleware.RequestID)
r.Use(middleware.RealIP)
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(middleware.Timeout(60 * time.Second))
// CORS
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", cfg.CorsOrigin)
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
}
next.ServeHTTP(w, r)
})
})
// Health check
healthHandler := handlers.NewHealthHandler(dbPool)
r.Get("/health", healthHandler.ServeHTTP)
// ── Repositories ──────────────────────────────────────────────────────
materialRepo := repositories.NewMaterialRepository(dbPool)
filamentRepo := repositories.NewFilamentRepository(dbPool)
printerRepo := repositories.NewPrinterRepository(dbPool)
printJobRepo := repositories.NewPrintJobRepository(dbPool)
usageLogRepo := repositories.NewUsageLogRepository(dbPool)
// ── Services ──────────────────────────────────────────────────────────
filamentService := services.NewFilamentService(filamentRepo)
printerService := services.NewPrinterService(printerRepo)
printJobService := services.NewPrintJobService(printJobRepo)
// ── Handlers ──────────────────────────────────────────────────────────
materialHandler := handlers.NewMaterialHandler(materialRepo)
filamentHandler := handlers.NewFilamentHandler(filamentService)
printerHandler := handlers.NewPrinterHandler(printerService)
printJobHandler := handlers.NewPrintJobHandler(printJobService)
usageLogHandler := handlers.NewUsageLogHandler(usageLogRepo)
// ── API Routes ────────────────────────────────────────────────────────
r.Route("/api", func(r chi.Router) {
r.Get("/materials", materialHandler.List)
r.Route("/filaments", func(r chi.Router) {
r.Get("/", filamentHandler.List)
r.Post("/", filamentHandler.Create)
r.Route("/{id}", func(r chi.Router) {
r.Get("/", filamentHandler.Get)
r.Put("/", filamentHandler.Update)
r.Delete("/", filamentHandler.Delete)
})
})
r.Get("/printers", printerHandler.List)
r.Get("/print-jobs", printJobHandler.List)
r.Get("/usage-logs", usageLogHandler.List)
})
return r
}