-
Notifications
You must be signed in to change notification settings - Fork 49
/
httpcache.go
67 lines (54 loc) · 1.71 KB
/
httpcache.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
package roadrunner
import (
"net/http"
"github.com/darkweak/souin/pkg/middleware"
"github.com/roadrunner-server/errors"
"go.uber.org/zap"
)
const pluginName string = "cache"
type (
// Configurer interface used to parse yaml configuration.
// Implementation will be provided by the RoadRunner automatically via Init method.
Configurer interface {
// Get used to get config section
Get(name string) any
// Has checks if config section exists.
Has(name string) bool
}
// Logger is the main RR's logger interface
// It's providing a named instance of the *zap.Logger
Logger interface {
NamedLogger(name string) *zap.Logger
}
Plugin struct {
*middleware.SouinBaseHandler
}
)
// Name is the plugin name
func (*Plugin) Name() string {
return pluginName
}
// Init allows the user to set up an HTTP cache system,
// RFC-7234 compliant and supports the tag based cache purge,
// distributed and not-distributed storage, key generation tweaking.
func (m *Plugin) Init(cfg Configurer, log Logger) error {
const op = errors.Op("httpcache_middleware_init")
if !cfg.Has(configurationKey) {
return errors.E(op, errors.Disabled)
}
c := parseConfiguration(cfg)
c.SetLogger(log.NamedLogger(pluginName))
m.SouinBaseHandler = middleware.NewHTTPCacheHandler(&c)
return nil
}
// Middleware is the request entrypoint to determine if either a cached
// response can be reused or if the roundtrip response can be stored in
// the cache system.
func (m *Plugin) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, rq *http.Request) {
_ = m.SouinBaseHandler.ServeHTTP(rw, rq, func(w http.ResponseWriter, r *http.Request) error {
next.ServeHTTP(w, r)
return nil
})
})
}