package main import ( "context" "os" "strconv" "github.com/gin-gonic/gin" "github.com/sirupsen/logrus" ) func main() { logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true}) logrus.SetLevel(logrus.InfoLevel) if err := loadConfig("config.yaml"); err != nil { logrus.Fatalf("Failed to load config.yaml: %v", err) } logrus.Infof("Loaded config: %+v", appConfig) visitDB := NewVisitDB() if err := loadUITemplate("ui.html"); err != nil { logrus.Fatalf("Failed to load ui.html: %v", err) } // Initialize PostgreSQL repository first dsn := buildDefaultDSN() logrus.Info("Connecting to PostgreSQL with DSN: ", dsn) repo, err := NewPGChatRepository(context.Background(), dsn) if err != nil { logrus.WithError(err).Warn("PostgreSQL repository disabled (connection failed)") } else if repo == nil { logrus.Info("PostgreSQL repository not configured (no DSN)") } // defer repo.Close() // optionally enable // Initialize LLM client llmClient := NewLLMClient( os.Getenv("OPENAI_API_KEY"), os.Getenv("OPENAI_BASE_URL"), os.Getenv("OPENAI_MODEL"), repo, ) var llm LLMClientAPI = llmClient chatService := NewChatService(llm, &visitDB, repo) r := gin.Default() // Routes r.GET("/", func(c *gin.Context) { c.Status(200) if err := uiTemplate.Execute(c.Writer, nil); err != nil { logrus.Errorf("Failed to execute ui.html template: %v", err) } }) r.GET("/health", func(c *gin.Context) { c.Status(200) c.JSON(200, gin.H{"status": "ok"}) }) r.POST("/chat", chatService.HandleChat) if err := loadDBEditTemplate("ui_dbedit.html"); err != nil { logrus.Fatalf("Failed to load ui_dbedit.html: %v", err) } if err := loadAdminChatsTemplate("ui_admin_chats.html"); err != nil { logrus.Fatalf("Failed to load ui_admin_chats.html: %v", err) } r.GET("/admin", func(c *gin.Context) { c.Status(200) if err := uiDBEditTemplate.Execute(c.Writer, nil); err != nil { logrus.Errorf("Failed to execute ui_dbedit.html template: %v", err) } }) r.GET("/db.yaml", func(c *gin.Context) { c.File("db.yaml") }) // JSON: list chat interactions r.GET("/admin/chats", func(c *gin.Context) { if repo == nil { c.JSON(200, gin.H{"items": []ChatInteraction{}, "pagination": gin.H{"limit": 0, "offset": 0, "count": 0}, "warning": "repository not configured"}) return } limit := 50 if ls := c.Query("limit"); ls != "" { if v, err := strconv.Atoi(ls); err == nil { limit = v } } offset := 0 if osf := c.Query("offset"); osf != "" { if v, err := strconv.Atoi(osf); err == nil { offset = v } } items, err := repo.ListChatInteractions(c.Request.Context(), limit, offset) if err != nil { c.JSON(500, gin.H{"error": "failed to list interactions"}) return } c.JSON(200, gin.H{"items": items, "pagination": gin.H{"limit": limit, "offset": offset, "count": len(items)}}) }) // JSON: list raw LLM events for a correlation id r.GET("/admin/chats/events", func(c *gin.Context) { if repo == nil { c.JSON(200, gin.H{"items": []RawLLMEvent{}, "pagination": gin.H{"limit": 0, "offset": 0, "count": 0}, "warning": "repository not configured"}) return } corr := c.Query("correlation_id") if corr == "" { c.JSON(400, gin.H{"error": "missing correlation_id"}) return } limit := 100 if ls := c.Query("limit"); ls != "" { if v, err := strconv.Atoi(ls); err == nil { limit = v } } offset := 0 if osf := c.Query("offset"); osf != "" { if v, err := strconv.Atoi(osf); err == nil { offset = v } } events, err := repo.ListLLMRawEvents(c.Request.Context(), corr, limit, offset) if err != nil { c.JSON(500, gin.H{"error": "failed to list events"}) return } c.JSON(200, gin.H{"items": events, "pagination": gin.H{"limit": limit, "offset": offset, "count": len(events)}}) }) // HTML UI for chats & events r.GET("/admin/chats/ui", func(c *gin.Context) { c.Status(200) if err := uiAdminChatsTemplate.Execute(c.Writer, nil); err != nil { logrus.Errorf("Failed to execute ui_admin_chats.html template: %v", err) } }) r.Run(":8080") }