55 lines
1.9 KiB
Go
55 lines
1.9 KiB
Go
// Package api provides HTTP routing and handlers for the chat-svc service.
|
|
package api
|
|
|
|
import (
|
|
"git.threesix.ai/jordan/sp4-rwx-test/pkg/app"
|
|
"git.threesix.ai/jordan/sp4-rwx-test/pkg/auth"
|
|
"git.threesix.ai/jordan/sp4-rwx-test/services/chat-svc/internal/api/handlers"
|
|
"git.threesix.ai/jordan/sp4-rwx-test/services/chat-svc/internal/config"
|
|
"git.threesix.ai/jordan/sp4-rwx-test/services/chat-svc/internal/service"
|
|
)
|
|
|
|
// RegisterRoutes registers all HTTP routes for the service.
|
|
// Routes are mounted under /api/chat-svc to match the ingress path routing.
|
|
// This allows the monorepo to expose multiple services under a single domain:
|
|
// - https://domain/api/chat-svc/health
|
|
// - https://domain/api/chat-svc/examples
|
|
func RegisterRoutes(application *app.App, exampleService *service.ExampleService) {
|
|
logger := application.Logger()
|
|
cfg := config.Load()
|
|
|
|
// Initialize handlers with injected services
|
|
healthHandler := handlers.NewHealth(logger)
|
|
exampleHandler := handlers.NewExample(exampleService, 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/chat-svc/* to this service.
|
|
application.Route("/api/chat-svc", func(r app.Router) {
|
|
r.Get("/health", healthHandler.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-rwx-test",
|
|
}),
|
|
}))
|
|
}
|
|
|
|
r.Post("/examples", app.Wrap(exampleHandler.Create))
|
|
r.Put("/examples/{id}", app.Wrap(exampleHandler.Update))
|
|
r.Delete("/examples/{id}", app.Wrap(exampleHandler.Delete))
|
|
})
|
|
})
|
|
}
|