2016-10-20 20:30:53 +02:00
|
|
|
+++
|
2016-11-17 08:46:00 +02:00
|
|
|
title = "Routing"
|
2016-10-20 20:30:53 +02:00
|
|
|
description = "Routing HTTP request in Echo"
|
|
|
|
[menu.side]
|
|
|
|
name = "Routing"
|
|
|
|
parent = "guide"
|
|
|
|
weight = 4
|
|
|
|
+++
|
|
|
|
|
2016-11-20 08:13:05 +02:00
|
|
|
Echo's router is based on [radix tree](http://en.wikipedia.org/wiki/Radix_tree) makings
|
|
|
|
route lookup really fast, it leverages [sync pool](https://golang.org/pkg/sync/#Pool)
|
2016-10-20 20:30:53 +02:00
|
|
|
to reuse memory and achieve zero dynamic memory allocation with no GC overhead.
|
|
|
|
|
|
|
|
Routes can be registered by specifying HTTP method, path and a matching handler.
|
|
|
|
For example, code below registers a route for method `GET`, path `/hello` and a
|
|
|
|
handler which sends `Hello, World!` HTTP response.
|
|
|
|
|
|
|
|
```go
|
|
|
|
// Handler
|
|
|
|
func hello(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "Hello, World!")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Route
|
|
|
|
e.GET("/hello", hello)
|
|
|
|
```
|
|
|
|
|
|
|
|
You can use `Echo.Any(path string, h Handler)` to register a handler for all HTTP methods.
|
|
|
|
If you want to register it for some methods use `Echo.Match(methods []string, path string, h Handler)`.
|
|
|
|
|
|
|
|
Echo defines handler function as `func(echo.Context) error` where `echo.Context` primarily
|
|
|
|
holds HTTP request and response interfaces.
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Match-any
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
Matches zero or more characters in the path. For example, pattern `/users/*` will
|
|
|
|
match:
|
|
|
|
|
|
|
|
- `/users/`
|
|
|
|
- `/users/1`
|
|
|
|
- `/users/1/files/1`
|
|
|
|
- `/users/anything...`
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Path matching order
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
- Static
|
|
|
|
- Param
|
|
|
|
- Match any
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
### Example
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
```go
|
|
|
|
e.GET("/users/:id", func(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "/users/:id")
|
|
|
|
})
|
|
|
|
|
|
|
|
e.GET("/users/new", func(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "/users/new")
|
|
|
|
})
|
|
|
|
|
|
|
|
e.GET("/users/1/files/*", func(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "/users/1/files/*")
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
Above routes would resolve in the following order:
|
|
|
|
|
|
|
|
- `/users/new`
|
|
|
|
- `/users/:id`
|
|
|
|
- `/users/1/files/*`
|
|
|
|
|
|
|
|
> Routes can be written in any order.
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## Group
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
`Echo#Group(prefix string, m ...Middleware) *Group`
|
|
|
|
|
|
|
|
Routes with common prefix can be grouped to define a new sub-router with optional
|
|
|
|
middleware. In addition to specified middleware group also inherits parent middleware.
|
|
|
|
To add middleware later in the group you can use `Group.Use(m ...Middleware)`.
|
|
|
|
Groups can also be nested.
|
|
|
|
|
|
|
|
In the code below, we create an admin group which requires basic HTTP authentication
|
|
|
|
for routes `/admin/*`.
|
|
|
|
|
|
|
|
```go
|
|
|
|
g := e.Group("/admin")
|
|
|
|
g.Use(middleware.BasicAuth(func(username, password string) bool {
|
|
|
|
if username == "joe" && password == "secret" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}))
|
|
|
|
```
|
|
|
|
|
2016-11-17 08:46:00 +02:00
|
|
|
## URI building
|
2016-10-20 20:30:53 +02:00
|
|
|
|
|
|
|
`Echo.URI` can be used to generate URI for any handler with specified path parameters.
|
|
|
|
It's helpful to centralize all your URI patterns which ease in refactoring your
|
|
|
|
application.
|
|
|
|
|
|
|
|
`e.URI(h, 1)` will generate `/users/1` for the route registered below
|
|
|
|
|
|
|
|
```go
|
|
|
|
// Handler
|
|
|
|
h := func(c echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "OK")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Route
|
|
|
|
e.GET("/users/:id", h)
|
|
|
|
```
|