mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-16 04:12:16 +01:00
slighty improvement of logging (#6)
This commit is contained in:
@@ -12,10 +12,40 @@ import (
|
||||
type key uint32
|
||||
|
||||
const (
|
||||
converterKey key = iota
|
||||
requestIDKey key = iota
|
||||
converterKey
|
||||
resultFilePathKey
|
||||
)
|
||||
|
||||
// WithRequestID populates a request's context with the given request ID
|
||||
// and returns the updated request.
|
||||
func WithRequestID(r *http.Request, requestID string) *http.Request {
|
||||
ctx := r.Context()
|
||||
ctx = context.WithValue(ctx, requestIDKey, requestID)
|
||||
r = r.WithContext(ctx)
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
type requestIDNotFoundError struct{}
|
||||
|
||||
const requestIDNotFoundErrorMessage = "the request ID was not found in request context"
|
||||
|
||||
func (e *requestIDNotFoundError) Error() string {
|
||||
return requestIDNotFoundErrorMessage
|
||||
}
|
||||
|
||||
// GetRequestID returns the request ID if found in
|
||||
// the request's context. Otherwise throws an error.
|
||||
func GetRequestID(r *http.Request) (string, error) {
|
||||
ID, ok := r.Context().Value(requestIDKey).(string)
|
||||
if !ok {
|
||||
return "", &requestIDNotFoundError{}
|
||||
}
|
||||
|
||||
return ID, nil
|
||||
}
|
||||
|
||||
// WithConverter populates a request's context with the given converter
|
||||
// and returns the updated request.
|
||||
func WithConverter(r *http.Request, converter *converter.Converter) *http.Request {
|
||||
|
||||
Reference in New Issue
Block a user