alchemyst/util/appctx/context.go
2019-11-14 22:28:58 +03:00

46 lines
940 B
Go

package appctx
import (
"context"
"fmt"
"github.com/rs/zerolog"
"lab.zaar.be/thefish/alchemyst-go/util"
)
const (
configKey = "config"
loggerKey = "logger"
)
type ClientCtx struct {
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}
}
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 Logger(c context.Context) *zerolog.Logger {
logger, ok := c.Value(loggerKey).(*zerolog.Logger)
if !ok {
panic(fmt.Errorf("no access to logger from context"))
}
return logger
}
//func FromContext(appctx context.Context) (*User, bool) {
// u, ok := appctx.Value(userKey).(*User)
// return u, ok
//}