Skip to content

Commit

Permalink
Fix the wrong logic to initialize the Request ID
Browse files Browse the repository at this point in the history
  • Loading branch information
yunkon-kim committed Apr 1, 2024
1 parent c74ddab commit 1c0d248
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/api/rest/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,18 @@ func RunServer(port string) {
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Logger().Printf("Start - RequestLog()")
reqID := fmt.Sprintf("%d", time.Now().UnixNano())
c.Set("RequestID", reqID)
// make X-Request-Id visible to all handlers
c.Response().Header().Set("Access-Control-Expose-Headers", "X-Request-Id")
c.Response().Header().Set("Access-Control-Expose-Headers", echo.HeaderXRequestID)

// Get or generate Request ID
reqID := c.Request().Header.Get(echo.HeaderXRequestID)
if reqID == "" {
reqID = fmt.Sprintf("%d", time.Now().UnixNano())
}

// Set Request on the context
c.Set("RequestID", reqID)

// reqID := c.Request().Header.Get("x-request-id")
// if reqID == "" {
// reqID = fmt.Sprintf("%d", time.Now().UnixNano())
// }
c.Logger().Printf("Request ID: %s", reqID)
if _, ok := common.RequestMap.Load(reqID); ok {
return fmt.Errorf("the x-request-id is already in use")
Expand Down

0 comments on commit 1c0d248

Please sign in to comment.