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

95 lines
3.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/middleware"
)
func main() {
// Load configuration from environment
cfg := config.Load()
// Connect to database (optional — runs without it in unmanaged mode)
if err := database.Connect(cfg); err != nil {
log.Printf("⚠ Database unavailable: %v", err)
log.Println(" Running in unmanaged mode (no persistence)")
}
defer database.Close()
// Initialize router
r := gin.Default()
r.Use(middleware.CORS())
// Health check
r.GET("/health", func(c *gin.Context) {
status := gin.H{
"status": "ok",
"database": database.IsConnected(),
}
c.JSON(200, status)
})
// API routes
api := r.Group("/api/v1")
{
// Public auth routes
api.POST("/auth/register", handleRegister)
api.POST("/auth/login", handleLogin)
// 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 (proxy to configured API)
protected.GET("/models", handleListModels)
}
}
log.Printf("🔀 Chat Switchboard API starting on port %s", cfg.Port)
if err := r.Run(":" + cfg.Port); err != nil {
log.Fatalf("Failed to start server: %v", err)
}
}
// Placeholder handlers — will be moved to handlers/ package
func handleRegister(c *gin.Context) { c.JSON(501, gin.H{"error": "not implemented"}) }
func handleLogin(c *gin.Context) { c.JSON(501, gin.H{"error": "not implemented"}) }
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"}) }