-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container.go
46 lines (40 loc) · 1.06 KB
/
container.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package etoken
import (
"github.com/eezz10001/eredis"
"github.com/go-redis/redis/v8"
"github.com/eezz10001/ego/core/econf"
"github.com/eezz10001/ego/core/elog"
)
type Container struct {
config *config
client *redis.Client
logger *elog.Component
}
func DefaultContainer() *Container {
return &Container{
config: DefaultConfig(),
logger: elog.EgoLogger.With(elog.FieldComponent(PackageName)),
}
}
// Load ...
func Load(key string) *Container {
var container = DefaultContainer()
if err := econf.UnmarshalKey(key, &container.config); err != nil {
container.logger.Panic("parse etoken config panic",
elog.FieldErr(err), elog.FieldKey(key), elog.FieldValueAny(container.config))
}
container.logger = container.logger.With(elog.FieldComponentName(key))
return container
}
// Build
func (con *Container) Build(options ...Option) *Component {
for _, option := range options {
option(con)
}
return newComponent(con.config, con.client, con.logger)
}
func WithRedis(client *eredis.Component) Option {
return func(c *Container) {
c.client = client.Stub()
}
}