@@ -18,11 +18,10 @@ import (
18
18
const Version = "3.1.4"
19
19
20
20
var (
21
- // ErrInvalidSessionID invalid session id
22
- ErrInvalidSessionID = errors .New ("invalid session id" )
21
+ ErrInvalidSessionID = errors .New ("Invalid session id" )
23
22
)
24
23
25
- // IDHandlerFunc Define the handler to get the session id
24
+ // Define the handler to get the session id
26
25
type IDHandlerFunc func (context.Context ) string
27
26
28
27
// Define default options
@@ -55,103 +54,102 @@ type options struct {
55
54
store ManagerStore
56
55
}
57
56
58
- // Option A session parameter options
59
57
type Option func (* options )
60
58
61
- // SetSign Set the session id signature value
59
+ // Set the session id signature value
62
60
func SetSign (sign []byte ) Option {
63
61
return func (o * options ) {
64
62
o .sign = sign
65
63
}
66
64
}
67
65
68
- // SetCookieName Set the cookie name
66
+ // Set the cookie name
69
67
func SetCookieName (cookieName string ) Option {
70
68
return func (o * options ) {
71
69
o .cookieName = cookieName
72
70
}
73
71
}
74
72
75
- // SetCookieLifeTime Set the cookie expiration time (in seconds)
73
+ // Set the cookie expiration time (in seconds)
76
74
func SetCookieLifeTime (cookieLifeTime int ) Option {
77
75
return func (o * options ) {
78
76
o .cookieLifeTime = cookieLifeTime
79
77
}
80
78
}
81
79
82
- // SetDomain Set the domain name of the cookie
80
+ // Set the domain name of the cookie
83
81
func SetDomain (domain string ) Option {
84
82
return func (o * options ) {
85
83
o .domain = domain
86
84
}
87
85
}
88
86
89
- // SetSecure Set cookie security
87
+ // Set cookie security
90
88
func SetSecure (secure bool ) Option {
91
89
return func (o * options ) {
92
90
o .secure = secure
93
91
}
94
92
}
95
93
96
- // SetSameSite Set SameSite attribute of the cookie
94
+ // Set SameSite attribute of the cookie
97
95
func SetSameSite (sameSite http.SameSite ) Option {
98
96
return func (o * options ) {
99
97
o .sameSite = sameSite
100
98
}
101
99
}
102
100
103
- // SetExpired Set session expiration time (in seconds)
101
+ // Set session expiration time (in seconds)
104
102
func SetExpired (expired int64 ) Option {
105
103
return func (o * options ) {
106
104
o .expired = expired
107
105
}
108
106
}
109
107
110
- // SetSessionID Set callback function to generate session id
108
+ // Set callback function to generate session id
111
109
func SetSessionID (handler IDHandlerFunc ) Option {
112
110
return func (o * options ) {
113
111
o .sessionID = handler
114
112
}
115
113
}
116
114
117
- // SetEnableSetCookie Enable writing session id to cookie
115
+ // Enable writing session id to cookie
118
116
// (enabled by default, can be turned off if no cookie is written)
119
117
func SetEnableSetCookie (enableSetCookie bool ) Option {
120
118
return func (o * options ) {
121
119
o .enableSetCookie = enableSetCookie
122
120
}
123
121
}
124
122
125
- // SetEnableSIDInURLQuery Allow session id from URL query parameters (enabled by default)
123
+ // Allow session id from URL query parameters (enabled by default)
126
124
func SetEnableSIDInURLQuery (enableSIDInURLQuery bool ) Option {
127
125
return func (o * options ) {
128
126
o .enableSIDInURLQuery = enableSIDInURLQuery
129
127
}
130
128
}
131
129
132
- // SetEnableSIDInHTTPHeader Allow session id to be obtained from the request header
130
+ // Allow session id to be obtained from the request header
133
131
func SetEnableSIDInHTTPHeader (enableSIDInHTTPHeader bool ) Option {
134
132
return func (o * options ) {
135
133
o .enableSIDInHTTPHeader = enableSIDInHTTPHeader
136
134
}
137
135
}
138
136
139
- // SetSessionNameInHTTPHeader The key name in the request header where the session ID is stored
137
+ // The key name in the request header where the session ID is stored
140
138
// (if it is empty, the default is the cookie name)
141
139
func SetSessionNameInHTTPHeader (sessionNameInHTTPHeader string ) Option {
142
140
return func (o * options ) {
143
141
o .sessionNameInHTTPHeader = sessionNameInHTTPHeader
144
142
}
145
143
}
146
144
147
- // SetStore Set session management storage
145
+ // Set session management storage
148
146
func SetStore (store ManagerStore ) Option {
149
147
return func (o * options ) {
150
148
o .store = store
151
149
}
152
150
}
153
151
154
- // NewManager Create a session management instance
152
+ // Create a session management instance
155
153
func NewManager (opt ... Option ) * Manager {
156
154
opts := defaultOptions
157
155
for _ , o := range opt {
@@ -168,7 +166,7 @@ func NewManager(opt ...Option) *Manager {
168
166
return & Manager {opts : & opts }
169
167
}
170
168
171
- // Manager A session management instance, including start and destroy operations
169
+ // A session management instance, including start and destroy operations
172
170
type Manager struct {
173
171
opts * options
174
172
}
@@ -305,8 +303,8 @@ func (m *Manager) Start(ctx context.Context, w http.ResponseWriter, r *http.Requ
305
303
}
306
304
307
305
if sid != "" {
308
- if exists , verr := m .opts .store .Check (ctx , sid ); verr != nil {
309
- return nil , verr
306
+ if exists , err := m .opts .store .Check (ctx , sid ); err != nil {
307
+ return nil , err
310
308
} else if exists {
311
309
return m .opts .store .Update (ctx , sid , m .opts .expired )
312
310
}
@@ -322,19 +320,19 @@ func (m *Manager) Start(ctx context.Context, w http.ResponseWriter, r *http.Requ
322
320
return store , nil
323
321
}
324
322
325
- // Refresh a session and return to session storage
323
+ // Refresh and return session storage
326
324
func (m * Manager ) Refresh (ctx context.Context , w http.ResponseWriter , r * http.Request ) (Store , error ) {
327
325
ctx = m .getContext (ctx , w , r )
328
326
329
- oldsid , err := m .sessionID (r )
327
+ oldSID , err := m .sessionID (r )
330
328
if err != nil {
331
329
return nil , err
332
- } else if oldsid == "" {
333
- oldsid = m .opts .sessionID (ctx )
330
+ } else if oldSID == "" {
331
+ oldSID = m .opts .sessionID (ctx )
334
332
}
335
333
336
334
sid := m .opts .sessionID (ctx )
337
- store , err := m .opts .store .Refresh (ctx , oldsid , sid , m .opts .expired )
335
+ store , err := m .opts .store .Refresh (ctx , oldSID , sid , m .opts .expired )
338
336
if err != nil {
339
337
return nil , err
340
338
}
@@ -354,8 +352,7 @@ func (m *Manager) Destroy(ctx context.Context, w http.ResponseWriter, r *http.Re
354
352
return nil
355
353
}
356
354
357
- exists , err := m .opts .store .Check (ctx , sid )
358
- if err != nil {
355
+ if exists , err := m .opts .store .Check (ctx , sid ); err != nil {
359
356
return err
360
357
} else if ! exists {
361
358
return nil
0 commit comments