This repository has been archived on 2026-04-03. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
core/server/main.go
2026-02-15 23:41:48 +00:00

103 lines
3.5 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"
)
func main() {
cfg := config.Load()
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
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) // 1 req/s, burst of 5
api := r.Group("/api/v1")
{
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
protected.GET("/chats", handleListChats)
protected.POST("/chats", handleCreateChat)
protected.GET("/chats/:id", handleGetChat)
protected.PUT("/chats/:id", handleUpdateChat)
protected.DELETE("/chats/:id", handleDeleteChat)
// Messages
protected.GET("/chats/:id/messages", handleGetMessages)
protected.POST("/chats/:id/messages", handleCreateMessage)
// Settings
protected.GET("/settings", handleGetSettings)
protected.PUT("/settings", handleUpdateSettings)
// API Configs
protected.GET("/api-configs", handleListAPIConfigs)
protected.POST("/api-configs", handleCreateAPIConfig)
protected.DELETE("/api-configs/:id", handleDeleteAPIConfig)
// Models
protected.GET("/models", handleListModels)
}
}
log.Printf("🔀 Chat Switchboard API v%s starting on port %s", Version, cfg.Port)
if err := r.Run(":" + cfg.Port); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}
// Placeholder handlers — ticket #9
func handleListChats(c *gin.Context) { c.JSON(501, gin.H{"error": "not implemented"}) }
func handleCreateChat(c *gin.Context) { c.JSON(501, gin.H{"error": "not implemented"}) }
func handleGetChat(c *gin.Context) { c.JSON(501, gin.H{"error": "not implemented"}) }
func handleUpdateChat(c *gin.Context) { c.JSON(501, gin.H{"error": "not implemented"}) }
func handleDeleteChat(c *gin.Context) { c.JSON(501, gin.H{"error": "not implemented"}) }
func handleGetMessages(c *gin.Context) { c.JSON(501, gin.H{"error": "not implemented"}) }
func handleCreateMessage(c *gin.Context) { c.JSON(501, gin.H{"error": "not implemented"}) }
func handleGetSettings(c *gin.Context) { c.JSON(501, gin.H{"error": "not implemented"}) }
func handleUpdateSettings(c *gin.Context) { c.JSON(501, gin.H{"error": "not implemented"}) }
func handleListAPIConfigs(c *gin.Context) { c.JSON(501, gin.H{"error": "not implemented"}) }
func handleCreateAPIConfig(c *gin.Context) {
c.JSON(501, gin.H{"error": "not implemented"})
}
func handleDeleteAPIConfig(c *gin.Context) {
c.JSON(501, gin.H{"error": "not implemented"})
}
func handleListModels(c *gin.Context) { c.JSON(501, gin.H{"error": "not implemented"}) }