forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
77 lines (63 loc) · 1.62 KB
/
session.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package share
import (
"fmt"
"path/filepath"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/config"
"github.com/yaoapp/gou/session"
)
var sessionDB *session.BuntDB
// SessionStart start session
func SessionStart() error {
if config.Conf.Session.Store == "file" {
return SessionFile()
} else if config.Conf.Session.Store == "redis" {
return SessionRedis()
}
return fmt.Errorf("Session Store config error %s (file|redis)", config.Conf.Session.Store)
}
// SessionStop stop session
func SessionStop() {
if sessionDB != nil {
sessionDB.Close()
}
}
// SessionRedis Connect redis server
func SessionRedis() error {
args := []string{}
if config.Conf.Session.Port == "" {
config.Conf.Session.Port = "6379"
}
if config.Conf.Session.DB == "" {
config.Conf.Session.DB = "1"
}
args = append(args, config.Conf.Session.Port, config.Conf.Session.DB, config.Conf.Session.Password)
rdb, err := session.NewRedis(config.Conf.Session.Host, args...)
if err != nil {
return err
}
session.Register("redis", rdb)
session.Name = "redis"
log.Trace("Session Store:REDIS HOST:%s PORT:%s DB:%s", config.Conf.Session.Host, config.Conf.Session.Port, config.Conf.Session.DB)
return nil
}
// SessionFile Start session file
func SessionFile() error {
file := config.Conf.Session.File
if file == "" {
file = filepath.Join(config.Conf.Root, "data", ".session.db")
}
file, err := filepath.Abs(file)
if err != nil {
return err
}
burndb, err := session.NewBuntDB(file)
if err != nil {
return err
}
session.Register("file", burndb)
session.Name = "file"
sessionDB = burndb
log.Trace("Session Store: File %s", file)
return nil
}