-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* fix #1 . add request ID middleware
- Loading branch information
Showing
6 changed files
with
47 additions
and
2 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
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
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
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
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
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,41 @@ | ||
package middlewares | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
type reqIDCtx int | ||
|
||
const reqIDContexKey reqIDCtx = reqIDCtx(0) | ||
|
||
// requestIDContext creates a context with request id | ||
func requestIDContext(ctx context.Context, rid string) context.Context { | ||
return context.WithValue(ctx, reqIDContexKey, rid) | ||
} | ||
|
||
// requestIDFromContext returns the request id from context | ||
func requestIDFromContext(ctx context.Context) string { | ||
rid, ok := ctx.Value(reqIDContexKey).(string) | ||
if !ok { | ||
return "" | ||
} | ||
return rid | ||
} | ||
|
||
// RequestIDHandler sets unique request id. | ||
// If header `X-Request-ID` is already present in the request, that is considered the | ||
// request id. Otherwise, generates a new unique ID. | ||
func RequestIDHandler(h http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
rid := r.Header.Get("X-Request-ID") | ||
if rid == "" { | ||
rid = uuid.New().String() | ||
r.Header.Set("X-Request-ID", rid) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
RaVbaker
|
||
} | ||
ctx := requestIDContext(r.Context(), rid) | ||
h.ServeHTTP(w, r.WithContext(ctx)) | ||
}) | ||
} |
@mbobakov @AndersonQ shouldn't here be used the responseWriter
w
instead ofRequest
?