With Go\'s context package it is possible to pass request-specific data to the stack of request handling functions using
func WithValue(parent C
One (functional) way to do it is using currying and closures
r.WithContext(BuildContext(
r.Context(),
SchemaId(mySchemaId),
RequestId(myRequestId),
Logger(myLogger)
))
func RequestId(id string) partialContextFn {
return func(ctx context.Context) context.Context {
return context.WithValue(ctx, requestIdCtxKey, requestId)
}
}
func BuildContext(ctx context.Context, ctxFns ...partialContextFn) context.Context {
for f := range ctxFns {
ctx = f(ctx)
}
return ctx
}
type partialContextFn func(context.Context) context.Context