sp4-verify-1770325799/services/auth-svc/internal/api/routes.go
rdev-worker 36d73dd23d
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed
build: /implement-feature mesh-interop --requirements 'Chat Service must cal...
2026-02-05 21:40:58 +00:00

67 lines
2.4 KiB
Go

// Package api provides HTTP routing and handlers for the auth-svc service.
package api
import (
"git.threesix.ai/jordan/sp4-verify-1770325799/pkg/app"
"git.threesix.ai/jordan/sp4-verify-1770325799/pkg/auth"
"git.threesix.ai/jordan/sp4-verify-1770325799/services/auth-svc/internal/api/handlers"
"git.threesix.ai/jordan/sp4-verify-1770325799/services/auth-svc/internal/config"
"git.threesix.ai/jordan/sp4-verify-1770325799/services/auth-svc/internal/service"
)
// RegisterRoutes registers all HTTP routes for the service.
// Routes are mounted under /api/auth-svc to match the ingress path routing.
// This allows the monorepo to expose multiple services under a single domain:
// - https://domain/api/auth-svc/health
// - https://domain/api/auth-svc/examples
// - https://domain/api/auth-svc/validate
func RegisterRoutes(application *app.App, exampleService *service.ExampleService) {
logger := application.Logger()
cfg := config.Load()
// Initialize JWT validator for token validation endpoint
jwtValidator := auth.NewJWTValidator(auth.JWTConfig{
Secret: []byte(cfg.JWTSecret),
Issuer: "sp4-verify-1770325799",
})
// Initialize handlers with injected services
healthHandler := handlers.NewHealth(logger)
exampleHandler := handlers.NewExample(exampleService, logger)
validateHandler := handlers.NewValidate(jwtValidator, 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/auth-svc/* to this service.
application.Route("/api/auth-svc", func(r app.Router) {
r.Get("/health", healthHandler.Check)
// Token validation endpoint (for sibling services)
r.Post("/validate", app.Wrap(validateHandler.Check))
r.Get("/validate", app.Wrap(validateHandler.Check))
// Public routes (no auth required)
r.Get("/examples", app.Wrap(exampleHandler.List))
r.Get("/examples/{id}", app.Wrap(exampleHandler.Get))
// 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: "sp4-verify-1770325799",
}),
}))
}
r.Post("/examples", app.Wrap(exampleHandler.Create))
r.Put("/examples/{id}", app.Wrap(exampleHandler.Update))
r.Delete("/examples/{id}", app.Wrap(exampleHandler.Delete))
})
})
}