Skip to content

AlphaOne1/midgard

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.

About

Golang middleware collection and tooling.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

 

Contributors 3

  •  
  •  
  •