Add CORS to api server

This commit is contained in:
Mark Beamer Jr 2021-03-10 20:04:48 -05:00
parent 29773829af
commit 4580a95b74
No known key found for this signature in database
GPG key ID: 1C314FB89AD76973

View file

@ -17,6 +17,9 @@ import (
// ResponseHeaders are returned with each response
var ResponseHeaders map[string]string
// CorsDomains Allowed domains for CORS Policy
var CorsDomains []string
// Log allows logging of events and errors
var Log = func(*http.Request, *Response, error) {}
@ -78,6 +81,20 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
for _, d := range CorsDomains {
if d == r.Header.Get("origin") {
w.Header().Set("Access-Control-Allow-Origin", d)
vary := w.Header().Get("Vary")
if vary != "*" {
if vary != "" {
vary += ", "
}
vary += "Origin"
}
w.Header().Set("Vary", vary)
}
}
// Stop here if its a preflighted OPTIONS request
if r.Method == "OPTIONS" {
return