Skip to content
Snippets Groups Projects

Full MVC of gitlab-zoekt-indexer

Merged Dylan Griffith requested to merge add-new-api-paths into main
All threads resolved!
Compare and Show latest version
4 files
+ 84
56
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 41
0
 
package main
 
 
import (
 
"context"
 
"net/http"
 
 
"gitlab.com/gitlab-org/labkit/correlation"
 
)
 
 
// Key to use when setting the request ID.
 
type ctxKeyRequestID int
 
 
// RequestIDKey is the key that holds the unique request ID in a request context.
 
const (
 
RequestIDHeader = "X-Request-Id"
 
RequestIDKey ctxKeyRequestID = 0
 
)
 
 
func correlationIDMiddleware(next http.Handler) http.Handler {
 
fn := func(w http.ResponseWriter, r *http.Request) {
 
ctx := r.Context()
 
requestID := r.Header.Get(RequestIDHeader)
 
if requestID == "" {
 
requestID = correlation.SafeRandomID()
 
}
 
 
ctx = context.WithValue(ctx, RequestIDKey, requestID)
 
next.ServeHTTP(w, r.WithContext(ctx))
 
}
 
return http.HandlerFunc(fn)
 
}
 
 
func getCorrelationID(ctx context.Context) string {
 
if ctx == nil {
 
return ""
 
}
 
if reqID, ok := ctx.Value(RequestIDKey).(string); ok {
 
return reqID
 
}
 
return ""
 
}
Loading