-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLocalSession.go
More file actions
114 lines (103 loc) · 2.54 KB
/
LocalSession.go
File metadata and controls
114 lines (103 loc) · 2.54 KB
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
package rest
import (
"math/rand"
"net/http"
)
//local session configuration
type LocalSessionConf struct {
MaxAge int
SessionKey string
}
func initConf(conf *LocalSessionConf) {
if 0 >= conf.MaxAge {
conf.MaxAge = 3600
}
if 0 == len(conf.SessionKey) {
conf.SessionKey = "sid"
}
}
func generateSessionId(length int) string {
bs := []byte("0123456789abcedfghijklmnopqrstuvwxyzABCEDFGHIJKLMNOPQRSTUVWXYZ")
cs := make([]byte, length)
for i := 0; i < length; i++ {
cs[i] = bs[rand.Intn(len(bs))]
}
return string(cs[:])
}
//local session middleware
func LocalSession(confs ...LocalSessionConf) func(request Request, response Response, next func()) {
var conf LocalSessionConf
if 0 < len(confs) {
conf = confs[0]
}
initConf(&conf)
sessions := map[string]*LocalSessionStore{}
return func(request Request, response Response, next func()) {
key := request.Cookie(conf.SessionKey)
session := sessions[key]
if nil != session {
response.SetCookie(&http.Cookie{Name: conf.SessionKey, Value: key, MaxAge: conf.MaxAge, HttpOnly: true})
} else {
newKey := generateSessionId(32)
session = &LocalSessionStore{SessionId: newKey, Store: map[string]interface{}{}}
sessions[newKey] = session
response.SetCookie(&http.Cookie{Name: conf.SessionKey, Value: newKey, MaxAge: conf.MaxAge, HttpOnly: true})
}
request.Context.Session = session
next()
}
}
/**
Get(key string) interface{}
Set(key string, value interface{})
Delete(key string)
Length() int
Destroy()
Save()
Reload()
Regenerate()
Has(key string) bool
GetInt(key string) int
GetString(key string) string
*/
type LocalSessionStore struct {
SessionId string
Store map[string]interface{}
}
func (this *LocalSessionStore) Has(key string) bool {
_, ok := this.Store[key]
return ok
}
func (this *LocalSessionStore) Get(key string) interface{} {
return this.Store[key]
}
func (this *LocalSessionStore) GetInt(key string) int {
if v, ok := this.Store[key].(int); ok {
return v
}
return 0
}
func (this *LocalSessionStore) GetString(key string) string {
if v, ok := this.Store[key].(string); ok {
return v
}
return ""
}
func (this *LocalSessionStore) Set(key string, value interface{}) {
this.Store[key] = value
}
func (this *LocalSessionStore) Delete(key string) {
delete(this.Store, key)
}
func (this *LocalSessionStore) Length() int {
return len(this.Store)
}
func (this *LocalSessionStore) Destroy() {
this.Store = map[string]interface{}{}
}
func (this *LocalSessionStore) Save() {
}
func (this *LocalSessionStore) Reload() {
}
func (this *LocalSessionStore) Regenerate() {
}