From ec158c77cdc1a092f559364a90b908db27caae1d Mon Sep 17 00:00:00 2001 From: Dexter Date: Tue, 24 Jan 2023 12:11:54 +0000 Subject: [PATCH] fix: better cors solution --- go.mod | 1 + go.sum | 2 ++ main.go | 8 ++++---- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index 0060e5c..9b8cd19 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/google/uuid v1.2.0 // indirect github.com/gorilla/websocket v1.4.2 // indirect github.com/rjeczalik/notify v0.9.1 // indirect + github.com/rs/cors v1.8.3 // indirect github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect github.com/tklauser/go-sysconf v0.3.5 // indirect github.com/tklauser/numcpus v0.2.2 // indirect diff --git a/go.sum b/go.sum index 3d0114e..3f27ede 100644 --- a/go.sum +++ b/go.sum @@ -23,6 +23,8 @@ github.com/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/rjeczalik/notify v0.9.1 h1:CLCKso/QK1snAlnhNR/CNvNiFU2saUtjV0bx3EwNeCE= github.com/rjeczalik/notify v0.9.1/go.mod h1:rKwnCoCGeuQnwBtTSPL9Dad03Vh2n40ePRrjvIXnJho= +github.com/rs/cors v1.8.3 h1:O+qNyWn7Z+F9M0ILBHgMVPuB1xTOucVd5gtaYyXBpRo= +github.com/rs/cors v1.8.3/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU= github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU= github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA= github.com/tklauser/go-sysconf v0.3.5 h1:uu3Xl4nkLzQfXNsWn15rPc/HQCJKObbt1dKJeWp3vU4= diff --git a/main.go b/main.go index 3849ab0..9639eec 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ import ( ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" "github.com/julienschmidt/httprouter" + "github.com/rs/cors" "golang.org/x/exp/maps" ) @@ -471,14 +472,13 @@ func main() { router.GET("/tranches/stats", state.GetTranchesStats) go state.PullForever() + handler := cors.Default().Handler(router) - log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), router)) - + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), handler)) } func writeOK(w http.ResponseWriter, payload interface{}) { - w.Header().Set("Access-Control-Allow-Methods", w.Header().Get("Allow")) - w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Content-Type", "application/json") buf, err := json.Marshal(payload) if err != nil {