mid

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package mid provides standard net/http middleware for servicekit services.

It covers the cross-cutting concerns most HTTP services need: panic recovery, trace-context injection, structured access logging, request timeouts, and body-size limits. Every middleware has the signature func(http.Handler) http.Handler (aliased as Middleware), so it composes with router.Use, router.With, and go-pkgz/routegroup. Because TraceRequest seeds a trace id and AccessLog logs through the servicekit logger, log lines carry trace_id automatically.

Ordering

Register them outermost-first. Panics goes first so it wraps everything; TraceRequest next so downstream logs and spans share the request's trace context; then AccessLog, then the limit/timeout guards:

r := router.New(appMids...)
r.Use(
	mid.Panics(log),            // recover -> 500, log stack
	mid.TraceRequest(tracer),   // extract/seed W3C trace context
	mid.AccessLog(log),         // one structured line per request
	mid.SizeLimit(1<<20),       // cap request body at 1 MiB
	mid.Timeout(5*time.Second), // cancel ctx after 5s
)

Middleware

  • Panics(log): recover from panics, log the stack, respond 500. A nil log skips logging.
  • TraceRequest(tracer): extract incoming W3C trace context and ensure a trace id is available for logging.
  • AccessLog(log): emit one structured line per request using OpenTelemetry HTTP semantic-convention field names. A nil log skips logging.
  • Timeout(d): cancel the request context after d; a non-positive d disables it (returns next unchanged).
  • SizeLimit(n): cap the request body at n bytes via http.MaxBytesReader; a non-positive n disables it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Middleware

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

Middleware is standard net/http middleware.

func AccessLog

func AccessLog(log *logger.Logger) Middleware

AccessLog logs one structured line per request. Because it logs through the servicekit logger, each line carries the request's trace_id automatically.

func Panics

func Panics(log *logger.Logger) Middleware

Panics recovers panics, logs the stack, and responds with 500.

func SizeLimit

func SizeLimit(n int64) Middleware

SizeLimit caps the request body at n bytes. A non-positive n disables it.

func Timeout

func Timeout(d time.Duration) Middleware

Timeout cancels the request context after d and (if nothing was written) lets downstream handlers observe ctx.Err. A non-positive d disables the middleware.

func TraceRequest

func TraceRequest(tracer trace.Tracer) Middleware

TraceRequest seeds the request context with trace context extracted from incoming headers and ensures a trace id is available for logging.

Jump to

Keyboard shortcuts

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