-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
session.go
70 lines (56 loc) · 1.38 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
package http
import (
"github.com/firmeve/firmeve/kernel/contract"
"github.com/gorilla/sessions"
"net/http"
)
type (
httpSession struct {
//Store sessions.Store `inject:"http.session.store"`
store sessions.Store
request *http.Request
response http.ResponseWriter
session *sessions.Session
}
)
func NewSession(store sessions.Store, request *http.Request, response http.ResponseWriter) contract.Session {
s := &httpSession{
store: store,
request: request,
response: response,
}
session, _ := store.Get(request, "session-name")
s.session = session
return s
}
func (s *httpSession) Id() string {
return s.session.ID
}
func (s *httpSession) Get(key string) interface{} {
if v, ok := s.session.Values[key]; ok {
return v
}
return nil
}
func (s *httpSession) GetString(key string) string {
return s.Get(key).(string)
}
func (s *httpSession) GetInt(key string) int {
return s.Get(key).(int)
}
func (s *httpSession) GetBool(key string) bool {
return s.Get(key).(bool)
}
func (s *httpSession) GetFloat(key string) float64 {
return s.Get(key).(float64)
}
func (s *httpSession) Put(key string, value interface{}) error {
s.session.Values[key] = value
return s.session.Save(s.request, s.response)
}
func (s *httpSession) Flush() {
s.session.Values = make(map[interface{}]interface{}, 0)
}
func (s *httpSession) Delete(key string) {
delete(s.session.Values, key)
}