This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathmiddleware.go
147 lines (108 loc) · 2.72 KB
/
middleware.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"log"
"net/http"
"sync"
"time"
"github.com/google/uuid"
"github.com/gorilla/mux"
elektra "github.com/ElektraInitiative/libelektra/src/bindings/go-elektra/kdb"
)
var (
sessions sync.Map
maxAge = 60 * 60 // 1 hour
)
type session struct {
handle *handle
mut sync.Mutex
expiry time.Time
}
func handleMiddleware(pool *handlePool) mux.MiddlewareFunc {
go freeHandles()
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var s *session
if cookie, err := r.Cookie("session"); err != nil {
s = newSession(w, r, pool)
} else {
uuid := cookie.Value
now := time.Now()
ses, ok := sessions.Load(uuid)
if s, ok = ses.(*session); !ok || now.After(s.expiry) {
// the session expired or does not exist, create a new one
s = newSessionWithUUID(w, r, pool, uuid)
} else {
// extend lifetime of session after every request
s.expiry = sessionExpiry()
}
}
// prevent the handle from being used in parallel
s.mut.Lock()
defer s.mut.Unlock()
next.ServeHTTP(w, r)
})
}
}
func freeHandles() {
for {
time.Sleep(5 * time.Minute)
now := time.Now()
sessions.Range(func(key, value interface{}) bool {
s := value.(*session)
if now.After(s.expiry) {
err := s.handle.kdb.Close()
if err != nil {
log.Printf("error closing handle: %v", err)
}
s.handle.keySet.Close()
sessions.Delete(key)
}
return true
})
}
}
func newSession(w http.ResponseWriter, r *http.Request, pool *handlePool) *session {
uuid := uuid.New().String()
return newSessionWithUUID(w, r, pool, uuid)
}
func newSessionWithUUID(w http.ResponseWriter, r *http.Request, pool *handlePool, uuid string) *session {
cookie := cookieFromUUID(uuid)
http.SetCookie(w, cookie)
r.AddCookie(cookie)
h := pool.Get()
s := &session{
handle: h,
expiry: sessionExpiry(),
}
sessions.Store(uuid, s)
return s
}
func sessionExpiry() time.Time {
return time.Now().Add(1 * time.Hour)
}
func cookieFromUUID(uuid string) *http.Cookie {
return &http.Cookie{
Name: "session",
Value: uuid,
HttpOnly: true,
MaxAge: maxAge,
}
}
func getHandle(r *http.Request) (elektra.KDB, elektra.KeySet) {
cookie, err := r.Cookie("session")
if err != nil {
panic("handle middleware is not activated (no session cookie)")
}
s, ok := sessions.Load(cookie.Value)
if !ok {
panic("handle middleware is not activated (no handle)")
}
ses := s.(*session)
return ses.handle.kdb, ses.handle.keySet
}
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.Method, r.RequestURI)
next.ServeHTTP(w, r)
})
}