-
Notifications
You must be signed in to change notification settings - Fork 48
/
souin.go
69 lines (61 loc) · 1.53 KB
/
souin.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
package goa
import (
"net/http"
"time"
"github.com/darkweak/souin/configurationtypes"
"github.com/darkweak/souin/pkg/middleware"
)
var (
DefaultConfiguration = middleware.BaseConfiguration{
DefaultCache: &configurationtypes.DefaultCache{
TTL: configurationtypes.Duration{
Duration: 10 * time.Second,
},
Stale: configurationtypes.Duration{
Duration: 10 * time.Second,
},
},
LogLevel: "info",
}
DevDefaultConfiguration = middleware.BaseConfiguration{
API: configurationtypes.API{
BasePath: "/souin-api",
Prometheus: configurationtypes.APIEndpoint{
Enable: true,
},
Souin: configurationtypes.APIEndpoint{
Enable: true,
},
},
DefaultCache: &configurationtypes.DefaultCache{
Regex: configurationtypes.Regex{
Exclude: "/excluded",
},
TTL: configurationtypes.Duration{
Duration: 5 * time.Second,
},
Stale: configurationtypes.Duration{
Duration: 10 * time.Second,
},
},
LogLevel: "debug",
}
)
// SouinGoaMiddleware declaration.
type SouinGoaMiddleware struct {
*middleware.SouinBaseHandler
}
func NewHTTPCache(c middleware.BaseConfiguration) func(http.Handler) http.Handler {
s := &SouinGoaMiddleware{
SouinBaseHandler: middleware.NewHTTPCacheHandler(&c),
}
return s.handle
}
func (s *SouinGoaMiddleware) handle(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, rq *http.Request) {
_ = s.SouinBaseHandler.ServeHTTP(rw, rq, func(w http.ResponseWriter, r *http.Request) error {
next.ServeHTTP(w, r)
return nil
})
})
}