From 2c3124ee2c458c444f7b3e22a20f1d05b66d3949 Mon Sep 17 00:00:00 2001 From: ErfanTech Date: Sun, 8 Sep 2024 13:34:06 +0330 Subject: [PATCH] feat: add CORS middleware configuration with custom allowed origins(#154) --- config/config.go | 7 ++++++- delivery/http_server/server.go | 3 +++ deploy/stage/app/config.yml | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 22aec59..f000f1e 100644 --- a/config/config.go +++ b/config/config.go @@ -10,7 +10,12 @@ import ( ) type HTTPServer struct { - Port int `koanf:"port"` + Port int `koanf:"port"` + Cors Cors `koanf:"cors"` +} + +type Cors struct { + AllowOrigins []string `koanf:"allow_origins"` } type Config struct { diff --git a/delivery/http_server/server.go b/delivery/http_server/server.go index 0d1ceca..780dca8 100644 --- a/delivery/http_server/server.go +++ b/delivery/http_server/server.go @@ -67,6 +67,9 @@ func (s Server) Serve() { func (s Server) RegisterRoutes() { s.Router.Use(middleware.RequestID()) s.Router.Use(middleware.Recover()) + s.Router.Use(middleware.CORSWithConfig(middleware.CORSConfig{ + AllowOrigins: s.config.HTTPServer.Cors.AllowOrigins, + })) registerSwagger(s.Router) // Routes diff --git a/deploy/stage/app/config.yml b/deploy/stage/app/config.yml index dd6c42a..dcd27e6 100644 --- a/deploy/stage/app/config.yml +++ b/deploy/stage/app/config.yml @@ -3,6 +3,9 @@ type: yml http_server: port: 1313 + cors: + allow_origins: + - "*" benefactor_service: length_of_otp_code: 5