package router import ( "database/sql" "net/http" "gpt-manager-go/internal/handler" "gpt-manager-go/internal/middleware" "gpt-manager-go/internal/repository" "gpt-manager-go/internal/service" "gpt-manager-go/internal/static" ) // SetupRoutes 设置路由 func SetupRoutes(db *sql.DB) http.Handler { mux := http.NewServeMux() // 初始化仓储 adminRepo := repository.NewAdminRepository(db) chatgptAccountRepo := repository.NewChatGPTAccountRepository(db) invitationRepo := repository.NewInvitationRepository(db) cardKeyRepo := repository.NewCardKeyRepository(db) // 初始化服务 chatgptService := service.NewChatGPTService() // 初始化处理器 authHandler := handler.NewAuthHandler(adminRepo) accountHandler := handler.NewChatGPTAccountHandler(chatgptAccountRepo, invitationRepo, chatgptService) inviteHandler := handler.NewInviteHandler(chatgptAccountRepo, invitationRepo, cardKeyRepo, chatgptService) cardKeyHandler := handler.NewCardKeyHandler(cardKeyRepo) // 公开路由 (无需认证) mux.HandleFunc("/api/login", authHandler.Login) mux.HandleFunc("/api/invite/card", inviteHandler.InviteByCardKey) // 卡密邀请 // 需要认证的路由 protectedMux := http.NewServeMux() protectedMux.HandleFunc("/api/profile", func(w http.ResponseWriter, r *http.Request) { claims := middleware.GetUserFromContext(r.Context()) w.Header().Set("Content-Type", "application/json") w.Write([]byte(`{"success":true,"user":{"id":` + itoa(claims.UserID) + `,"username":"` + claims.Username + `"}}`)) }) // ChatGPT 账号管理接口 protectedMux.HandleFunc("/api/accounts", accountHandler.List) protectedMux.HandleFunc("/api/accounts/create", accountHandler.Create) protectedMux.HandleFunc("/api/accounts/refresh", accountHandler.Refresh) protectedMux.HandleFunc("/api/accounts/delete", accountHandler.Delete) protectedMux.HandleFunc("/api/accounts/batch/delete", accountHandler.BatchDelete) protectedMux.HandleFunc("/api/accounts/batch/refresh", accountHandler.BatchRefresh) // 邀请接口 (管理员) - GET: 列表, POST: 邀请, DELETE: 移除 protectedMux.HandleFunc("/api/invite", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodGet: inviteHandler.ListByAccount(w, r) case http.MethodPost, http.MethodDelete: inviteHandler.InviteByAdmin(w, r) default: w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusMethodNotAllowed) w.Write([]byte(`{"success":false,"message":"Method not allowed"}`)) } }) // 卡密管理接口 protectedMux.HandleFunc("/api/cardkeys", cardKeyHandler.Handle) // GET: 列表, POST: 创建 protectedMux.HandleFunc("/api/cardkeys/batch", func(w http.ResponseWriter, r *http.Request) { switch r.Method { case http.MethodPost: cardKeyHandler.BatchCreate(w, r) case http.MethodDelete: cardKeyHandler.BatchDelete(w, r) default: w.Header().Set("Content-Type", "application/json") w.WriteHeader(http.StatusMethodNotAllowed) w.Write([]byte(`{"success":false,"message":"Method not allowed"}`)) } }) protectedMux.HandleFunc("/api/cardkeys/delete", cardKeyHandler.Delete) // DELETE: 单个删除 protectedMux.HandleFunc("/api/cardkeys/toggle", cardKeyHandler.ToggleActive) // POST: 切换激活状态 // 挂载受保护的路由 mux.Handle("/api/profile", middleware.AuthMiddleware(protectedMux)) mux.Handle("/api/accounts", middleware.AuthMiddleware(protectedMux)) mux.Handle("/api/accounts/", middleware.AuthMiddleware(protectedMux)) mux.Handle("/api/invite", middleware.AuthMiddleware(protectedMux)) mux.Handle("/api/cardkeys", middleware.AuthMiddleware(protectedMux)) mux.Handle("/api/cardkeys/", middleware.AuthMiddleware(protectedMux)) // 静态文件服务(前端) mux.Handle("/", static.Handler()) // CORS 中间件包装 return corsMiddleware(mux) } // corsMiddleware CORS 中间件 func corsMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization") if r.Method == http.MethodOptions { w.WriteHeader(http.StatusOK) return } next.ServeHTTP(w, r) }) } func itoa(i int) string { if i == 0 { return "0" } var s string for i > 0 { s = string(rune('0'+i%10)) + s i /= 10 } return s }