middleware

package
v0.0.0-...-86cb477 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 25, 2024 License: BSD-3-Clause Imports: 34 Imported by: 0

Documentation

Overview

Package middleware implements a simple middleware pattern for http handlers, along with implementations for some common middlewares.

Index

Constants

This section is empty.

Variables

View Source
var (
	CacheResultCount = &view.View{
		Name:        "go-discovery/cache/result_count",
		Measure:     cacheResults,
		Aggregation: view.Count(),
		Description: "cache results, by cache name and whether it was a hit",
		TagKeys:     []tag.Key{keyCacheName, keyCacheHit},
	}
	CacheLatency = &view.View{
		Name:        "go-discovery/cache/result_latency",
		Measure:     cacheLatency,
		Description: "cache result latency, by cache name and whether it was a hit",
		Aggregation: ochttp.DefaultLatencyDistribution,
		TagKeys:     []tag.Key{keyCacheName, keyCacheHit},
	}
	CacheErrorCount = &view.View{
		Name:        "go-discovery/cache/errors",
		Measure:     cacheErrors,
		Aggregation: view.Count(),
		Description: "cache errors, by cache name",
		TagKeys:     []tag.Key{keyCacheName, keyCacheOperation},
	}

	// To avoid test flakiness, when TestMode is true, cache writes are
	// synchronous.
	TestMode = false
)
View Source
var (

	// QuotaResultCount is a counter of quota results, by whether the request was blocked or not.
	QuotaResultCount = &view.View{
		Name:        "go-discovery/quota/result_count",
		Measure:     quotaResults,
		Aggregation: view.Count(),
		Description: "quota results, by blocked or allowed",
		TagKeys:     []tag.Key{keyQuotaBlocked},
	}
)

Functions

func NewCacher

func NewCacher(client *redis.Client) *cacher

NewCacher returns a new Cacher, used for creating a middleware that caches each request.

Types

type ExperimentGetter

type ExperimentGetter func(context.Context) ([]*internal.Experiment, error)

ExperimentGetter is the signature of a function that gets experiments.

type Experimenter

type Experimenter struct {
	// contains filtered or unexported fields
}

An Experimenter contains information about active experiments from the experiment source.

func NewExperimenter

func NewExperimenter(ctx context.Context, pollEvery time.Duration, getter ExperimentGetter, rep derrors.Reporter) (_ *Experimenter, err error)

NewExperimenter returns an Experimenter for use in the middleware. The experimenter regularly polls for updates to the snapshot in the background.

func (*Experimenter) Experiments

func (e *Experimenter) Experiments() []*internal.Experiment

Experiments returns the experiments currently in use.

type Expirer

type Expirer func(r *http.Request) time.Duration

An Expirer computes the TTL that should be used when caching a page.

type LocalLogger

type LocalLogger struct{}

LocalLogger is a logger that can be used when running locally (i.e.: not on GCP)

func (LocalLogger) Log

func (l LocalLogger) Log(entry logging.Entry)

Log implements the Logger interface via our internal log package.

type Logger

type Logger interface {
	Log(logging.Entry)
}

Logger is the interface used to write request logs to GCP.

type Middleware

type Middleware func(http.Handler) http.Handler

A Middleware is a func that wraps an http.Handler.

func AcceptRequests

func AcceptRequests(methods ...string) Middleware

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.

func BetaPkgGoDevRedirect

func BetaPkgGoDevRedirect() Middleware

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.

func Chain

func Chain(middlewares ...Middleware) Middleware

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

func ErrorReporting

func ErrorReporting(reporter derrors.Reporter) Middleware

ErrorReporting returns a middleware that reports any server errors using the report func.

func Experiment

func Experiment(e *Experimenter) Middleware

Experiment returns a new Middleware that sets active experiments for each incoming request.

func GodocOrgRedirect

func GodocOrgRedirect() Middleware

GodocOrgRedirect redirects requests from godoc.org to pkg.go.dev.

func Identity

func Identity() Middleware

Identity is a middleware that does nothing. It can be used as a helper when building middleware chains.

func Panic

func Panic(panicHandler http.Handler) Middleware

Panic returns a middleware that executes panicHandler on any panic originating from the delegate handler.

func Quota

func Quota(settings config.QuotaSettings, client *redis.Client) Middleware

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.

func RequestLog

func RequestLog(lg Logger) Middleware

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').

func SecureHeaders

func SecureHeaders(enableCSP bool) Middleware

SecureHeaders adds a content-security-policy and other security-related headers to all responses.

func ValidateIAPHeader

func ValidateIAPHeader(audience string) Middleware

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL