From 29e877903db504412b8caf561c661758d270f645 Mon Sep 17 00:00:00 2001 From: optclblast Date: Thu, 30 May 2024 01:19:01 +0300 Subject: [PATCH] add fixes --- backend/go.mod | 1 + backend/go.sum | 2 ++ backend/internal/interface/rest/server.go | 12 ++++++++++++ 3 files changed, 15 insertions(+) diff --git a/backend/go.mod b/backend/go.mod index 69d3c61..8652d39 100644 --- a/backend/go.mod +++ b/backend/go.mod @@ -39,6 +39,7 @@ require ( github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/ethereum/c-kzg-4844 v1.0.0 // indirect + github.com/go-chi/cors v1.2.1 // indirect github.com/go-ole/go-ole v1.3.0 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/gorilla/websocket v1.4.2 // indirect diff --git a/backend/go.sum b/backend/go.sum index 93ae596..d08a01d 100644 --- a/backend/go.sum +++ b/backend/go.sum @@ -142,6 +142,8 @@ github.com/getsentry/sentry-go v0.18.0 h1:MtBW5H9QgdcJabtZcuJG80BMOwaBpkRDZkxRkN github.com/getsentry/sentry-go v0.18.0/go.mod h1:Kgon4Mby+FJ7ZWHFUAZgVaIa8sxHtnRJRLTXZr51aKQ= github.com/go-chi/chi/v5 v5.0.12 h1:9euLV5sTrTNTRUU9POmDUvfxyj6LAABLUcEWO+JJb4s= github.com/go-chi/chi/v5 v5.0.12/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4= +github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58= github.com/go-chi/render v1.0.3 h1:AsXqd2a1/INaIfUSKq3G5uA8weYx20FOsM7uSoCyyt4= github.com/go-chi/render v1.0.3/go.mod h1:/gr3hVkmYR0YlEy3LxCuVRFzEu9Ruok+gFqbIofjao0= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= diff --git a/backend/internal/interface/rest/server.go b/backend/internal/interface/rest/server.go index 1268498..91ded68 100644 --- a/backend/internal/interface/rest/server.go +++ b/backend/internal/interface/rest/server.go @@ -16,6 +16,7 @@ import ( "github.com/emochka2007/block-accounting/internal/usecase/interactors/jwt" "github.com/go-chi/chi/v5" mw "github.com/go-chi/chi/v5/middleware" + "github.com/go-chi/cors" "github.com/go-chi/render" "github.com/google/uuid" ) @@ -84,6 +85,17 @@ func (s *Server) Close() { func (s *Server) buildRouter() { router := chi.NewRouter() + router.Use(cors.Handler(cors.Options{ + // AllowedOrigins: []string{"https://foo.com"}, // Use this to allow specific origin hosts + AllowedOrigins: []string{"https://*", "http://*"}, + // AllowOriginFunc: func(r *http.Request, origin string) bool { return true }, + AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, + AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"}, + ExposedHeaders: []string{"Link"}, + AllowCredentials: false, + MaxAge: 300, // Maximum value not ignored by any of major browsers + })) + router.Use(mw.Recoverer) router.Use(mw.RequestID) router.Use(s.handleMw)