Documentation ¶
Overview ¶
Package middleware provides middleware used by httphandle.
Index ¶
- Constants
- Variables
- func ApplyGlobal(h http.Handler, l *slog.Logger, options GlobalOptions) http.Handler
- func ApplyGlobalDefaults(h http.Handler, l *slog.Logger) http.Handler
- func EncodeGzip(next http.Handler) http.Handler
- func RequestUUID(next http.Handler) http.Handler
- func Wrap(handler http.Handler, middleware ...Middleware) http.Handler
- func WriteErrorBody(ctx context.Context, code int, message string, writer http.ResponseWriter)
- type CacheControlOptions
- type GlobalOptions
- type Middleware
Constants ¶
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 ¶
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 ¶
ApplyGlobal applies global middleware to a handler.
func ApplyGlobalDefaults ¶
ApplyGlobalDefaults applies global middleware to a handler with default options.
func EncodeGzip ¶
EncodeGzip is a middleware that encodes the response body with gzip if the client accepts it.
func RequestUUID ¶
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 ¶
WriteErrorBody writes an error body to the response writer.
Types ¶
type CacheControlOptions ¶
type GlobalOptions ¶
GlobalOptions are the options for global middleware.
type Middleware ¶
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.