Middleware

Overview

Middleware is a function chained in the HTTP request-response cycle with accessto Echo#Context which it uses to perform a specific action, for example, loggingevery request or limiting the number of requests.

Handler is processed in the end after all middleware are finished executing.

Middleware registed using Echo#Use() is only executed for paths which areregistered after Echo#Use() has been called.

Levels

Root Level (Before router)

Echo#Pre() can be used to register a middleware which is executed before routerprocesses the request. It is helpful to make any changes to the request properties,for example, adding or removing a trailing slash from the path so it matches theroute.

The following built-in middleware should be registered at this level:

  • HTTPSRedirect
  • HTTPSWWWRedirect
  • WWWRedirect
  • NonWWWRedirect
  • AddTrailingSlash
  • RemoveTrailingSlash
  • MethodOverride
  • Rewrite

As router has not processed the request, middleware at this level won’thave access to any path related API from echo.Context.

Root Level (After router)

Most of the time you will register a middleware at this level using Echo#Use().This middleware is executed after router processes the request and has full accessto echo.Context API.

The following built-in middleware should be registered at this level:

  • BodyLimit
  • Logger
  • Gzip
  • Recover
  • BasicAuth
  • JWTAuth
  • Secure
  • CORS
  • Static

Group Level

When creating a new group, you can register middleware just for that group. Forexample, you can have an admin group which is secured by registering a BasicAuthmiddleware for it.

Usage

  1. e := echo.New()
  2. admin := e.Group("/admin", middleware.BasicAuth())

You can also add a middleware after creating a group via admin.Use().

Route Level

When defining a new route, you can optionally register middleware just for it.

Usage

  1. e := echo.New()
  2. e.GET("/", <Handler>, <Middleware...>)

Skipping Middleware

There are cases when you would like to skip a middleware based on some condition,for that each middleware has an option to define a function Skipper func(c echo.Context) bool.

Usage

  1. e := echo.New()
  2. e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
  3. Skipper: func(c echo.Context) bool {
  4. if strings.HasPrefix(c.Request().Host, "localhost") {
  5. return true
  6. }
  7. return false
  8. },
  9. }))

Example above skips Logger middleware when request host starts with localhost.

Writing Custom Middleware