Categorygithub.com/AlphaOne1/midgard
modulepackage
0.0.0-20241222002135-c9d1db9af773
Repository: https://github.com/alphaone1/midgard.git
Documentation: pkg.go.dev

# README

Logo
Test Pipeline Result CodeQL Pipeline Result Security Pipeline Result Go Report Card Code Coverage OpenSSF Best Practises OpenSSF Scorecard FOSSA Status FOSSA Status GoDoc 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 middlware.

type Middleware func(http.Handler) http.Handler

To ease the pain of stacking different middlwares midgard offsers 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{
        util.Must(correlation.New()),
        util.Must(access_log.New(
            access_log.WithLogLevel(slog.LevelDebug))),
        util.Must(cors.New(
            cors.WithHeaders(cors.MinimumAllowedHeaders()),
            cors.WithMethods([]string{http.MethodGet}),
            cors.WithOrigins([]string{"*"}))),
        util.Must(method_filter.New(
            method_filter.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{
        util.Must(correlation.New()),
        util.Must(access_log.New(
            access_log.WithLogLevel(slog.LevelDebug))),
        util.Must(cors.New(
            cors.WithHeaders(cors.MinimumAllowedHeaders()),
            cors.WithMethods([]string{http.MethodGet}),
            cors.WithOrigins([]string{"*"}))),
        util.Must(method_filter.New(
            method_filter.WithMethods([]string{http.MethodGet}))),
    })

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

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

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

# Packages

No description provided by the author
Copyright the midgard contributors.
No description provided by the author
No description provided by the author

# Functions

StackMiddleware stacks the given middleware slice to generate a single combined middleware.
StackMiddlewareHandler calls StackMiddleware on mw and applies it to the handler final.