From ab0c4780ddf37373ab0b9500f0c40d7ccc5dd7cf Mon Sep 17 00:00:00 2001 From: vbasiuk Date: Mon, 2 Sep 2024 16:18:19 +0300 Subject: [PATCH] add CORS settings --- cmd/driver/main.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/cmd/driver/main.go b/cmd/driver/main.go index 7a19b9e..9e38f38 100644 --- a/cmd/driver/main.go +++ b/cmd/driver/main.go @@ -41,7 +41,7 @@ func main() { server := http.Server{ Addr: fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port), - Handler: mux.Routes(), + Handler: addCORSHeaders(mux.Routes()), ReadHeaderTimeout: time.Second, } log.Printf("HTTP server start on '%s:%d'\n", cfg.Server.Host, cfg.Server.Port) @@ -98,3 +98,17 @@ func initEIP712Signers() *services.EIP712SignerRegistry { return chainSigners } + +func addCORSHeaders(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS") + + if r.Method == http.MethodOptions { + w.WriteHeader(http.StatusOK) + return + } + + next.ServeHTTP(w, r) + }) +}