feat: 初次启动

This commit is contained in:
Blizzard
2026-04-27 21:23:13 +08:00
parent e515f6a287
commit bb8ad4d515
148 changed files with 8602 additions and 5678 deletions
+55
View File
@@ -0,0 +1,55 @@
package middleware
import (
"net/http"
"strings"
)
// CorsMiddleware 跨域中间件
type CorsMiddleware struct {
allowOrigins []string
allowMethods []string
allowHeaders []string
}
func NewCorsMiddleware(origins, methods, headers []string) *CorsMiddleware {
return &CorsMiddleware{
allowOrigins: origins,
allowMethods: methods,
allowHeaders: headers,
}
}
func (m *CorsMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
origin := r.Header.Get("Origin")
if origin == "" {
next(w, r)
return
}
allowed := false
for _, o := range m.allowOrigins {
if o == "*" || o == origin {
allowed = true
break
}
}
if allowed {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Access-Control-Allow-Methods", strings.Join(m.allowMethods, ", "))
w.Header().Set("Access-Control-Allow-Headers", strings.Join(m.allowHeaders, ", "))
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Max-Age", "3600")
}
// 预检请求直接返回
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
}
next(w, r)
}
}