package cors import ( "net/http" ) func NotAllowedHandler() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodOptions { w.WriteHeader(http.StatusNoContent) } else { w.WriteHeader(http.StatusNotFound) } }) } func Middleware(fn func(w http.Header), origins ...string) func(http.HandlerFunc) http.HandlerFunc { return func(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { header := w.Header() header.Set("Access-Control-Allow-Origin", "*") header.Set("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token") header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS") header.Set("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type") header.Set("Access-Control-Allow-Credentials", "true") if fn != nil { fn(header) } if r.Method == http.MethodOptions { w.WriteHeader(http.StatusNoContent) } else { next(w, r) } } } }