middleware

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package middleware provides composable HTTP middleware for use with net/http. It includes utilities for panic recovery, access logging, and middleware chaining.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AccessLogger

func AccessLogger(logger *slog.Logger, prefix string) func(h http.Handler) http.Handler

AccessLogger returns middleware that logs request and response details. If logger is nil, slog.Default() is used.

Fields logged include remote address, method, URL, protocol, response status, user agent, and request duration.

func Queue

func Queue(ms ...func(http.Handler) http.Handler) func(http.Handler) http.Handler

Queue composes a sequence of middleware in first-in-first-out (FIFO) order. The first middleware passed in will be the outermost, and the last will be closest to the final handler.

i.e. Queue(m1, m2)(handler) = m2(m1(handler)).

func RecoverAndHandle

func RecoverAndHandle(fallback http.Handler, logger *slog.Logger) func(h http.Handler) http.Handler

RecoverAndHandle returns middleware that recovers from panics in downstream handlers. If a panic occurs, it logs the error and stack trace using logger, then delegates to the fallback handler.

If logger is nil, slog.Default() is used.

func Stack

func Stack(ms ...func(http.Handler) http.Handler) func(http.Handler) http.Handler

Stack composes a sequence of middleware in last-in-first-out (LIFO) order. The first middleware passed in will be closest to the final handler, and the last will be the outermost.

i.e. Stack(m1, m2)(handler) = m1(m2(handler)).

Types

This section is empty.

Jump to

Keyboard shortcuts

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