147 lines
4.4 KiB
Go
147 lines
4.4 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"git.gobha.me/xcaliber/chat-switchboard/config"
|
|
"git.gobha.me/xcaliber/chat-switchboard/database"
|
|
"git.gobha.me/xcaliber/chat-switchboard/handlers"
|
|
"git.gobha.me/xcaliber/chat-switchboard/middleware"
|
|
"git.gobha.me/xcaliber/chat-switchboard/providers"
|
|
)
|
|
|
|
func main() {
|
|
cfg := config.Load()
|
|
|
|
// Register LLM providers
|
|
providers.Init()
|
|
|
|
if err := database.Connect(cfg); err != nil {
|
|
log.Printf("⚠ Database unavailable: %v", err)
|
|
log.Println(" Running in unmanaged mode (no persistence)")
|
|
}
|
|
defer database.Close()
|
|
|
|
r := gin.Default()
|
|
r.Use(middleware.CORS())
|
|
|
|
// Health check (k8s probes hit this directly)
|
|
r.GET("/health", func(c *gin.Context) {
|
|
c.JSON(200, gin.H{
|
|
"status": "ok",
|
|
"version": Version,
|
|
"database": database.IsConnected(),
|
|
})
|
|
})
|
|
|
|
// ── Auth routes (rate limited) ──────────────
|
|
auth := handlers.NewAuthHandler(cfg)
|
|
authLimiter := middleware.NewRateLimiter(1, 5)
|
|
|
|
api := r.Group("/api/v1")
|
|
{
|
|
// Health (routable through ingress)
|
|
api.GET("/health", func(c *gin.Context) {
|
|
info := gin.H{
|
|
"status": "ok",
|
|
"version": Version,
|
|
"database": database.IsConnected(),
|
|
"providers": providers.List(),
|
|
}
|
|
// Include registration status for frontend
|
|
if database.IsConnected() {
|
|
info["registration_enabled"] = isRegistrationOpen()
|
|
}
|
|
c.JSON(200, info)
|
|
})
|
|
|
|
authGroup := api.Group("/auth")
|
|
authGroup.Use(authLimiter.Limit())
|
|
{
|
|
authGroup.POST("/register", auth.Register)
|
|
authGroup.POST("/login", auth.Login)
|
|
authGroup.POST("/refresh", auth.Refresh)
|
|
authGroup.POST("/logout", auth.Logout)
|
|
}
|
|
|
|
// ── Protected routes ────────────────────
|
|
protected := api.Group("")
|
|
protected.Use(middleware.Auth(cfg))
|
|
{
|
|
// Chats
|
|
chats := handlers.NewChatHandler()
|
|
protected.GET("/chats", chats.ListChats)
|
|
protected.POST("/chats", chats.CreateChat)
|
|
protected.GET("/chats/:id", chats.GetChat)
|
|
protected.PUT("/chats/:id", chats.UpdateChat)
|
|
protected.DELETE("/chats/:id", chats.DeleteChat)
|
|
|
|
// Messages
|
|
msgs := handlers.NewMessageHandler()
|
|
protected.GET("/chats/:id/messages", msgs.ListMessages)
|
|
protected.POST("/chats/:id/messages", msgs.CreateMessage)
|
|
|
|
// ── Chat Engine (#8) ────────────────
|
|
comp := handlers.NewCompletionHandler()
|
|
protected.POST("/chat/completions", comp.Complete)
|
|
protected.POST("/chats/:id/regenerate", msgs.Regenerate) // TODO: wire to completion handler
|
|
|
|
// API Configs
|
|
apiCfg := handlers.NewAPIConfigHandler()
|
|
protected.GET("/api-configs", apiCfg.ListConfigs)
|
|
protected.POST("/api-configs", apiCfg.CreateConfig)
|
|
protected.GET("/api-configs/:id", apiCfg.GetConfig)
|
|
protected.PUT("/api-configs/:id", apiCfg.UpdateConfig)
|
|
protected.DELETE("/api-configs/:id", apiCfg.DeleteConfig)
|
|
|
|
// Models (per-config and aggregate)
|
|
protected.GET("/api-configs/:id/models", apiCfg.ListModels)
|
|
protected.GET("/models", apiCfg.ListAllModels)
|
|
|
|
// User Settings & Profile
|
|
settings := handlers.NewSettingsHandler()
|
|
protected.GET("/profile", settings.GetProfile)
|
|
protected.PUT("/profile", settings.UpdateProfile)
|
|
protected.POST("/profile/password", settings.ChangePassword)
|
|
protected.GET("/settings", settings.GetSettings)
|
|
protected.PUT("/settings", settings.UpdateSettings)
|
|
}
|
|
|
|
// ── Admin routes ────────────────────────
|
|
admin := api.Group("/admin")
|
|
admin.Use(middleware.Auth(cfg))
|
|
admin.Use(middleware.RequireAdmin())
|
|
{
|
|
adm := handlers.NewAdminHandler()
|
|
|
|
// User management
|
|
admin.GET("/users", adm.ListUsers)
|
|
admin.POST("/users", adm.CreateUser)
|
|
admin.PUT("/users/:id/role", adm.UpdateUserRole)
|
|
admin.PUT("/users/:id/active", adm.ToggleUserActive)
|
|
admin.POST("/users/:id/reset-password", adm.ResetPassword)
|
|
admin.DELETE("/users/:id", adm.DeleteUser)
|
|
|
|
// Global settings
|
|
admin.GET("/settings", adm.ListGlobalSettings)
|
|
admin.GET("/settings/:key", adm.GetGlobalSetting)
|
|
admin.PUT("/settings/:key", adm.UpdateGlobalSetting)
|
|
|
|
// Stats
|
|
admin.GET("/stats", adm.GetStats)
|
|
}
|
|
}
|
|
|
|
log.Printf("🔀 Chat Switchboard API v%s starting on port %s", Version, cfg.Port)
|
|
log.Printf(" Providers: %v", providers.List())
|
|
if err := r.Run(":" + cfg.Port); err != nil {
|
|
log.Fatalf("Failed to start server: %v", err)
|
|
}
|
|
}
|
|
|
|
func isRegistrationOpen() bool {
|
|
return handlers.IsRegistrationEnabled()
|
|
}
|