chiMiddleware

package
v1.21.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2026 License: Apache-2.0, MIT Imports: 18 Imported by: 0

README

chiMiddleware

chiMiddleware contains middleware helpers intended for use with go-chi/chi.

Middleware

  • HandleHTMLMinification() func(http.Handler) http.Handler
    • Minifies HTML responses when Content-Type contains text/html.
    • Uses tdewolff/minify.
  • HandleHTTPMethodConversion(next http.Handler) http.Handler
    • Converts incoming POST/GET requests to PUT or DELETE if the form contains _method=PUT or _method=DELETE.
  • HandleBrotli(next http.Handler) http.Handler
    • Adds Brotli compression support to responses.
  • InjectUserContext(next http.Handler, uid, username string) http.Handler
    • Injects user ID and username into the request context.
  • InjectWorkerPoolIntoContext(next http.Handler, workerPool pond.Pool) http.Handler
    • Injects a worker pool into the request context.

Example (chi)

import (
    "net/http"

    "github.com/go-chi/chi/v5"
    "github.com/mt1976/frantic-core/chiMiddleware"
)

func router() http.Handler {
    r := chi.NewRouter()

    r.Use(chiMiddleware.HandleHTMLMinification())
    r.Use(chiMiddleware.HandleHTTPMethodConversion)

    return r
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrClientAborted = fmt.Errorf("request aborted: client disconnected before response was sent")

Functions

func CURL added in v1.21.0

func CURL(req *http.Request, reqBody string) string

CURL returns a curl command for the given request and body.

func HandleBrotli

func HandleBrotli(next http.Handler) http.Handler

func HandleHTMLMinification

func HandleHTMLMinification() func(http.Handler) http.Handler

HandleHTMLMinification returns a chi middleware that minifies HTML responses

func HandleHTTPMethodConversion

func HandleHTTPMethodConversion(next http.Handler) http.Handler

handleHTTPMethodConversion is a middleware component that checks for the combination of a POST method with a form field named _method having a value of PUT. POST method with a form field named _method having a value of DELETE. It converts the request method to PUT or DELETE accordingly. This middleware should be used before any handlers that require PUT or DELETE methods to function correctly. It is typically used in web applications that need to support RESTful operations via HTML forms. It is designed to be used with the Chi router, but can be adapted for other routers It should run early in the middleware stack.

func InjectUserContext added in v1.20.0

func InjectUserContext(next http.Handler, uid string, username string) http.Handler

func InjectWorkerPoolIntoContext added in v1.20.0

func InjectWorkerPoolIntoContext(next http.Handler, workerPool pond.Pool) http.Handler

func RequestLogger added in v1.21.0

func RequestLogger(logger *log.Logger, o *Options) func(http.Handler) http.Handler

func SetAttrs added in v1.21.0

func SetAttrs(ctx context.Context, attrs ...string)

SetAttrs sets string attributes on the request log.

func SetError added in v1.21.0

func SetError(ctx context.Context, err error) error

SetError sets the error attribute on the request log.

Types

type Options added in v1.21.0

type Options struct {
	// RecoverPanics recovers from panics occurring in the underlying HTTP handlers
	// and middlewares and returns HTTP 500 unless response status was already set.
	//
	// NOTE: Panics are logged as errors automatically, regardless of this setting.
	RecoverPanics bool

	// Skip is an optional predicate function that determines whether to skip
	// recording logs for a given request.
	//
	// If nil, all requests are recorded.
	// If provided, requests where Skip returns true will not be recorded.
	Skip func(req *http.Request, respStatus int) bool

	// LogRequestHeaders is a list of headers to be logged.
	// If not provided, the default is ["Content-Type", "Origin"].
	//
	// WARNING: Do not leak any request headers with sensitive information.
	LogRequestHeaders []string

	// LogRequestBody is an optional predicate function that controls logging of request body.
	//
	// If the function returns true, the request body will be logged.
	// If false, no request body will be logged.
	//
	// WARNING: Do not leak any request bodies with sensitive information.
	LogRequestBody func(req *http.Request) bool

	// LogResponseHeaders controls a list of headers to be logged.
	//
	// If not provided, there are no default headers.
	LogResponseHeaders []string

	// LogResponseBody is an optional predicate function that controls logging of response body.
	//
	// If the function returns true, the response body will be logged.
	// If false, no response body will be logged.
	//
	// WARNING: Do not leak any response bodies with sensitive information.
	LogResponseBody func(req *http.Request) bool

	// LogBodyContentTypes defines a list of body Content-Types that are safe to be logged
	// with LogRequestBody or LogResponseBody options.
	//
	// If not provided, the default is ["application/json", "application/xml", "text/plain", "text/csv", "application/x-www-form-urlencoded", ""].
	LogBodyContentTypes []string

	// LogBodyMaxLen defines the maximum length of the body to be logged.
	//
	// If not provided, the default is 1024 bytes. Set to -1 to log the full body.
	LogBodyMaxLen int

	// LogExtraAttrs is an optional function that lets you add extra string attributes
	// to the request log.
	//
	// WARNING: Be careful not to leak any sensitive information in the logs.
	LogExtraAttrs func(req *http.Request, reqBody string, respStatus int) []string
}

Jump to

Keyboard shortcuts

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