diff options
Diffstat (limited to 'cors.go')
-rw-r--r-- | cors.go | 30 |
1 files changed, 30 insertions, 0 deletions
@@ -0,0 +1,30 @@ +package main + +import ( + "net/http" +) + +type CorsAllowAllOrigins struct { + AndThen http.Handler +} + +func (c CorsAllowAllOrigins) ServeHTTP(w http.ResponseWriter, r *http.Request) { + w.Header().Add("Access-Control-Allow-Origin", "*") + w.Header().Add("Access-Control-Allow-Methods", "*") + w.Header().Add("Access-Control-Allow-Headers", "*") + c.AndThen.ServeHTTP(w, r) +} + +type OptionsNoError struct { + Error ErrorHandler +} + +func (c OptionsNoError) WithError(err error) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method == "OPTIONS" { + w.WriteHeader(200) + } else { + c.Error.WithError(err).ServeHTTP(w, r) + } + }) +} |