This commit is contained in:
dpurbosakti
2025-08-19 14:26:39 +07:00
parent 911cf6d1fb
commit a448f43b67
+31
View File
@@ -0,0 +1,31 @@
package corsmanagermw
import (
"net/http"
a "github.com/karincake/apem"
)
type CorsCfg struct {
AllowedOrigins []string `yaml:"allowedOrigins"`
AllowedMethod string `yaml:"allowedMethod"`
}
var cfg CorsCfg
func SetCors(next http.Handler) http.Handler {
a.ParseSingleCfg(&cfg)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Methods", cfg.AllowedMethod)
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}