Middleware

You can use a Mediator/Middleware component to augment the functionality of go-msx components such as endpoints:

Middleware is software that's assembled into an app pipeline to handle requests and responses. Each component:

  • chooses whether to pass execution to the next component in the pipeline.
  • can perform work before and after the next component in the pipeline.

-- ASP.NET Core Middleware, Microsoft

Go HTTP Middleware factories implement a de facto function signature:

type Middleware func(next http.Handler) http.Handler

func myMiddleware(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        // Middleware BEFORE logic goes here...
        next.ServeHTTP(w, r)
        // Middleware AFTER logic goes here...
    })
}

The factory accepts the subsequent Handler in the middleware chain, and returns a new Handler which wraps it with the desired added functionality. You can find more details and examples on this blog post.

Available HTTP Middleware

go-msx does not currently define any HTTP Middleware, however there are many libraries available, including: