middleware

package
v0.5.8 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: Apache-2.0 Imports: 14 Imported by: 1

Documentation

Overview

Package middleware provides middleware used by httphandle.

Index

Constants

View Source
const (
	// DefaultTimeout is the default timeout for the context.
	DefaultTimeout = 10 * time.Second
	// DefaultMaxReqSize is the default maximum request size.
	DefaultMaxReqSize = 10 * 1024 * 1024 // 10 MB
	// FieldKeyMethod is the key for the HTTP method.
	FieldKeyMethod = "method"
	// FieldKeyReqUUID is the key for the request UUID.
	FieldKeyReqUUID = "reqUUID"
	// FieldKeyURL is the key for the URL.
	FieldKeyURL = "url"
)

Variables

View Source
var (
	// GlobalDefaults are the default options for global middleware.
	GlobalDefaults = GlobalOptions{
		MaxReqSize: DefaultMaxReqSize,
		ReqTimeout: DefaultTimeout,
	}
	// CacheDefaults are the default options for cache middleware.
	CacheDefaults = CacheControlOptions{
		MaxAge:  30 * time.Minute,
		NoCache: false,
		NoStore: false,
		Public:  true,
	}
	// CacheControlStatic is the default Cache-Control middleware.
	CacheControlStatic = CreateCacheControl(CacheDefaults)
)

Functions

func ApplyGlobal

func ApplyGlobal(h http.Handler, l *slog.Logger, options GlobalOptions) http.Handler

ApplyGlobal applies global middleware to a handler.

func ApplyGlobalDefaults

func ApplyGlobalDefaults(h http.Handler, l *slog.Logger) http.Handler

ApplyGlobalDefaults applies global middleware to a handler with default options.

func EncodeGzip

func EncodeGzip(next http.Handler) http.Handler

EncodeGzip is a middleware that encodes the response body with gzip if the client accepts it.

func RequestUUID

func RequestUUID(next http.Handler) http.Handler

RequestUUID is a middleware that adds a request UUID to the request.

func Wrap

func Wrap(handler http.Handler, middleware ...Middleware) http.Handler

Wrap wraps a handler with multiple middleware. The middleware is applied in the order it is passed in.

func WriteErrorBody

func WriteErrorBody(ctx context.Context, code int, message string, writer http.ResponseWriter)

WriteErrorBody writes an error body to the response writer.

Types

type CacheControlOptions

type CacheControlOptions struct {
	MaxAge  time.Duration
	NoCache bool
	NoStore bool
	Public  bool
}

type GlobalOptions

type GlobalOptions struct {
	MaxReqSize uint32
	ReqTimeout time.Duration
}

GlobalOptions are the options for global middleware.

type Middleware

type Middleware func(next http.Handler) http.Handler

Middleware is a function that returns a wrapped handler.

func CreateAddCtx

func CreateAddCtx(timeout time.Duration) Middleware

CreateAddCtx creates a middleware that adds a context to the request.

func CreateAddLogger

func CreateAddLogger(l *slog.Logger) Middleware

CreateAddLogger creates a middleware that adds a logger to the request.

func CreateAddTx

func CreateAddTx(begin func(ctx context.Context) (pgx.Tx, error)) Middleware

CreateAddTx creates a middleware that adds a transaction to the request.

func CreateCacheControl

func CreateCacheControl(options CacheControlOptions) Middleware

CreateCacheControl creates a middleware that adds a Cache-Control header to the response.

func CreateLimitReqSize

func CreateLimitReqSize(maxBytes int64) Middleware

CreateLimitReqSize creates a middleware that limits the size of a request.

Directories

Path Synopsis
Package ctxkey contains the context keys used by httphandle.
Package ctxkey contains the context keys used by httphandle.

Jump to

Keyboard shortcuts

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