| | package main
|
| |
|
| | import (
|
| | "fmt"
|
| | "log"
|
| | "opus-api/internal/handler"
|
| | "opus-api/internal/logger"
|
| | "opus-api/internal/middleware"
|
| | "opus-api/internal/model"
|
| | "opus-api/internal/service"
|
| | "opus-api/internal/tokenizer"
|
| | "opus-api/internal/types"
|
| | "os"
|
| |
|
| | "github.com/gin-gonic/gin"
|
| | "github.com/joho/godotenv"
|
| | )
|
| |
|
| | func main() {
|
| |
|
| | if err := godotenv.Load(); err != nil {
|
| | log.Printf("[INFO] No .env file found or error loading it: %v", err)
|
| | } else {
|
| | log.Printf("[INFO] .env file loaded successfully")
|
| | }
|
| |
|
| |
|
| | if err := os.MkdirAll(types.LogDir, 0755); err != nil {
|
| | log.Fatalf("Failed to create logs directory: %v", err)
|
| | }
|
| |
|
| |
|
| | if types.DebugMode {
|
| | logger.CleanupOldLogs()
|
| | }
|
| |
|
| |
|
| | if err := tokenizer.Init(); err != nil {
|
| | log.Printf("[WARN] Failed to initialize tokenizer: %v (will use fallback)", err)
|
| | }
|
| |
|
| |
|
| | if err := model.InitDB(); err != nil {
|
| | log.Printf("[WARN] Failed to initialize database: %v (running without database)", err)
|
| | } else {
|
| |
|
| | if err := model.CreateDefaultAdmin(model.DB); err != nil {
|
| | log.Printf("[WARN] Failed to create default admin: %v", err)
|
| | }
|
| | }
|
| |
|
| |
|
| | var authService *service.AuthService
|
| | var cookieService *service.CookieService
|
| | var cookieValidator *service.CookieValidator
|
| | var cookieRotator *service.CookieRotator
|
| |
|
| | if model.DB != nil {
|
| | authService = service.NewAuthService(model.DB)
|
| | cookieService = service.NewCookieService(model.DB)
|
| | cookieValidator = service.NewCookieValidator(cookieService)
|
| | cookieRotator = service.NewCookieRotator(cookieService, service.StrategyRoundRobin)
|
| |
|
| |
|
| | types.CookieRotatorInstance = cookieRotator
|
| | }
|
| |
|
| |
|
| | gin.SetMode(gin.ReleaseMode)
|
| |
|
| |
|
| | router := gin.Default()
|
| |
|
| |
|
| | router.Static("/static", "./web/static")
|
| |
|
| |
|
| | router.GET("/", func(c *gin.Context) {
|
| | c.Redirect(302, "/static/index.html")
|
| | })
|
| |
|
| |
|
| | router.GET("/dashboard", func(c *gin.Context) {
|
| | c.Redirect(302, "/static/dashboard.html")
|
| | })
|
| |
|
| |
|
| | router.POST("/v1/messages", handler.HandleMessages)
|
| | router.GET("/health", handler.HandleHealth)
|
| |
|
| |
|
| | if authService != nil {
|
| | authHandler := handler.NewAuthHandler(authService)
|
| | router.POST("/api/auth/login", authHandler.Login)
|
| | router.POST("/api/auth/logout", authHandler.Logout)
|
| |
|
| |
|
| | authGroup := router.Group("/api")
|
| | authGroup.Use(middleware.AuthMiddleware(authService))
|
| | {
|
| | authGroup.GET("/auth/me", authHandler.Me)
|
| | authGroup.PUT("/auth/password", authHandler.ChangePassword)
|
| |
|
| |
|
| | if cookieService != nil && cookieValidator != nil {
|
| | cookieHandler := handler.NewCookieHandler(cookieService, cookieValidator)
|
| | authGroup.GET("/cookies", cookieHandler.ListCookies)
|
| | authGroup.GET("/cookies/stats", cookieHandler.GetStats)
|
| | authGroup.POST("/cookies", cookieHandler.CreateCookie)
|
| | authGroup.GET("/cookies/:id", cookieHandler.GetCookie)
|
| | authGroup.PUT("/cookies/:id", cookieHandler.UpdateCookie)
|
| | authGroup.DELETE("/cookies/:id", cookieHandler.DeleteCookie)
|
| | authGroup.POST("/cookies/:id/validate", cookieHandler.ValidateCookie)
|
| | authGroup.POST("/cookies/validate/all", cookieHandler.ValidateAllCookies)
|
| | }
|
| | }
|
| | }
|
| |
|
| |
|
| |
|
| | port := 7860
|
| | if envPort := os.Getenv("PORT"); envPort != "" {
|
| | fmt.Sscanf(envPort, "%d", &port)
|
| | }
|
| | addr := fmt.Sprintf("0.0.0.0:%d", port)
|
| | log.Printf("Server running on http://0.0.0.0:%d", port)
|
| | log.Printf("Debug mode: %v", types.DebugMode)
|
| | log.Printf("Log directory: %s", types.LogDir)
|
| | log.Printf("Database connected: %v", model.DB != nil)
|
| |
|
| | if err := router.Run(addr); err != nil {
|
| | log.Fatalf("Failed to start server: %v", err)
|
| | }
|
| | }
|
| |
|