feat: Implement the main entry point for the Codex Pool backend HTTP API server, including core routing and initialization.
This commit is contained in:
@@ -104,7 +104,7 @@ func startServer(cfg *config.Config) {
|
|||||||
mux.HandleFunc("/api/db/owners", api.CORS(handleGetOwners))
|
mux.HandleFunc("/api/db/owners", api.CORS(handleGetOwners))
|
||||||
mux.HandleFunc("/api/db/owners/stats", api.CORS(handleGetOwnerStats))
|
mux.HandleFunc("/api/db/owners/stats", api.CORS(handleGetOwnerStats))
|
||||||
mux.HandleFunc("/api/db/owners/clear", api.CORS(handleClearOwners))
|
mux.HandleFunc("/api/db/owners/clear", api.CORS(handleClearOwners))
|
||||||
mux.HandleFunc("/api/upload/validate", api.CORS(handleUploadValidate))
|
mux.HandleFunc("/api/upload/validate", api.CORS(api.HandleUploadValidate))
|
||||||
|
|
||||||
// 注册测试 API
|
// 注册测试 API
|
||||||
mux.HandleFunc("/api/register/test", api.CORS(handleRegisterTest))
|
mux.HandleFunc("/api/register/test", api.CORS(handleRegisterTest))
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@@ -8,7 +8,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"codex-pool/internal/api"
|
|
||||||
"codex-pool/internal/database"
|
"codex-pool/internal/database"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -26,19 +25,20 @@ type accountRecord struct {
|
|||||||
AccountID string `json:"account_id"`
|
AccountID string `json:"account_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleUploadValidate(w http.ResponseWriter, r *http.Request) {
|
// HandleUploadValidate 处理上传验证请求
|
||||||
|
func HandleUploadValidate(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
api.Error(w, http.StatusMethodNotAllowed, "仅支持 POST")
|
Error(w, http.StatusMethodNotAllowed, "仅支持 POST")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if database.Instance == nil {
|
if database.Instance == nil {
|
||||||
api.Error(w, http.StatusInternalServerError, "数据库未初始化")
|
Error(w, http.StatusInternalServerError, "数据库未初始化")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 10<<20))
|
body, err := io.ReadAll(http.MaxBytesReader(w, r.Body, 10<<20))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.Error(w, http.StatusBadRequest, "读取请求失败")
|
Error(w, http.StatusBadRequest, "读取请求失败")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,12 +55,12 @@ func handleUploadValidate(w http.ResponseWriter, r *http.Request) {
|
|||||||
case strings.TrimSpace(req.Content) != "":
|
case strings.TrimSpace(req.Content) != "":
|
||||||
parsed, parseErr := parseAccountsFlexible(req.Content)
|
parsed, parseErr := parseAccountsFlexible(req.Content)
|
||||||
if parseErr != nil {
|
if parseErr != nil {
|
||||||
api.Error(w, http.StatusBadRequest, parseErr.Error())
|
Error(w, http.StatusBadRequest, parseErr.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
records = parsed
|
records = parsed
|
||||||
default:
|
default:
|
||||||
api.Error(w, http.StatusBadRequest, "未提供账号内容")
|
Error(w, http.StatusBadRequest, "未提供账号内容")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,24 +68,24 @@ func handleUploadValidate(w http.ResponseWriter, r *http.Request) {
|
|||||||
for i, rec := range records {
|
for i, rec := range records {
|
||||||
owner, err := normalizeOwner(rec, i+1)
|
owner, err := normalizeOwner(rec, i+1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.Error(w, http.StatusBadRequest, err.Error())
|
Error(w, http.StatusBadRequest, err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
owners = append(owners, owner)
|
owners = append(owners, owner)
|
||||||
}
|
}
|
||||||
if len(owners) == 0 {
|
if len(owners) == 0 {
|
||||||
api.Error(w, http.StatusBadRequest, "未解析到有效账号")
|
Error(w, http.StatusBadRequest, "未解析到有效账号")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
inserted, err := database.Instance.AddTeamOwners(owners)
|
inserted, err := database.Instance.AddTeamOwners(owners)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
api.Error(w, http.StatusInternalServerError, fmt.Sprintf("写入数据库失败: %v", err))
|
Error(w, http.StatusInternalServerError, fmt.Sprintf("写入数据库失败: %v", err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
stats := database.Instance.GetOwnerStats()
|
stats := database.Instance.GetOwnerStats()
|
||||||
api.Success(w, map[string]interface{}{
|
Success(w, map[string]interface{}{
|
||||||
"imported": inserted,
|
"imported": inserted,
|
||||||
"total": len(owners),
|
"total": len(owners),
|
||||||
"stats": stats,
|
"stats": stats,
|
||||||
Reference in New Issue
Block a user