feat(middlewares): Continue adding std middlewares
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
10
commonctx/contextkey.go
Normal file
10
commonctx/contextkey.go
Normal file
@ -0,0 +1,10 @@
|
||||
package commonctx
|
||||
|
||||
// contextkey is a string type that will be used to define standard key used for values in context.
|
||||
type contextkey string
|
||||
|
||||
//nolint:exported // keeping the enum simple and readable.
|
||||
const (
|
||||
ContextKey_MainLogger contextkey = "mainLogger"
|
||||
ContextKey_RequestID contextkey = "requestID"
|
||||
)
|
@ -6,22 +6,40 @@ import (
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const LoggerKey = "mainLogger"
|
||||
|
||||
// AddMainLogger will add the provided logrus entry as the main logger in context.
|
||||
func AddMainLogger(ctx context.Context, logger *logrus.Entry) context.Context {
|
||||
return context.WithValue(ctx, LoggerKey, logger)
|
||||
return context.WithValue(ctx, ContextKey_MainLogger, logger)
|
||||
}
|
||||
|
||||
// GetLogger retrieve logger from the context.
|
||||
func GetLogger(ctx context.Context) *logrus.Entry {
|
||||
if log := ctx.Value(LoggerKey); log != nil {
|
||||
if log := ctx.Value(ContextKey_MainLogger); log != nil {
|
||||
return log.(*logrus.Entry)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetContextAndLoggerWithField will add a key and value field to the logger, update the context, and return the new logger.
|
||||
func GetContextAndLoggerWithField(ctx context.Context, key string, value interface{}) (context.Context, *logrus.Entry) {
|
||||
if log := ctx.Value(ContextKey_MainLogger); log != nil {
|
||||
log, ok := log.(*logrus.Entry)
|
||||
if !ok {
|
||||
panic("invalid logger in context")
|
||||
}
|
||||
log = log.WithField(key, value)
|
||||
ctx = AddMainLogger(ctx, log)
|
||||
return ctx, log
|
||||
}
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
// GetContextAndLoggerWithFields will add provided fields to the logger, update the context, and return the new logger.
|
||||
func GetContextAndLoggerWithFields(ctx context.Context, fields logrus.Fields) (context.Context, *logrus.Entry) {
|
||||
if log := ctx.Value(LoggerKey); log != nil {
|
||||
log := log.(*logrus.Entry)
|
||||
if log := ctx.Value(ContextKey_MainLogger); log != nil {
|
||||
log, ok := log.(*logrus.Entry)
|
||||
if !ok {
|
||||
panic("invalid logger in context")
|
||||
}
|
||||
log = log.WithFields(fields)
|
||||
ctx = AddMainLogger(ctx, log)
|
||||
return ctx, log
|
||||
|
18
commonctx/request_id.go
Normal file
18
commonctx/request_id.go
Normal file
@ -0,0 +1,18 @@
|
||||
package commonctx
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
// AddRequestID will add the provided requestID in the context.
|
||||
func AddRequestID(ctx context.Context, requestID string) context.Context {
|
||||
return context.WithValue(ctx, ContextKey_RequestID, requestID)
|
||||
}
|
||||
|
||||
// GetRequestID retrieve a requestID from the context.
|
||||
func GetRequestID(ctx context.Context) *string {
|
||||
if requestID := ctx.Value(ContextKey_RequestID); requestID != nil {
|
||||
return requestID.(*string)
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user