middleware

package
v0.5.12 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package middleware provides the HTTP middleware shared by every route on the Schedularr API server: request-id propagation, structured request logging, panic recovery, and bearer-token authentication. internal/api/router.go wires these into the chi router built from internal/api/gen.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BearerAuth

func BearerAuth(token string) (func(http.Handler) http.Handler, error)

BearerAuth returns middleware that requires every request to carry "Authorization: Bearer <token>" matching token, and errors if token is shorter than 32 characters.

The presented token is never compared to the configured one directly: both are hashed with SHA-256 first and the digests are compared with crypto/subtle.ConstantTimeCompare, so a wrong guess cannot be timed to learn how many leading characters it got right. A missing or non-matching token gets a 401 application/problem+json response.

func Logging

func Logging(l *slog.Logger) func(http.Handler) http.Handler

Logging returns middleware that logs one structured line per request to l after the handler completes, with method, path, status, duration_ms, and request_id keys. It should sit inside RequestID (so a request id is already in context) and outside Recovery (so a recovered panic's 500 status is reflected in the logged line).

func Recovery

func Recovery(l *slog.Logger) func(http.Handler) http.Handler

Recovery returns middleware that recovers from a panic anywhere in the wrapped handler chain, logs it (with a stack trace) to l, and writes a 500 problem+json response instead of letting the panic take down the server process.

func RequestID

func RequestID(next http.Handler) http.Handler

RequestID returns middleware that generates a fresh request identifier for every request, sets it on the response as X-Request-Id, and stores it in the request context so downstream handlers — and api.WriteProblem, via RequestIDFrom — can read it back.

An inbound X-Request-Id header is never trusted: a caller could use one to spoof or correlate log entries across requests it does not own, so a new id is always generated regardless of what the client sent.

func RequestIDFrom

func RequestIDFrom(ctx context.Context) string

RequestIDFrom returns the request id stored in ctx by RequestID, or "" if none is present. It shares storage with problem.ContextWithRequestID / problem.RequestIDFromContext, so this is what problem.WriteProblem (and api.WriteProblem, which forwards to it) effectively reads from when it populates Problem.RequestID.

Types

This section is empty.

Jump to

Keyboard shortcuts

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