Changeset 0.6.0 (#36)

This commit is contained in:
2026-02-20 22:24:47 +00:00
parent 30d0c11219
commit 925b70f98c
34 changed files with 2575 additions and 637 deletions

View File

@@ -27,18 +27,24 @@ func main() {
if err := database.Migrate(); err != nil {
log.Fatalf("❌ Schema migration failed: %v", err)
}
// Bootstrap admin from env (K8s secret) — upserts on every restart
handlers.BootstrapAdmin(cfg)
}
defer database.Close()
r := gin.Default()
r.Use(middleware.CORS())
// ── Base path group ──────────────────────
// All routes live under cfg.BasePath (e.g. "/dev", "/test", or "")
base := r.Group(cfg.BasePath)
// ── EventBus + WebSocket Hub ─────────────
bus := events.NewBus()
hub := events.NewHub(bus)
// Health check (k8s probes hit this directly)
r.GET("/health", func(c *gin.Context) {
base.GET("/health", func(c *gin.Context) {
c.JSON(200, gin.H{
"status": "ok",
"version": Version,
@@ -48,13 +54,13 @@ func main() {
})
// WebSocket endpoint — auth via ?token= query param
r.GET("/ws", middleware.Auth(cfg), hub.HandleWebSocket)
base.GET("/ws", middleware.Auth(cfg), hub.HandleWebSocket)
// ── Auth routes (rate limited) ──────────────
auth := handlers.NewAuthHandler(cfg)
authLimiter := middleware.NewRateLimiter(1, 5)
api := r.Group("/api/v1")
api := base.Group("/api/v1")
{
// Health (routable through ingress)
api.GET("/health", func(c *gin.Context) {
@@ -85,23 +91,23 @@ func main() {
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)
// Channels (unified: replaces /chats)
channels := handlers.NewChannelHandler()
protected.GET("/channels", channels.ListChannels)
protected.POST("/channels", channels.CreateChannel)
protected.GET("/channels/:id", channels.GetChannel)
protected.PUT("/channels/:id", channels.UpdateChannel)
protected.DELETE("/channels/:id", channels.DeleteChannel)
// Messages
msgs := handlers.NewMessageHandler()
protected.GET("/chats/:id/messages", msgs.ListMessages)
protected.POST("/chats/:id/messages", msgs.CreateMessage)
protected.GET("/channels/:id/messages", msgs.ListMessages)
protected.POST("/channels/: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
protected.POST("/channels/:id/regenerate", msgs.Regenerate) // TODO: wire to completion handler
// API Configs
apiCfg := handlers.NewAPIConfigHandler()
@@ -123,6 +129,10 @@ func main() {
protected.POST("/profile/password", settings.ChangePassword)
protected.GET("/settings", settings.GetSettings)
protected.PUT("/settings", settings.UpdateSettings)
// Public global settings (non-admin users can read safe subset)
adm := handlers.NewAdminHandler()
protected.GET("/settings/public", adm.PublicSettings)
}
// ── Admin routes ────────────────────────
@@ -156,15 +166,21 @@ func main() {
// Model Configs
admin.GET("/models", adm.ListModelConfigs)
admin.POST("/models/fetch", adm.FetchModels)
admin.PUT("/models/bulk", adm.BulkUpdateModels)
admin.PUT("/models/:id", adm.UpdateModelConfig)
admin.DELETE("/models/:id", adm.DeleteModelConfig)
}
}
bp := cfg.BasePath
if bp == "" {
bp = "/"
}
log.Printf("🔀 Chat Switchboard API v%s starting on port %s", Version, cfg.Port)
log.Printf(" Base path: %s", bp)
log.Printf(" Schema: %s", database.SchemaVersion())
log.Printf(" Providers: %v", providers.List())
log.Printf(" EventBus: ready, WebSocket on /ws")
log.Printf(" EventBus: ready, WebSocket on %s/ws", cfg.BasePath)
if err := r.Run(":" + cfg.Port); err != nil {
log.Fatalf("Failed to start server: %v", err)
}