Something went wrong. Try again.
forked from darkweak/souin
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970package roadrunner
import ( "net/http"
"github.com/darkweak/souin/pkg/middleware" "github.com/darkweak/souin/plugins/souin/storages" "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 namefunc (*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).Sugar())
storages.InitFromConfiguration(&c) 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 }) })}