mirror of
https://github.com/absmach/magistrala.git
synced 2026-06-23 04:10:28 +00:00
61d0427898
Signed-off-by: dusan <borovcanindusan1@gmail.com>
28 lines
634 B
Go
28 lines
634 B
Go
// Copyright (c) Abstract Machines
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package http
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/absmach/magistrala"
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
)
|
|
|
|
func RequestIDMiddleware(idp magistrala.IDProvider) func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
requestID, err := idp.ID()
|
|
if err != nil {
|
|
EncodeError(r.Context(), err, w)
|
|
return
|
|
}
|
|
|
|
ctx := context.WithValue(r.Context(), middleware.RequestIDKey, requestID)
|
|
next.ServeHTTP(w, r.WithContext(ctx))
|
|
})
|
|
}
|
|
}
|