-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
58 lines (51 loc) · 1.19 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
package session
import (
"net/http"
"github.com/enorith/http/content"
"github.com/enorith/http/contracts"
"github.com/enorith/http/pipeline"
"github.com/enorith/session"
)
type Middleware struct {
manager *session.Manager
id string
cookieName string
maxLifeTime int
domain string
path string
secure bool
httpOnly bool
sameSite http.SameSite
}
func (m Middleware) Handle(r contracts.RequestContract, next pipeline.PipeHandler) contracts.ResponseContract {
err := m.manager.Start(m.id)
if err != nil {
return content.ErrResponseFromError(err, 500, nil)
}
ses := m.manager.Get(m.id)
hp := make(History, 0)
ses.Get(hp.SessionKey(), &hp)
path := string(r.GetUri())
if hp.Latest() != path {
hp = append(hp, string(r.GetUri()))
if len(hp) > MaxHistoryPath {
hp = hp[1:]
}
ses.Set(hp.SessionKey(), hp)
}
resp := next(r)
if rc, ok := resp.(contracts.WithResponseCookies); ok {
rc.SetCookie(&http.Cookie{
Name: m.cookieName,
Value: m.id,
MaxAge: m.maxLifeTime,
Domain: m.domain,
Path: m.path,
Secure: m.secure,
HttpOnly: m.httpOnly,
SameSite: m.sameSite,
})
}
m.manager.Save(m.id)
return resp
}