2016-10-20 20:30:53 +02:00
|
|
|
+++
|
2016-11-20 08:13:05 +02:00
|
|
|
title = "Middleware"
|
|
|
|
description = "Middleware"
|
|
|
|
type = "middleware"
|
2016-11-21 00:16:22 +02:00
|
|
|
[menu.main]
|
|
|
|
name = "Middleware"
|
|
|
|
pre = "<i class='fa fa-filter'></i>"
|
|
|
|
weight = 2
|
|
|
|
identifier = "middleware"
|
|
|
|
url = "/middleware"
|
2016-10-20 20:30:53 +02:00
|
|
|
+++
|
|
|
|
|
2016-11-20 08:13:05 +02:00
|
|
|
## Overview
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
Middleware is a function chained in the HTTP request-response cycle with access
|
|
|
|
to `Echo#Context` which it uses to perform a specific action, for example, logging
|
|
|
|
every request or limiting the number of requests.
|
|
|
|
|
|
|
|
Handler is processed in the end after all middleware are finished executing.
|
|
|
|
|
2016-11-20 08:13:05 +02:00
|
|
|
## Levels
|
2016-10-20 20:30:53 +02:00
|
|
|
|
2016-11-20 08:13:05 +02:00
|
|
|
### Root Level (Before router)
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
`Echo#Pre()` can be used to register a middleware which is executed before router
|
|
|
|
processes 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 the
|
|
|
|
route.
|
|
|
|
|
|
|
|
The following built-in middleware should be registered at this level:
|
|
|
|
|
|
|
|
- HTTPSRedirect
|
|
|
|
- HTTPSWWWRedirect
|
|
|
|
- WWWRedirect
|
|
|
|
- NonWWWRedirect
|
|
|
|
- AddTrailingSlash
|
|
|
|
- RemoveTrailingSlash
|
|
|
|
- MethodOverride
|
|
|
|
|
|
|
|
> As router has not processed the request, middleware at this level won't
|
|
|
|
have access to any path related API from `echo.Context`.
|
|
|
|
|
2016-11-20 08:13:05 +02:00
|
|
|
### Root Level (After router)
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
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 access
|
|
|
|
to `echo.Context` API.
|
|
|
|
|
|
|
|
The following built-in middleware should be registered at this level:
|
|
|
|
|
|
|
|
- BodyLimit
|
|
|
|
- Logger
|
|
|
|
- Gzip
|
|
|
|
- Recover
|
|
|
|
- BasicAuth
|
|
|
|
- JWTAuth
|
|
|
|
- Secure
|
|
|
|
- CORS
|
|
|
|
- Static
|
|
|
|
|
2016-11-20 08:13:05 +02:00
|
|
|
### Group Level
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
When creating a new group, you can register middleware just for that group. For
|
|
|
|
example, you can have an admin group which is secured by registering a BasicAuth
|
|
|
|
middleware for it.
|
|
|
|
|
|
|
|
*Usage*
|
|
|
|
|
|
|
|
```go
|
|
|
|
e := echo.New()
|
|
|
|
admin := e.Group("/admin", middleware.BasicAuth())
|
|
|
|
```
|
|
|
|
|
|
|
|
You can also add a middleware after creating a group via `admin.Use()`.
|
|
|
|
|
2016-11-20 08:13:05 +02:00
|
|
|
### Route Level
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
When defining a new route, you can optionally register middleware just for it.
|
|
|
|
|
|
|
|
*Usage*
|
|
|
|
|
|
|
|
```go
|
|
|
|
e := echo.New()
|
|
|
|
e.GET("/", <Handler>, <Middleware...>)
|
|
|
|
```
|
|
|
|
|
2016-11-20 08:13:05 +02:00
|
|
|
## Skipping Middleware
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
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*
|
|
|
|
|
|
|
|
```go
|
|
|
|
e := echo.New()
|
|
|
|
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
|
|
|
|
Skipper: func(c echo.Context) bool {
|
|
|
|
if strings.HasPrefix(c.Request().Host(), "localhost") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
```
|
|
|
|
|
|
|
|
Example above skips Logger middleware when request host starts with localhost.
|
|
|
|
|
2016-11-20 08:13:05 +02:00
|
|
|
## [Writing Custom Middleware]({{< ref "recipes/middleware.md">}})
|