package middleware

Import Path
	golang.org/x/pkgsite/internal/middleware (on go.dev)

Dependency Relation
	imports 34 packages, and imported by 8 packages

Involved Source Files accept_requests.go betaRedirect.go caching.go errorreporting.go experiment.go iapheader.go Package middleware implements a simple middleware pattern for http handlers, along with implementations for some common middlewares. panic.go quota.go requestlog.go secureheaders.go stats.go timeout.go
Package-Level Type Names (total 15, in which 8 are exported)
/* sort exporteds by: | */
An Experimenter contains information about active experiments from the experiment source. Experiments returns the experiments currently in use. func NewExperimenter(ctx context.Context, pollEvery time.Duration, getter ExperimentGetter, rep Reporter) (_ *Experimenter, err error) func golang.org/x/pkgsite/cmd/internal/cmdconfig.Experimenter(ctx context.Context, cfg *config.Config, getter ExperimentGetter, reportingClient *errorreporting.Client) *Experimenter func Experiment(e *Experimenter) Middleware
ExperimentGetter is the signature of a function that gets experiments. func golang.org/x/pkgsite/cmd/internal/cmdconfig.ExperimentGetter(ctx context.Context, cfg *config.Config) ExperimentGetter func NewExperimenter(ctx context.Context, pollEvery time.Duration, getter ExperimentGetter, rep Reporter) (_ *Experimenter, err error) func golang.org/x/pkgsite/internal/queue.New(ctx context.Context, cfg *config.Config, queueName string, numWorkers int, expGetter ExperimentGetter, processFunc queue.inMemoryProcessFunc) (queue.Queue, error) func golang.org/x/pkgsite/cmd/internal/cmdconfig.Experimenter(ctx context.Context, cfg *config.Config, getter ExperimentGetter, reportingClient *errorreporting.Client) *Experimenter
An Expirer computes the TTL that should be used when caching a page. func TTL(ttl time.Duration) Expirer func Cache(name string, client *redis.Client, expirer Expirer, authValues []string) Middleware
LocalLogger is a logger that can be used when running locally (i.e.: not on GCP) Log implements the Logger interface via our internal log package. T : Logger
Logger is the interface used to write request logs to GCP. ( T) Log(logging.Entry) LocalLogger *cloud.google.com/go/logging.Logger func golang.org/x/pkgsite/cmd/internal/cmdconfig.Logger(ctx context.Context, cfg *config.Config, logName string) Logger func RequestLog(lg Logger) Middleware
A Middleware is a func that wraps an http.Handler. func AcceptRequests(methods ...string) Middleware func BetaPkgGoDevRedirect() Middleware func Cache(name string, client *redis.Client, expirer Expirer, authValues []string) Middleware func Chain(middlewares ...Middleware) Middleware func ErrorReporting(report func(errorreporting.Entry)) Middleware func Experiment(e *Experimenter) Middleware func Identity() Middleware func Panic(panicHandler http.Handler) Middleware func Quota(settings config.QuotaSettings, client *redis.Client) Middleware func RequestLog(lg Logger) Middleware func SecureHeaders(enableCSP bool) Middleware func Stats() Middleware func Timeout(d time.Duration) Middleware func ValidateIAPHeader(audience string) Middleware func Chain(middlewares ...Middleware) Middleware
// hash of page contents MillisToFirstByte int64 MillisToLastByte int64 Other map[string]interface{} // total size of data written // HTTP status
A Reporter sends errors to the Error-Reporting service. ( T) Report(errorreporting.Entry) *cloud.google.com/go/errorreporting.Client func NewExperimenter(ctx context.Context, pollEvery time.Duration, getter ExperimentGetter, rep Reporter) (_ *Experimenter, err error)
Package-Level Functions (total 32, in which 18 are exported)
AcceptRequests serves 405 (Method Not Allowed) for any method not on the given list and 414 (Method Request URI Too Long) for any URI that exceeds the maxURILength.
BetaPkgGoDevRedirect redirects requests from pkg.go.dev to beta.pkg.go.dev, based on whether a cookie is set for betapkggodev-redirect. The cookie can be turned on/off using a query param.
Cache returns a new Middleware that caches every request. The name of the cache is used only for metrics. The expirer is a func that is used to map a new request to its TTL. authHeader is the header key used by the cache to know that a request should bypass the cache. authValues is the set of values that could be set on the authHeader in order to bypass the cache.
Chain creates a new Middleware that applies a sequence of Middlewares, so that they execute in the given order when handling an http request. In other words, Chain(m1, m2)(handler) = m1(m2(handler)) A similar pattern is used in e.g. github.com/justinas/alice: https://github.com/justinas/alice/blob/ce87934/chain.go#L45
ElapsedStat records as a stat the elapsed time for a function execution. Invoke like so: defer ElapsedStat(ctx, "FunctionName")() The resulting stat will be called "FunctionName ms" and will be the wall-clock execution time of the function in milliseconds.
ErrorReporting returns a middleware that reports any server errors using the report func.
Experiment returns a new Middleware that sets active experiments for each incoming request.
Identity is a middleware that does nothing. It can be used as a helper when building middleware chains.
NewExperimenter returns an Experimenter for use in the middleware. The experimenter regularly polls for updates to the snapshot in the background.
Panic returns a middleware that executes panicHandler on any panic originating from the delegate handler.
Quota implements a simple IP-based rate limiter. Each set of incoming IP addresses with the same low-order byte gets settings.QPS requests per second. Information is kept in a redis instance. If a request is disallowed, a 429 (TooManyRequests) will be served.
RequestLog returns a middleware that logs each incoming requests using the given logger. This logger replaces the built-in appengine request logger, which logged PII when behind IAP, in such a way that was impossible to turn off. Logs may be viewed in Pantheon by selecting the log source corresponding to the AppEngine service name (e.g. 'dev-worker').
SecureHeaders adds a content-security-policy and other security-related headers to all responses.
SetStat sets a stat named key in the current context. If key already has a value, the old and new value are both stored in a slice.
Stats returns a Middleware that, instead of serving the page, serves statistics about the page.
Timeout returns a new Middleware that times out each request after the given duration.
TTL returns an Expirer that expires all pages after the given TTL.
ValidateIAPHeader checks that the request has a header that proves it arrived via the IAP. See https://cloud.google.com/iap/docs/signed-headers-howto#securing_iap_headers.
Package-Level Variables (total 15, in which 5 are exported)
QuotaResultCount is a counter of quota results, by whether the request was blocked or not.
To avoid test flakiness, when TestMode is true, cache writes are synchronous.
Package-Level Constants (total 7, none are exported)