feat: 初始化 ChatGPT Team 管理后端项目

- 添加用户认证模块 (JWT + 密码管理)
- 添加 ChatGPT 账户管理功能
- 添加卡密管理功能 (创建、批量生成、查询)
- 添加邀请功能
- 配置数据库迁移和路由系统
This commit is contained in:
sar
2026-01-13 14:42:56 +08:00
commit 42c423bd32
29 changed files with 2969 additions and 0 deletions

95
internal/router/router.go Normal file
View File

@@ -0,0 +1,95 @@
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"
)
// 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, 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)
// 邀请接口 (管理员) - POST: 邀请, DELETE: 移除
protectedMux.HandleFunc("/api/invite", inviteHandler.InviteByAdmin)
// 卡密管理接口
protectedMux.HandleFunc("/api/cardkeys", cardKeyHandler.Handle) // GET: 列表, POST: 创建
protectedMux.HandleFunc("/api/cardkeys/batch", cardKeyHandler.BatchCreate) // 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))
// 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
}