From 16f93d74c85d282d1d4a8434a16950eb1e62dd58 Mon Sep 17 00:00:00 2001 From: AmirAgassi <33383085+AmirAgassi@users.noreply.github.com> Date: Mon, 16 Dec 2024 12:31:08 -0500 Subject: [PATCH 1/3] Add request ID middleware --- backend/internal/middleware/request_id.go | 42 +++++++++++++++++++++++ backend/internal/server/middleware.go | 6 +++- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 backend/internal/middleware/request_id.go diff --git a/backend/internal/middleware/request_id.go b/backend/internal/middleware/request_id.go new file mode 100644 index 00000000..464c3572 --- /dev/null +++ b/backend/internal/middleware/request_id.go @@ -0,0 +1,42 @@ +package middleware + +import ( + "github.com/labstack/echo/v4" + "github.com/labstack/echo/v4/middleware" +) + +// RequestIDConfig holds configuration for the request ID middleware +type RequestIDConfig struct { + // Generator is a function to generate request IDs + Generator func() string + // RequestHeader is the name of the header to read the request ID from + RequestHeader string + // ResponseHeader is the name of the header to write the request ID to + ResponseHeader string +} + +// DefaultRequestIDConfig provides default configuration values +func DefaultRequestIDConfig() RequestIDConfig { + return RequestIDConfig{ + Generator: nil, // will use echo's default uuid generator + RequestHeader: echo.HeaderXRequestID, + ResponseHeader: echo.HeaderXRequestID, + } +} + +// RequestID returns a middleware that adds a request ID to the request context +func RequestID() echo.MiddlewareFunc { + return RequestIDWithConfig(DefaultRequestIDConfig()) +} + +// RequestIDWithConfig returns a middleware with custom configuration +func RequestIDWithConfig(config RequestIDConfig) echo.MiddlewareFunc { + // convert our config to echo's middleware config + echoConfig := middleware.RequestIDConfig{ + Generator: config.Generator, + RequestHeader: config.RequestHeader, + ResponseHeader: config.ResponseHeader, + } + + return middleware.RequestIDWithConfig(echoConfig) +} \ No newline at end of file diff --git a/backend/internal/server/middleware.go b/backend/internal/server/middleware.go index f9c12b67..4b0daa04 100644 --- a/backend/internal/server/middleware.go +++ b/backend/internal/server/middleware.go @@ -1,8 +1,12 @@ package server +import ( + "KonferCA/SPUR/internal/middleware" +) + /* Setup all the global middlewares used in the server. */ func (s *Server) setupMiddlewares() { - + s.E.Use(middleware.RequestID()) } From b05b0518816e47fb3ca4607244bd1d24413bce6c Mon Sep 17 00:00:00 2001 From: AmirAgassi <33383085+AmirAgassi@users.noreply.github.com> Date: Mon, 16 Dec 2024 13:26:12 -0500 Subject: [PATCH 2/3] Remove custom config --- backend/internal/middleware/request_id.go | 33 +---------------------- 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/backend/internal/middleware/request_id.go b/backend/internal/middleware/request_id.go index 464c3572..06594af6 100644 --- a/backend/internal/middleware/request_id.go +++ b/backend/internal/middleware/request_id.go @@ -5,38 +5,7 @@ import ( "github.com/labstack/echo/v4/middleware" ) -// RequestIDConfig holds configuration for the request ID middleware -type RequestIDConfig struct { - // Generator is a function to generate request IDs - Generator func() string - // RequestHeader is the name of the header to read the request ID from - RequestHeader string - // ResponseHeader is the name of the header to write the request ID to - ResponseHeader string -} - -// DefaultRequestIDConfig provides default configuration values -func DefaultRequestIDConfig() RequestIDConfig { - return RequestIDConfig{ - Generator: nil, // will use echo's default uuid generator - RequestHeader: echo.HeaderXRequestID, - ResponseHeader: echo.HeaderXRequestID, - } -} - // RequestID returns a middleware that adds a request ID to the request context func RequestID() echo.MiddlewareFunc { - return RequestIDWithConfig(DefaultRequestIDConfig()) -} - -// RequestIDWithConfig returns a middleware with custom configuration -func RequestIDWithConfig(config RequestIDConfig) echo.MiddlewareFunc { - // convert our config to echo's middleware config - echoConfig := middleware.RequestIDConfig{ - Generator: config.Generator, - RequestHeader: config.RequestHeader, - ResponseHeader: config.ResponseHeader, - } - - return middleware.RequestIDWithConfig(echoConfig) + return middleware.RequestID() } \ No newline at end of file From a47cdee4fbd22863c9b2881ce067a70e148c3165 Mon Sep 17 00:00:00 2001 From: AmirAgassi <33383085+AmirAgassi@users.noreply.github.com> Date: Mon, 16 Dec 2024 13:54:20 -0500 Subject: [PATCH 3/3] Fix E --- backend/internal/server/middleware.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/internal/server/middleware.go b/backend/internal/server/middleware.go index 4b0daa04..2fa1028a 100644 --- a/backend/internal/server/middleware.go +++ b/backend/internal/server/middleware.go @@ -8,5 +8,5 @@ import ( Setup all the global middlewares used in the server. */ func (s *Server) setupMiddlewares() { - s.E.Use(middleware.RequestID()) + s.Echo.Use(middleware.RequestID()) }