midgard

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2025 License: MPL-2.0 Imports: 2 Imported by: 0

README

Logo
Test Pipeline Result CodeQL Pipeline Result Security Pipeline Result Go Report Card Code Coverage CodeRabbit Reviews OpenSSF Best Practises OpenSSF Scorecard REUSE compliance FOSSA License Status FOSSA Security Status Go Reference

midgard

midgard is a collection of Golang http middlewares and helper functionality to use them more elegantly.

Usage

midgard defines a type Middleware that is just a convenience to not always having to write the full definition of what is commonly known as http middleware.

type Middleware func(http.Handler) http.Handler

To ease the pain of stacking different middlewares, midgard offers two functions to facilitate it. StackMiddlewareHandler stacks the given slice of middlewares on top of each other and finally calls the given handler. It generates a new handler that has all the given middlewares prepended:

finalHandler := midgard.StackMiddlewareHandler(
    []midgard.Middleware{
        helper.Must(correlation.New()),
        helper.Must(accesslog.New(
            accesslog.WithLogLevel(slog.LevelDebug))),
        helper.Must(cors.New(
            cors.WithHeaders(cors.MinimumAllowedHeaders()),
            cors.WithMethods([]string{http.MethodGet}),
            cors.WithOrigins([]string{"*"}))),
        helper.Must(methodfilter.New(
            methodfilter.WithMethods([]string{http.MethodGet}))),
        },
    http.HandlerFunc(HelloHandler),
)

StackMiddleware does basically the same, but without having given a handler. It generates a new middleware:

newMiddleware:= midgard.StackMiddleware(
    []midgard.Middleware{
        helper.Must(correlation.New()),
        helper.Must(accesslog.New(
            accesslog.WithLogLevel(slog.LevelDebug))),
        helper.Must(cors.New(
            cors.WithHeaders(cors.MinimumAllowedHeaders()),
            cors.WithMethods([]string{http.MethodGet}),
            cors.WithOrigins([]string{"*"}))),
        helper.Must(methodfilter.New(
            methodfilter.WithMethods([]string{http.MethodGet}))),
    })

The native solution for this would be to nest the calls to the middleware like this:

finalHandler := helper.Must(correlation.New())(
                    helper.Must(accesslog.New(
                        accesslog.WithLogLevel(slog.LevelDebug)))(
                        helper.Must(cors.New(
                            cors.WithHeaders(cors.MinimumAllowedHeaders()),
                            cors.WithMethods([]string{http.MethodGet}),
                            cors.WithOrigins([]string{"*"})))(
                            helper.Must(methodfilter.New(
                                methodfilter.WithMethods([]string{http.MethodGet}))))))

As you see, depending on the number of middlewares, that can be quite confusing. Further one cannot easily dynamically add or remove middlewares.

Documentation

Overview

Package midgard provides a set of utilities for building HTTP middleware.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StackMiddleware

func StackMiddleware(mw []defs.Middleware) defs.Middleware

StackMiddleware stacks the given middleware slice to generate a single combined middleware. The middleware at index 0 is the outermost, going step by step to the innermost, e. g. mw[0](mw[1](mw[2]())).

func StackMiddlewareHandler

func StackMiddlewareHandler(mw []defs.Middleware, final http.Handler) http.Handler

StackMiddlewareHandler calls StackMiddleware on mw and applies it to the handler final.

Types

This section is empty.

Directories

Path Synopsis
Package defs contains the common types and functions for all midgard handlers.
Package defs contains the common types and functions for all midgard handlers.
Package example contains an example for midgard middleware usage.
Package example contains an example for midgard middleware usage.
handler
accesslog
Package accesslog provides a middleware that logs every request.
Package accesslog provides a middleware that logs every request.
addheader
Package addheader provides a middleware for adding headers to HTTP responses.
Package addheader provides a middleware for adding headers to HTTP responses.
basicauth
Package basicauth implements the basic auth functionality.
Package basicauth implements the basic auth functionality.
basicauth/htpasswdauth
Package htpasswdauth implements the basic auth functionality using a htpasswd file.
Package htpasswdauth implements the basic auth functionality using a htpasswd file.
basicauth/mapauth
Package mapauth implements the basic auth functionality using a user-pass-map.
Package mapauth implements the basic auth functionality using a user-pass-map.
correlation
Package correlation provides a middleware for adding correlation ids to HTTP requests.
Package correlation provides a middleware for adding correlation ids to HTTP requests.
cors
Package cors provides a middleware for handling CORS (Cross-Origin Resource Sharing) requests.
Package cors provides a middleware for handling CORS (Cross-Origin Resource Sharing) requests.
methodfilter
Package methodfilter provides a middleware for filtering HTTP requests by methods.
Package methodfilter provides a middleware for filtering HTTP requests by methods.
ratelimit
Package ratelimit provides middleware for rate limiting HTTP requests.
Package ratelimit provides middleware for rate limiting HTTP requests.
ratelimit/locallimit
Package locallimit provides a process-local rate limiter.
Package locallimit provides a process-local rate limiter.
Package helper provides utility functions for the midgard package.
Package helper provides utility functions for the midgard package.

Jump to

Keyboard shortcuts

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