CUB-136: add SSE endpoint in Go backend
All checks were successful
Dev Build / build-test (pull_request) Successful in 2m9s
All checks were successful
Dev Build / build-test (pull_request) Successful in 2m9s
This commit is contained in:
@@ -8,13 +8,14 @@ import (
|
||||
"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/CubeCraft-Creations/Extrudex/backend/internal/sse"
|
||||
"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 {
|
||||
func New(cfg *config.Config, dbPool *pgxpool.Pool, sseBC *sse.Broadcaster) chi.Router {
|
||||
r := chi.NewRouter()
|
||||
|
||||
// Middleware
|
||||
@@ -22,7 +23,7 @@ func New(cfg *config.Config, dbPool *pgxpool.Pool) chi.Router {
|
||||
r.Use(middleware.RealIP)
|
||||
r.Use(middleware.Logger)
|
||||
r.Use(middleware.Recoverer)
|
||||
r.Use(middleware.Timeout(60 * time.Second))
|
||||
// Timeout middleware is applied per-route below to exclude SSE
|
||||
|
||||
// CORS
|
||||
r.Use(func(next http.Handler) http.Handler {
|
||||
@@ -38,9 +39,9 @@ func New(cfg *config.Config, dbPool *pgxpool.Pool) chi.Router {
|
||||
})
|
||||
})
|
||||
|
||||
// Health check
|
||||
// Health check (with timeout)
|
||||
healthHandler := handlers.NewHealthHandler(dbPool)
|
||||
r.Get("/health", healthHandler.ServeHTTP)
|
||||
r.With(middleware.Timeout(30 * time.Second)).Get("/health", healthHandler.ServeHTTP)
|
||||
|
||||
// ── Repositories ──────────────────────────────────────────────────────
|
||||
materialRepo := repositories.NewMaterialRepository(dbPool)
|
||||
@@ -61,8 +62,9 @@ func New(cfg *config.Config, dbPool *pgxpool.Pool) chi.Router {
|
||||
printJobHandler := handlers.NewPrintJobHandler(printJobService)
|
||||
usageLogHandler := handlers.NewUsageLogHandler(usageLogRepo)
|
||||
|
||||
// ── API Routes ────────────────────────────────────────────────────────
|
||||
// ── API Routes (with timeout) ─────────────────────────────────────────
|
||||
r.Route("/api", func(r chi.Router) {
|
||||
r.Use(middleware.Timeout(60 * time.Second))
|
||||
r.Get("/materials", materialHandler.List)
|
||||
|
||||
r.Route("/filaments", func(r chi.Router) {
|
||||
@@ -78,6 +80,10 @@ func New(cfg *config.Config, dbPool *pgxpool.Pool) chi.Router {
|
||||
r.Get("/printers", printerHandler.List)
|
||||
r.Get("/print-jobs", printJobHandler.List)
|
||||
r.Get("/usage-logs", usageLogHandler.List)
|
||||
|
||||
// SSE Events stream
|
||||
sseHandler := sse.NewHandler(sseBC)
|
||||
r.Get("/events", sseHandler.ServeHTTP)
|
||||
})
|
||||
|
||||
return r
|
||||
|
||||
Reference in New Issue
Block a user