-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsession.go
More file actions
72 lines (59 loc) · 1.76 KB
/
session.go
File metadata and controls
72 lines (59 loc) · 1.76 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
package main
import (
"context"
"encoding/json"
"errors"
"net/http"
"time"
"github.com/redis/rueidis"
"github.com/rs/zerolog/log"
"app/session"
)
const SessionKeyRedisPrefix = "patch:session:"
func (h *handler) NewSession(ctx context.Context, w http.ResponseWriter, s session.Session) error {
return h.setSession(ctx, w, &s)
}
func (h *handler) setSession(ctx context.Context, w http.ResponseWriter, s *session.Session) error {
err := h.r.Do(ctx, h.r.B().Set().Key(SessionKeyRedisPrefix+s.Key).Value(rueidis.JSON(s)).Ex(time.Hour*24*30).Build()).Error()
if err != nil {
return err
}
http.SetCookie(w, &http.Cookie{
Name: session.CookieName,
Value: s.Key,
HttpOnly: true,
Expires: time.Now().Add(time.Hour * 24 * 30),
})
return nil
}
func SessionMiddleware(h *handler) func(next http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := r.Cookie(session.CookieName)
if err != nil { // no cookie, do nothing
next.ServeHTTP(w, r)
return
}
v, err := h.r.Do(r.Context(), h.r.B().Get().Key(SessionKeyRedisPrefix+c.Value).Build()).AsBytes()
if rueidis.IsRedisNil(err) {
next.ServeHTTP(w, r)
return
}
if err != nil {
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return
}
log.Err(err).Msg("failed to fetch session from redis")
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var s session.Session
if err := json.Unmarshal(v, &s); err != nil {
log.Err(err).Msg("failed to decode session from redis value")
next.ServeHTTP(w, r)
return
}
next.ServeHTTP(w, r.WithContext(session.SetSession(r.Context(), &s)))
})
}
}