/
session_handler.go
50 lines (38 loc) · 1.07 KB
/
session_handler.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
package app
import (
"github.com/thinkoner/thinkgo/config"
"github.com/thinkoner/thinkgo/context"
"github.com/thinkoner/thinkgo/session"
)
type SessionHandler struct {
Manager *session.Manager
app *Application
}
// SessionHandler The default SessionHandler
func NewSessionHandler(app *Application) Handler {
handler := &SessionHandler{}
handler.Manager = session.NewManager(&session.Config{
Driver: config.Session.Driver,
CookieName: config.Session.CookieName,
Lifetime: config.Session.Lifetime,
Encrypt: config.Session.Encrypt,
Files: config.Session.Files,
})
handler.app = app
return handler
}
func (h *SessionHandler) Process(req *context.Request, next Closure) interface{} {
store := h.startSession(req)
req.SetSession(store)
result := next(req)
if res, ok := result.(session.Response); ok {
h.saveSession(res)
}
return result
}
func (h *SessionHandler) startSession(req *context.Request) *session.Store {
return h.Manager.SessionStart(req)
}
func (h *SessionHandler) saveSession(res session.Response) {
h.Manager.SessionSave(res)
}