middleware

package
v0.0.0-...-52a538d Latest Latest
Warning

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

Go to latest
Published: Sep 6, 2025 License: MIT Imports: 23 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ACT_app_json

func ACT_app_json(next http.Handler) http.HandlerFunc

Only allow the content type application/json

Use AllowContentType, to construct custom allowed list of content types

func ACT_app_x_www_form_urlencoded

func ACT_app_x_www_form_urlencoded(next http.Handler) http.HandlerFunc

Only allow the content type application/x-www-form-urlencoded

Use AllowContentType, to construct custom allowed list of content types

func ACT_multipart_form_data

func ACT_multipart_form_data(next http.Handler) http.HandlerFunc

Only allow the content type multipart/form-data

Use AllowContentType, to construct custom allowed list of content types

func AllowContentType

func AllowContentType(contentTypes ...string) func(http.Handler) http.HandlerFunc

AllowContentType enforces a whitelist of request Content-Types otherwise responds with a 415 Unsupported Media Type status.

func CSRFProtection

func CSRFProtection(trustedOrigin ...string) func(next http.Handler) http.HandlerFunc

func Cors

func Cors(options cors.Options) func(next http.Handler) http.HandlerFunc

func Heartbeat

func Heartbeat(next http.Handler) http.HandlerFunc

func If

func If(con bool, mw func(http.Handler) http.HandlerFunc) func(http.Handler) http.HandlerFunc

func LocalizerInjector

func LocalizerInjector(next http.Handler) http.HandlerFunc

func Maybe

func Maybe(mw func(http.Handler) http.HandlerFunc, maybeFn func(r *http.Request) bool) func(http.Handler) http.HandlerFunc

Maybe middleware will allow you to change the flow of the middleware stack execution depending on return value of maybeFn(request). This is useful for example if you'd like to skip a middleware handler if a request does not satisfy the maybeFn logic.

func MiddlewareChain

func MiddlewareChain(h http.HandlerFunc, m ...Middleware) http.HandlerFunc

Wrappe an endpoint with a chain of middlewares

e.g:

middleware.MiddlewareChain(
	CreateNewBook, // <-- wrapped by the next middlewares:
	middleware.ACT_app_x_www_form_urlencoded, // <-- First middleware to be run
	middleware.Cors, // <-- Second
	middleware.Auth, // <-- Third
	middleware.RateLimiter, // <-- Fourth, and last
)

func NoCache

func NoCache(h http.Handler) http.HandlerFunc

NoCache is a simple piece of middleware that sets a number of HTTP headers to prevent a router (or subrouter) from being cached by an upstream proxy and/or client.

As per http://wiki.nginx.org/HttpProxyModule - NoCache sets:

Expires: Thu, 01 Jan 1970 00:00:00 UTC
Cache-Control: no-cache, private, max-age=0
X-Accel-Expires: 0
Pragma: no-cache (for HTTP/1.0 proxies/clients)

func PathRewrite

func PathRewrite(old, new string) func(http.Handler) http.HandlerFunc

PathRewrite is a simple middleware which allows you to rewrite the request URL path.

func RateLimiter

func RateLimiter(limitKeyFn func(r *http.Request) (string, error), limiter ratelimiter.Limiter) func(next http.Handler) http.HandlerFunc

func RealIp

func RealIp(trustedIpHeaders ...string) func(next http.Handler) http.HandlerFunc

func RealIpFromRequest

func RealIpFromRequest(r *http.Request, trustedIpHeaders ...string) string

func Recoverer

func Recoverer(next http.Handler) http.HandlerFunc

Recoverer is a middleware that recovers from panics, logs the panic (and a backtrace), and returns a HTTP 500 (Internal Server Error) status if possible. Recoverer prints a request ID if one is provided.

func RequestLogger

func RequestLogger(next http.Handler) http.HandlerFunc

func RequestLoggerWithHeaderSkipFn

func RequestLoggerWithHeaderSkipFn(keepHeader func(headerName string) bool) func(http.Handler) http.HandlerFunc

func RequestSize

func RequestSize(bytes int64) func(http.Handler) http.HandlerFunc

RequestSize is a middleware that will limit request sizes to a specified number of bytes. It uses MaxBytesReader to do so.

func RequestUUIDMiddleware

func RequestUUIDMiddleware(next http.Handler) http.HandlerFunc

func SetHeader

func SetHeader(key, value string) func(http.Handler) http.HandlerFunc

SetHeader is a convenience handler to set a response header key/value

func StripSlashes

func StripSlashes(next http.Handler) http.HandlerFunc

StripSlashes is a middleware that will match request paths with a trailing slash, strip it from the path and continue routing through the mux, if a route matches, then it will serve the handler.

func Sunset

func Sunset(sunsetAt time.Time, links ...string) func(http.Handler) http.HandlerFunc

Sunset set Deprecation/Sunset header to response This can be used to enable Sunset in a route or a route group NOTE: the sunsetAt Time should be in UTC time zone For more: https://www.rfc-editor.org/rfc/rfc8594.html

func Throttle

func Throttle(limit int) func(http.Handler) http.HandlerFunc

Throttle is a middleware that limits number of currently processed requests at a time across all users. Note: Throttle is not a rate-limiter per user, instead it just puts a ceiling on the number of current in-flight requests being processed from the point from where the Throttle middleware is mounted.

func ThrottleBacklog

func ThrottleBacklog(limit, backlogLimit int, backlogTimeout time.Duration) func(http.Handler) http.HandlerFunc

ThrottleBacklog is a middleware that limits number of currently processed requests at a time and provides a backlog for holding a finite number of pending requests.

func ThrottleWithOpts

func ThrottleWithOpts(opts ThrottleOpts) func(http.Handler) http.HandlerFunc

ThrottleWithOpts is a middleware that limits number of currently processed requests using passed ThrottleOpts.

func Timeout

func Timeout(d time.Duration) func(next http.Handler) http.HandlerFunc

Timeout is a middleware that cancels ctx after a given timeout and return a 504 Gateway Timeout error to the client.

It's required that you select the ctx.Done() channel to check for the signal if the context has reached its deadline and return, otherwise the timeout signal will be just ignored.

ie. a route/handler may look like:

r.Get("/long", func(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()
	processTime := time.Duration(rand.Intn(4)+1) * time.Second

	select {
	case <-ctx.Done():
		return

	case <-time.After(processTime):
		// The above channel simulates some hard work.
	}

	w.Write([]byte("done"))
})

func WithValue

func WithValue(key, val any) func(next http.Handler) http.HandlerFunc

WithValue is a middleware that sets a given key/value in a context chain.

Types

type Middleware

type Middleware func(http.Handler) http.HandlerFunc

type ThrottleOpts

type ThrottleOpts struct {
	RetryAfterFn   func(ctxDone bool) time.Duration
	Limit          int
	BacklogLimit   int
	BacklogTimeout time.Duration
	StatusCode     int
}

ThrottleOpts represents a set of throttling options.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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