/
advanceddev
/
license-server
Обзор
Документация
Войти
/
advanceddev
/
license-server
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
2
CI/CD
Аналитика
Безопасность
master
internal/middleware/requestid.go
36 строк
840 B
advanceddev
first_commit
09 авг 2026, 20:04
Не верифицирован
09 авг 2026, 20:04
1326ca6
Код
Авторство
О чём код?
package middleware import ( "context" "net/http" "github.com/google/uuid" ) type ctxKey string // RequestIDKey - ключ для контекста const RequestIDKey ctxKey = "request_id" // RequestID - добавляет уникальный ID к каждому запросу func RequestID(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { id := r.Header.Get("X-Request-ID") if id == "" { id = uuid.New().String() } w.Header().Set("X-Request-ID", id) ctx := context.WithValue(r.Context(), RequestIDKey, id) next.ServeHTTP(w, r.WithContext(ctx)) }) } // GetRequestID - извлекает request ID из контекста. func GetRequestID(ctx context.Context) string { if id, ok := ctx.Value(RequestIDKey).(string); ok { return id } return "" }