50 lines
1.9 KiB
Go
50 lines
1.9 KiB
Go
// Package api provides HTTP routing and handlers for the preferences-api service.
|
|
package api
|
|
|
|
import (
|
|
"git.threesix.ai/jordan/slack5-1770574304/pkg/app"
|
|
"git.threesix.ai/jordan/slack5-1770574304/pkg/auth"
|
|
"git.threesix.ai/jordan/slack5-1770574304/services/preferences-api/internal/api/handlers"
|
|
"git.threesix.ai/jordan/slack5-1770574304/services/preferences-api/internal/config"
|
|
"git.threesix.ai/jordan/slack5-1770574304/services/preferences-api/internal/service"
|
|
)
|
|
|
|
// RegisterRoutes registers all HTTP routes for the service.
|
|
// Routes are mounted under /api/preferences-api to match the ingress path routing.
|
|
// This allows the monorepo to expose multiple services under a single domain:
|
|
// - https://domain/api/preferences-api/health
|
|
// - https://domain/api/preferences-api/preferences/{user_id}
|
|
func RegisterRoutes(application *app.App, preferenceService *service.PreferenceService) {
|
|
logger := application.Logger()
|
|
cfg := config.Load()
|
|
|
|
// Initialize handlers with injected services
|
|
healthHandler := handlers.NewHealth(logger)
|
|
preferenceHandler := handlers.NewPreference(preferenceService, logger)
|
|
|
|
// Build and mount OpenAPI spec
|
|
spec := NewServiceSpec()
|
|
application.EnableDocs(spec)
|
|
|
|
// Register API routes under /api/{service-name} to match ingress path routing.
|
|
// The ingress routes /api/preferences-api/* to this service.
|
|
application.Route("/api/preferences-api", func(r app.Router) {
|
|
r.Get("/health", healthHandler.Check)
|
|
|
|
// Protected routes (auth required when enabled)
|
|
r.Group(func(r app.Router) {
|
|
if cfg.AuthEnabled {
|
|
r.Use(auth.Middleware(auth.MiddlewareConfig{
|
|
Validator: auth.NewJWTValidator(auth.JWTConfig{
|
|
Secret: []byte(cfg.JWTSecret),
|
|
Issuer: "slack5-1770574304",
|
|
}),
|
|
}))
|
|
}
|
|
|
|
r.Get("/preferences/{user_id}", app.Wrap(preferenceHandler.Get))
|
|
r.Put("/preferences/{user_id}", app.Wrap(preferenceHandler.Update))
|
|
})
|
|
})
|
|
}
|