refactor: 重构项目架构,拆分代码到模块化目录
将单文件tts_server.go重构为模块化项目结构,拆分出common、dto、middleware、router、controller、service、adapter、setting等目录,优化代码组织提升可维护性
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var validAPIKeys []string
|
||||
|
||||
func InitAPIKeys() {
|
||||
apiKey := os.Getenv("OPENAI_TTS_API_KEY")
|
||||
if apiKey != "" {
|
||||
validAPIKeys = strings.Split(apiKey, ",")
|
||||
for i, k := range validAPIKeys {
|
||||
validAPIKeys[i] = strings.TrimSpace(k)
|
||||
}
|
||||
log.Printf("已配置 %d 个有效的API密钥", len(validAPIKeys))
|
||||
} else {
|
||||
log.Println("警告: OPENAI_TTS_API_KEY 环境变量未设置,将允许所有请求")
|
||||
}
|
||||
}
|
||||
|
||||
func ValidateAPIKey(r *http.Request) bool {
|
||||
if len(validAPIKeys) == 0 {
|
||||
return true
|
||||
}
|
||||
|
||||
authHeader := r.Header.Get("Authorization")
|
||||
if authHeader == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(authHeader, "Bearer ") {
|
||||
return false
|
||||
}
|
||||
|
||||
token := strings.TrimPrefix(authHeader, "Bearer ")
|
||||
for _, validKey := range validAPIKeys {
|
||||
if token == validKey {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func SendJSONError(w http.ResponseWriter, statusCode int, message string, errType string, code string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(statusCode)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"error": map[string]interface{}{
|
||||
"message": message,
|
||||
"type": errType,
|
||||
"code": code,
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user