chore: add a new cors response header

This commit is contained in:
PuerNya 2023-12-27 16:20:40 +08:00 committed by 汐殇
parent fb1c0aa387
commit 2e87c6f4da

View File

@ -63,6 +63,7 @@ func Start(addr string, tlsAddr string, secret string,
AllowedHeaders: []string{"Content-Type", "Authorization"},
MaxAge: 300,
})
r.Use(setPrivateNetworkAccess)
r.Use(corsM.Handler)
if isDebug {
r.Mount("/debug", func() http.Handler {
@ -149,6 +150,15 @@ func Start(addr string, tlsAddr string, secret string,
}
func setPrivateNetworkAccess(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodOptions && r.Header.Get("Access-Control-Request-Method") != "" {
w.Header().Add("Access-Control-Allow-Private-Network", "true")
}
next.ServeHTTP(w, r)
})
}
func safeEuqal(a, b string) bool {
aBuf := utils.ImmutableBytesFromString(a)
bBuf := utils.ImmutableBytesFromString(b)