make context great again

This commit is contained in:
2019-11-14 22:28:58 +03:00
parent 3560be99a1
commit 6a37870bd2
12 changed files with 160 additions and 84 deletions

View File

@ -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"))
}