-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsessions.go
59 lines (49 loc) · 1.32 KB
/
sessions.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
package sessions
import (
"net/http"
)
const (
defaultMaxAge = 3600 * 24 * 7 // 1 week
)
// Session represents state values maintained in a sessions Store.
type Session[V any] struct {
name string
values map[string]V
// convenience methods Save and Destroy use store
store Store[V]
}
// NewSession returns a new Session.
func NewSession[V any](store Store[V], name string) *Session[V] {
return &Session[V]{
name: name,
values: make(map[string]V),
store: store,
}
}
// Name returns the name of the session.
func (s *Session[V]) Name() string {
return s.name
}
// Set sets a key/value pair in the session state.
func (s *Session[V]) Set(key string, value V) {
s.values[key] = value
}
// Get returns the state value for the given key.
func (s *Session[V]) Get(key string) V {
return s.values[key]
}
// GetOk returns the state value for the given key and whether they key exists.
func (s *Session[V]) GetOk(key string) (V, bool) {
value, ok := s.values[key]
return value, ok
}
// Save adds or updates the session. Identical to calling
// store.Save(w, session).
func (s *Session[V]) Save(w http.ResponseWriter) error {
return s.store.Save(w, s)
}
// Destroy destroys the session. Identical to calling
// store.Destroy(w, session.name).
func (s *Session[V]) Destroy(w http.ResponseWriter) {
s.store.Destroy(w, s.name)
}