-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! feat(requestid): add helper package
- Loading branch information
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// reqid provides gin-gonic/gin middleware to generate a requestid for each request | ||
package reqid | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/google/uuid" | ||
) | ||
|
||
type RequestIDKey string | ||
|
||
const ( | ||
RequestID RequestIDKey = "request_id" | ||
|
||
headerRequestID = "X-Request-ID" | ||
) | ||
|
||
// Middleware initializes the request ID | ||
func Middleware() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
rid := c.GetHeader(headerRequestID) | ||
if rid == "" { | ||
rid = uuid.New().String() | ||
c.Request.Header.Add(headerRequestID, rid) | ||
} | ||
|
||
// set reqeust ID request context | ||
ctx := context.WithValue(c.Request.Context(), RequestID, rid) | ||
c.Request = c.Request.WithContext(ctx) | ||
|
||
// ensures that the request ID is in the response | ||
c.Header(headerRequestID, rid) | ||
c.Next() | ||
} | ||
} | ||
|
||
// Get returns the request ID | ||
func Get(c *gin.Context) string { | ||
return c.GetHeader(headerRequestID) | ||
} | ||
|
||
func GetFromContext(ctx context.Context) string { | ||
rid, ok := ctx.Value(RequestID).(string) | ||
if !ok { | ||
return "" | ||
} | ||
return rid | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package reqid | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
//nolint:gochecknoinits | ||
func init() { | ||
gin.SetMode(gin.TestMode) | ||
} | ||
|
||
func testHandler(c *gin.Context) { | ||
c.Status(http.StatusOK) | ||
} | ||
|
||
func TestMiddleware(t *testing.T) { | ||
r := gin.New() | ||
r.Use(Middleware()) | ||
r.GET("/", testHandler) | ||
|
||
w := httptest.NewRecorder() | ||
req, err := http.NewRequest(http.MethodGet, "/", nil) | ||
require.NoError(t, err) | ||
|
||
r.ServeHTTP(w, req) | ||
|
||
assert.Equal(t, http.StatusOK, w.Code) | ||
assert.NotEmpty(t, w.Header().Get(headerRequestID)) | ||
} |