feat: add permissions on user create, CORS middleware, cors server playground.

This commit is contained in:
2026-04-24 10:55:40 +02:00
parent d629bd52eb
commit 2fd3a1d57b
8 changed files with 179 additions and 3 deletions
+27
View File
@@ -244,3 +244,30 @@ func (app *application) requirePermission(code string, next http.HandlerFunc) ht
// Wrap this with the requireActivatedUser() middleware before returning it.
return app.requireActivatedUser(fn)
}
func (app *application) enableCORS(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Add the "Vary: Origin" header.
w.Header().Add("Vary", "Origin")
// Get the value of the request's Origin header.
origin := r.Header.Get("Origin")
// Only run this if there's an Origin request header present AND at least one
// trusted origin is configured.
if origin != "" && len(app.config.cors.trustedOrigins) != 0 {
// Loop through the list of trusted origins, checking to see if the request
// origin exactly matches one of them.
for i := range app.config.cors.trustedOrigins {
if origin == app.config.cors.trustedOrigins[i] {
// If there is a match, then set a "Access-Control-Allow-Origin
// response header with the request origin as the value.
w.Header().Set("Access-Control-Allow-Origin", origin)
}
}
}
// Call the next handler in the chain.
next.ServeHTTP(w, r)
})
}