feat: refactor application to use Gin framework, update routing and middleware handling
This commit is contained in:
@@ -1,58 +1,58 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// OpenAIAuth 中间件验证 OpenAI API 请求的令牌
|
||||
func OpenAIAuth(apiToken string, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func OpenAIAuth(apiToken string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 如果没有配置令牌,跳过验证
|
||||
if apiToken == "" {
|
||||
next.ServeHTTP(w, r)
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
// 获取请求头中的 Authorization
|
||||
authHeader := r.Header.Get("Authorization")
|
||||
authHeader := c.GetHeader("Authorization")
|
||||
if authHeader == "" {
|
||||
http.Error(w, "未提供授权令牌", http.StatusUnauthorized)
|
||||
c.AbortWithStatusJSON(401, gin.H{"error": "未提供授权令牌"})
|
||||
return
|
||||
}
|
||||
|
||||
// 验证格式是否为 "Bearer {token}"
|
||||
parts := strings.SplitN(authHeader, " ", 2)
|
||||
if len(parts) != 2 || parts[0] != "Bearer" {
|
||||
http.Error(w, "授权格式无效", http.StatusUnauthorized)
|
||||
c.AbortWithStatusJSON(401, gin.H{"error": "授权格式无效"})
|
||||
return
|
||||
}
|
||||
|
||||
// 验证令牌是否正确
|
||||
if parts[1] != apiToken {
|
||||
http.Error(w, "令牌无效", http.StatusUnauthorized)
|
||||
c.AbortWithStatusJSON(401, gin.H{"error": "令牌无效"})
|
||||
return
|
||||
}
|
||||
|
||||
// 令牌验证通过,继续处理请求
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// TTSAuth 是用于验证 TTS API 接口的中间件
|
||||
func TTSAuth(apiKey string, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func TTSAuth(apiKey string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 从查询参数中获取 api_key
|
||||
queryKey := r.URL.Query().Get("api_key")
|
||||
queryKey := c.Query("api_key")
|
||||
|
||||
// 如果 apiKey 配置为空字符串,表示不需要验证
|
||||
if apiKey != "" && queryKey != apiKey {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
w.Write([]byte("未授权访问: 无效的 API 密钥"))
|
||||
c.AbortWithStatusJSON(401, gin.H{"error": "未授权访问: 无效的 API 密钥"})
|
||||
return
|
||||
}
|
||||
|
||||
// 验证通过,继续处理请求
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
package middleware
|
||||
|
||||
import "net/http"
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
// CORS 处理跨域资源共享
|
||||
func CORS(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func CORS() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 设置CORS响应头
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||||
c.Header("Access-Control-Allow-Origin", "*")
|
||||
c.Header("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
||||
c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||||
|
||||
// 如果是预检请求,直接返回200
|
||||
if r.Method == http.MethodOptions {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
if c.Request.Method == "OPTIONS" {
|
||||
c.AbortWithStatus(200)
|
||||
return
|
||||
}
|
||||
|
||||
// 继续下一个处理器
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,45 +2,27 @@ package middleware
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Logger 是一个HTTP中间件,记录请求的详细信息
|
||||
func Logger(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
func Logger() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
|
||||
// 包装ResponseWriter以捕获状态码
|
||||
wrapper := &responseWriterWrapper{
|
||||
ResponseWriter: w,
|
||||
statusCode: http.StatusOK,
|
||||
}
|
||||
|
||||
// 调用下一个处理器
|
||||
next.ServeHTTP(wrapper, r)
|
||||
// 处理请求
|
||||
c.Next()
|
||||
|
||||
// 记录请求信息
|
||||
duration := time.Since(start)
|
||||
log.Printf(
|
||||
"[%s] %s %s %d %s",
|
||||
r.Method,
|
||||
r.RequestURI,
|
||||
r.RemoteAddr,
|
||||
wrapper.statusCode,
|
||||
log.Printf("[%s] %s %s %d %s",
|
||||
c.Request.Method,
|
||||
c.Request.URL.Path,
|
||||
c.ClientIP(),
|
||||
c.Writer.Status(),
|
||||
duration,
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
// responseWriterWrapper 包装http.ResponseWriter以捕获状态码
|
||||
type responseWriterWrapper struct {
|
||||
http.ResponseWriter
|
||||
statusCode int
|
||||
}
|
||||
|
||||
// WriteHeader 捕获状态码
|
||||
func (w *responseWriterWrapper) WriteHeader(statusCode int) {
|
||||
w.statusCode = statusCode
|
||||
w.ResponseWriter.WriteHeader(statusCode)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user