forked from mtusi2026/mtusi2026lab6
lab6
This commit is contained in:
@@ -3,12 +3,15 @@ package rest
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const requestIDHeader = "X-Request-ID"
|
||||
|
||||
var requestIDPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,64}$`)
|
||||
|
||||
type requestIDKey struct{}
|
||||
|
||||
func requestIDFromContext(ctx context.Context) string {
|
||||
@@ -24,7 +27,7 @@ func withRequestID(ctx context.Context, id string) context.Context {
|
||||
func requestIDMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.Header.Get(requestIDHeader)
|
||||
if id == "" {
|
||||
if !requestIDPattern.MatchString(id) {
|
||||
id = uuid.NewString()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user