This commit is contained in:
lbenedar
2026-03-24 14:20:29 +03:00
parent 4ff2f16110
commit 974e7f151f
4 changed files with 74 additions and 1 deletions

View File

@@ -170,3 +170,22 @@ func (app *application) requirePermissions(code string, next http.HandlerFunc) h
}
return app.requireActivatedUser(fn)
}
func (app *application) enableCORS(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Vary", "Origin")
origin := r.Header.Get("Origin")
if origin != "" {
for i := range app.config.cors.trustedOrigins {
if origin == app.config.cors.trustedOrigins[i] {
w.Header().Set("Access-Control-Allow-Origin", origin)
break
}
}
}
next.ServeHTTP(w, r)
})
}