Files
api-golang/internal/routes/v1/v1.go
Flavio Fois 84521d8d59 refactor routing into a modular and versioned structure
Move inline route definitions from main.go into a dedicated internal/routes package. This organization introduces support for API versioning (v1) and separates endpoint logic into specialized modules for administration and bug reporting.
2026-03-23 09:18:16 +01:00

35 lines
798 B
Go

package v1
import (
"net/http"
"emly-api-go/internal/handlers"
"github.com/go-chi/chi/v5"
"github.com/jmoiron/sqlx"
)
// NewRouter returns a chi.Router with all /v1 routes mounted.
// Add new API versions by creating an analogous package (e.g. v2) and
// mounting it alongside this one in internal/routes/routes.go.
func NewRouter(db *sqlx.DB) http.Handler {
r := chi.NewRouter()
r.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Server", "emly-api-go")
w.Header().Set("X-Powered-By", "Pure Protogen sillyness :3")
next.ServeHTTP(w, r)
})
})
r.Get("/health", handlers.Health(db))
r.Route("/api", func(r chi.Router) {
registerAdmin(r, db)
registerBugReports(r, db)
})
return r
}