sp4-debug-1770477266/services/chat-svc/internal/api/routes.go
rdev-worker 5a877ca1a1
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
feat: implement mesh-interop service communication
Add auth-svc /validate endpoint for token checking
Add chat-svc with auth client and Redis task queue
Add worker-svc chat handler for task processing

Co-Authored-By: Claude Code <claude@anthropic.com>
2026-02-07 16:45:22 +00:00

69 lines
2.6 KiB
Go

// Package api provides HTTP routing and handlers for the chat-svc service.
package api
import (
"git.threesix.ai/jordan/sp4-debug-1770477266/pkg/app"
"git.threesix.ai/jordan/sp4-debug-1770477266/pkg/auth"
"git.threesix.ai/jordan/sp4-debug-1770477266/pkg/svc"
"git.threesix.ai/jordan/sp4-debug-1770477266/services/chat-svc/internal/api/handlers"
"git.threesix.ai/jordan/sp4-debug-1770477266/services/chat-svc/internal/authclient"
"git.threesix.ai/jordan/sp4-debug-1770477266/services/chat-svc/internal/config"
"git.threesix.ai/jordan/sp4-debug-1770477266/services/chat-svc/internal/service"
"git.threesix.ai/jordan/sp4-debug-1770477266/services/chat-svc/internal/taskqueue"
)
// RegisterRoutes registers all HTTP routes for the service.
// Routes are mounted under /api/chat-svc to match the ingress path routing.
func RegisterRoutes(application *app.App, exampleService *service.ExampleService, producer *taskqueue.Producer) {
logger := application.Logger()
cfg := config.Load()
// Initialize handlers with injected services
healthHandler := handlers.NewHealth(logger)
exampleHandler := handlers.NewExample(exampleService, logger)
chatHandler := handlers.NewChat(producer, 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 {
// Use remote auth-svc validation if configured, otherwise fall back to local JWT
if svc.ServiceConfigured("auth-svc") {
ac, err := authclient.New(logger)
if err != nil {
logger.Error("failed to create auth client", "error", err)
} else {
r.Use(authclient.Middleware(ac))
}
} else {
r.Use(auth.Middleware(auth.MiddlewareConfig{
Validator: auth.NewJWTValidator(auth.JWTConfig{
Secret: []byte(cfg.JWTSecret),
Issuer: "sp4-debug-1770477266",
}),
}))
}
}
r.Post("/examples", app.Wrap(exampleHandler.Create))
r.Put("/examples/{id}", app.Wrap(exampleHandler.Update))
r.Delete("/examples/{id}", app.Wrap(exampleHandler.Delete))
// Chat endpoints (require auth, push to worker queue)
r.Post("/send", app.Wrap(chatHandler.Send))
})
})
}