make context great again
This commit is contained in:
@ -13,26 +13,26 @@ const (
|
||||
)
|
||||
|
||||
type ClientCtx struct {
|
||||
ctx context.Context
|
||||
context.Context
|
||||
}
|
||||
|
||||
func NewClientContext(config *util.Config, logger *zerolog.Logger) ClientCtx {
|
||||
ctx := context.Context(context.TODO())
|
||||
ctx = context.WithValue(ctx, configKey, config)
|
||||
ctx = context.WithValue(ctx, loggerKey, logger)
|
||||
return ClientCtx{ctx: ctx}
|
||||
return ClientCtx{ ctx}
|
||||
}
|
||||
|
||||
func (c *ClientCtx) Config() *util.Config {
|
||||
cfg, ok := c.ctx.Value(configKey).(*util.Config)
|
||||
func Config(c context.Context) *util.Config {
|
||||
cfg, ok := c.Value(configKey).(*util.Config)
|
||||
if !ok {
|
||||
panic(fmt.Errorf("no access to config from context"))
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
|
||||
func (c *ClientCtx) Logger() *zerolog.Logger {
|
||||
logger, ok := c.ctx.Value(loggerKey).(*zerolog.Logger)
|
||||
func Logger(c context.Context) *zerolog.Logger {
|
||||
logger, ok := c.Value(loggerKey).(*zerolog.Logger)
|
||||
if !ok {
|
||||
panic(fmt.Errorf("no access to logger from context"))
|
||||
}
|
||||
|
Reference in New Issue
Block a user