package middleware import ( "net/http" ) // CORSMiddleware 處理 CORS 請求 func CORSMiddleware(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { // 設置 CORS 標頭 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") w.Header().Set("Access-Control-Max-Age", "3600") // 處理預檢請求 if r.Method == http.MethodOptions { w.WriteHeader(http.StatusNoContent) return } next(w, r) } }