middleware

package
v0.0.0-...-c8c7e02 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package middleware provides HTTP handler useful decorators.

Index

Constants

This section is empty.

Variables

View Source
var AccessLevel slog.Level = slog.LevelError + 4

AccessLevel is the log level used for access logs.

Functions

func AccessLog

func AccessLog(next http.Handler, logger *slog.Logger, callback AccessRequestCallbeck) http.Handler

AccessLog is a decorator/middleware that extracts/ads a correlation id from/to request/response.

func CorrelationID

func CorrelationID(next http.Handler, makeCorrelationID xtransport.CorrelationIDFactory) http.Handler

CorrelationID is a decorator/middleware that extracts/ads a correlation id from/to request/response.

func Recover

func Recover(next http.Handler, logger *slog.Logger) http.Handler

Recover is a decorator/middleware that gracefully logs any panic occurred while serving a request.

Types

type AccessRequestCallbeck

type AccessRequestCallbeck func(r *http.Request) bool

AccessRequestCallback is a function type through which you can specify a callback to to indicate whether a request should be logged or not, and to modify the request before logging.

Usage example:

func myAccessRequestCallback(r *http.Request) bool {
    if r.URL.Path == "/health" {
        return false // skip logging for health endpoint
    }

    if r.PathValue("secretToken") != "" {
        // obfuscate secret token in the request path
        newPath := strings.ReplaceAll(r.URL.Path, r.PathValue("secretToken"), r.PathValue("secretToken")[:4]+"****")
        r.URL.Path = newPath
    }

    return true // log the request
}

Jump to

Keyboard shortcuts

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