-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
76 lines (68 loc) · 1.45 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
package framework
import (
"net/http"
)
type Sessioner interface {
Sessions() Sessions
Session(string) Session
SetSessionValue(string, interface{}, interface{}) error
GetSessionValue(string, interface{}) (Value, error)
}
type SessionStore interface {
Names() []string
Get(*http.Request, string) (Session, error)
GetAll(*http.Request) (Sessions, error)
GetMany(*http.Request, ...string) (Sessions, error)
New(*http.Request, string) (Session, error)
Save(*http.Request, http.ResponseWriter, Session) error
}
type Sessions []Session
func (s Sessions) Get(name string) Session {
for _, se := range s {
if se.Name() == name {
return se
}
}
return nil
}
func (s *Sessions) Append(nses ...Session) {
for _, ns := range nses {
if ns == nil {
continue
}
for i, se := range *s {
if se.Name() == ns.Name() {
(*s)[i] = ns
continue
}
}
*s = append(*s, ns)
}
}
func (s Sessions) Save(r *http.Request, w http.ResponseWriter) error {
var err error
for _, se := range s {
if !se.Changed() {
continue
}
err = se.Save(r, w)
if err != nil {
return err
}
}
return nil
}
type Session interface {
Set(interface{}, interface{})
Get(interface{}) Value
GetOr(interface{}, interface{}) Value
Unset(interface{}) bool
ID() string
Flashes(vars ...string) []Value
AddFlash(value interface{}, vars ...string)
Save(*http.Request, http.ResponseWriter) error
Name() string
Store() SessionStore
Values() Values
Changed() bool
}