/
boltdb.go
78 lines (71 loc) · 1.63 KB
/
boltdb.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
78
package session
import (
"github.com/admpub/boltstore/reaper"
"github.com/admpub/boltstore/store"
"github.com/admpub/sessions"
"github.com/boltdb/bolt"
"github.com/webx-top/webx/lib/events"
I "github.com/webx-top/webx/lib/session/ssi"
)
var boltDB *bolt.DB
var onCloseBolt func() error
type BoltStore interface {
Store
}
func CloseBolt() {
if boltDB == nil {
return
}
boltDB.Close()
if onCloseBolt != nil {
onCloseBolt()
}
}
//./sessions.db
func NewBoltStore(dbFile string, options I.Options, bucketName []byte, keyPairs ...[]byte) (BoltStore, error) {
var err error
if boltDB == nil {
boltDB, err = bolt.Open(dbFile, 0666, nil)
if err != nil {
panic(err)
}
quiteC, doneC := reaper.Run(boltDB, reaper.Options{})
onCloseBolt = func() error {
// Invoke a reaper which checks and removes expired sessions periodically.
reaper.Quit(quiteC, doneC)
return nil
}
events.AddEvent(`webx.serverExit`, func(_ interface{}, next func(bool)) {
CloseBolt()
next(true)
})
}
stor, err := store.New(boltDB, store.Config{
SessionOptions: sessions.Options{
Path: options.Path,
Domain: options.Domain,
MaxAge: options.MaxAge,
Secure: options.Secure,
HttpOnly: options.HttpOnly,
},
DBOptions: store.Options{bucketName},
}, keyPairs...)
if err != nil {
return nil, err
}
return &boltStore{stor}, nil
}
type boltStore struct {
*store.Store
}
func (c *boltStore) Options(options I.Options) {
/*
c.Store.SessionOptions = sessions.Options{
Path: options.Path,
Domain: options.Domain,
MaxAge: options.MaxAge,
Secure: options.Secure,
HttpOnly: options.HttpOnly,
}
*/
}