Allow all CORS origins and headers

This commit is contained in:
Elara 2022-12-20 14:02:57 -08:00
parent 1d09a2cbb5
commit 1ea03b1025
1 changed files with 13 additions and 2 deletions

View File

@ -42,9 +42,20 @@ func main() {
}
log.Info("Starting HTTP API server").Str("addr", ln.Addr().String()).Send()
err = http.Serve(ln, srv)
err = http.Serve(ln, allowAllCORSHandler(srv))
if err != nil {
log.Fatal("Error while running server").Err(err).Send()
}
}
func allowAllCORSHandler(h http.Handler) http.Handler {
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Access-Control-Allow-Origin", "*")
res.Header().Set("Access-Control-Allow-Headers", "*")
if req.Method == http.MethodOptions {
return
}
h.ServeHTTP(res, req)
})
}