/
config.go
63 lines (55 loc) · 1.16 KB
/
config.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package redisactives
import (
"errors"
"time"
"github.com/herb-go/datasource/redis/redispool"
"github.com/herb-go/usersystem"
"github.com/herb-go/usersystem/modules/activesessions"
)
type Config struct {
redispool.Config
Interval int64
Prefix string
Durations map[string]string
}
func (c *Config) CreateService() (*Service, error) {
s := NewService()
p := redispool.New()
err := c.Config.ApplyTo(p)
if err != nil {
return nil, err
}
s.Pool = p.Open()
for ks := range c.Durations {
k := usersystem.SessionType(ks)
d, err := time.ParseDuration(c.Durations[ks])
if err != nil {
return nil, err
}
if d <= 0 {
return nil, errors.New("duration must larger than 0")
}
s.configs[k] = &activesessions.Config{
Duration: d,
Supported: true,
}
}
s.Interval = c.Interval
s.Prefix = c.Prefix
return s, nil
}
func (c *Config) Execute(us *usersystem.UserSystem) error {
s, err := c.CreateService()
if err != nil {
return err
}
return s.Execute(us)
}
var DirectiveFactory = func(loader func(v interface{}) error) (usersystem.Directive, error) {
c := &Config{}
err := loader(c)
if err != nil {
return nil, err
}
return c, nil
}