forked from i-love-flamingo/flamingo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
119 lines (94 loc) · 2.27 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package web
import (
"sync"
"github.com/google/uuid"
"github.com/gorilla/sessions"
)
// todo: still necessary?
const (
// INFO notice
INFO = "info"
// WARNING notice
WARNING = "warning"
// ERROR notice
ERROR = "error"
)
type Session struct {
mu sync.RWMutex
//data map[interface{}]interface{}
//id string
s *sessions.Session
}
func newID() string {
return uuid.New().String()
}
// NewSession wraps a gorilla session
func NewSession(s *sessions.Session) *Session {
return &Session{s: s}
}
// Load data by a key
func (s *Session) Load(key interface{}) (data interface{}, ok bool) {
s.mu.RLock()
defer s.mu.RUnlock()
data, ok = s.s.Values[key]
return data, ok
}
// Load data by a key
func (s *Session) Try(key interface{}) (data interface{}) {
s.mu.RLock()
defer s.mu.RUnlock()
data, _ = s.s.Values[key]
return data
}
// Store data with a key in the Session
func (s *Session) Store(key interface{}, data interface{}) {
s.mu.Lock()
defer s.mu.Unlock()
s.s.Values[key] = data
}
// Delete a given key from the session
func (s *Session) Delete(key interface{}) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.s.Values, key)
}
// ID returns the Session id
func (s *Session) ID() (id string) {
s.mu.RLock()
defer s.mu.RUnlock()
return s.s.ID
}
// Migrate should generate a new ID and remove old Session data
func (s *Session) migrate() (id, old_id string) {
s.mu.Lock()
defer s.mu.Unlock()
//old_id = s.id
//s.id = newID()
return "", ""
//return s.id, old_id
}
// Flashes returns a slice of flash messages from the session.
//
// A single variadic argument is accepted, and it is optional: it defines
// the flash key. If not defined "_flash" is used by default.
func (s *Session) Flashes(vars ...string) []interface{} {
s.mu.RLock()
defer s.mu.RUnlock()
return s.s.Flashes(vars...)
}
// AddFlash adds a flash message to the session.
//
// A single variadic argument is accepted, and it is optional: it defines
// the flash key. If not defined "_flash" is used by default.
func (s *Session) AddFlash(value interface{}, vars ...string) {
s.mu.Lock()
defer s.mu.Unlock()
s.s.AddFlash(value, vars...)
}
// G access gorilla subsession
// deprecated: kept for backwards compatibility
func (s *Session) G() *sessions.Session {
s.mu.Lock()
defer s.mu.Unlock()
return s.s
}