2016-07-27 20:20:09 +02:00
|
|
|
# [Echo](http://labstack.com/echo) [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/labstack/echo) [![License](http://img.shields.io/badge/license-mit-blue.svg?style=flat-square)](https://raw.githubusercontent.com/labstack/echo/master/LICENSE) [![Build Status](http://img.shields.io/travis/labstack/echo.svg?style=flat-square)](https://travis-ci.org/labstack/echo) [![Coverage Status](http://img.shields.io/coveralls/labstack/echo.svg?style=flat-square)](https://coveralls.io/r/labstack/echo) [![Join the chat at https://gitter.im/labstack/echo](https://img.shields.io/badge/gitter-join%20chat-brightgreen.svg?style=flat-square)](https://gitter.im/labstack/echo) [![Twitter](https://img.shields.io/badge/twitter-@labstack-55acee.svg?style=flat-square)](https://twitter.com/labstack)
|
2015-06-30 21:10:35 +02:00
|
|
|
|
2016-05-14 03:55:00 +02:00
|
|
|
#### Fast and unfancy HTTP server framework for Go (Golang). Up to 10x faster than the rest.
|
2016-02-09 21:46:08 +02:00
|
|
|
|
2016-06-08 05:24:24 +02:00
|
|
|
## Feature Overview
|
2016-03-11 08:22:42 +02:00
|
|
|
|
2016-09-07 04:41:05 +02:00
|
|
|
- Optimized HTTP router which smartly prioritize routes
|
|
|
|
- Build robust and scalable RESTful APIs
|
|
|
|
- Run with standard HTTP server or FastHTTP server
|
|
|
|
- Group APIs
|
|
|
|
- Extensible middleware framework
|
|
|
|
- Define middleware at root, group or route level
|
|
|
|
- Data binding for JSON, XML and form payload
|
|
|
|
- Handy functions to send variety of HTTP responses
|
|
|
|
- Centralized HTTP error handling
|
|
|
|
- Template rendering with any template engine
|
|
|
|
- Define your format for the logger
|
|
|
|
- Highly customizable
|
2016-03-11 08:22:42 +02:00
|
|
|
|
|
|
|
## Performance
|
|
|
|
|
2016-04-03 04:32:52 +02:00
|
|
|
- Environment:
|
|
|
|
- Go 1.6
|
|
|
|
- wrk 4.0.0
|
|
|
|
- 2 GB, 2 Core (DigitalOcean)
|
|
|
|
- Test Suite: https://github.com/vishr/web-framework-benchmark
|
|
|
|
- Date: 4/4/2016
|
2016-03-11 08:22:42 +02:00
|
|
|
|
2016-09-07 21:18:57 +02:00
|
|
|
![Performance](https://i.imgur.com/fZVnK52.png)
|
2016-03-11 08:22:42 +02:00
|
|
|
|
2016-04-03 04:32:52 +02:00
|
|
|
## Quick Start
|
2016-03-11 08:22:42 +02:00
|
|
|
|
|
|
|
### Installation
|
|
|
|
|
|
|
|
```sh
|
2016-07-14 18:35:56 +02:00
|
|
|
$ go get -u github.com/labstack/echo
|
2016-03-11 08:22:42 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
### Hello, World!
|
|
|
|
|
2016-04-03 04:32:52 +02:00
|
|
|
Create `server.go`
|
2016-03-11 08:22:42 +02:00
|
|
|
|
2016-02-09 21:46:08 +02:00
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2016-03-11 08:22:42 +02:00
|
|
|
"net/http"
|
2016-11-01 17:33:56 +02:00
|
|
|
|
2016-02-09 21:46:08 +02:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
e := echo.New()
|
2016-04-19 01:59:58 +02:00
|
|
|
e.GET("/", func(c echo.Context) error {
|
2016-04-03 04:32:52 +02:00
|
|
|
return c.String(http.StatusOK, "Hello, World!")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2016-11-01 17:33:56 +02:00
|
|
|
|
|
|
|
if err := e.Start(":1323"); err != nil {
|
|
|
|
e.Logger.Fatal(err.Error())
|
|
|
|
}
|
2016-02-09 21:46:08 +02:00
|
|
|
}
|
|
|
|
```
|
2015-03-12 23:51:39 +02:00
|
|
|
|
2016-03-11 08:22:42 +02:00
|
|
|
Start server
|
2015-04-19 01:47:48 +02:00
|
|
|
|
2016-03-11 08:22:42 +02:00
|
|
|
```sh
|
2016-04-03 04:32:52 +02:00
|
|
|
$ go run server.go
|
2016-03-11 08:22:42 +02:00
|
|
|
```
|
2015-04-19 01:47:48 +02:00
|
|
|
|
2016-03-11 08:22:42 +02:00
|
|
|
Browse to [http://localhost:1323](http://localhost:1323) and you should see
|
|
|
|
Hello, World! on the page.
|
2015-04-16 19:18:35 +02:00
|
|
|
|
2016-04-03 04:32:52 +02:00
|
|
|
### Routing
|
|
|
|
|
|
|
|
```go
|
2016-04-19 01:59:58 +02:00
|
|
|
e.POST("/users", saveUser)
|
|
|
|
e.GET("/users/:id", getUser)
|
|
|
|
e.PUT("/users/:id", updateUser)
|
|
|
|
e.DELETE("/users/:id", deleteUser)
|
2016-04-03 04:32:52 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
### Path Parameters
|
|
|
|
|
|
|
|
```go
|
2016-11-08 20:19:30 +02:00
|
|
|
// e.GET("/users/:id", getUser)
|
2016-04-03 04:32:52 +02:00
|
|
|
func getUser(c echo.Context) error {
|
2016-11-08 20:19:30 +02:00
|
|
|
// User ID from path `users/:id`
|
|
|
|
id := c.Param("id")
|
|
|
|
return c.String(http.StatusOK, id)
|
2016-04-03 04:32:52 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-11-08 20:19:30 +02:00
|
|
|
Browse to http://localhost:1323/users/Joe and you should see 'Joe' on the page.
|
|
|
|
|
2016-04-03 04:32:52 +02:00
|
|
|
### Query Parameters
|
|
|
|
|
|
|
|
`/show?team=x-men&member=wolverine`
|
|
|
|
|
|
|
|
```go
|
2016-11-08 20:19:30 +02:00
|
|
|
//e.GET("/show", show)
|
2016-04-03 04:32:52 +02:00
|
|
|
func show(c echo.Context) error {
|
|
|
|
// Get team and member from the query string
|
|
|
|
team := c.QueryParam("team")
|
2016-04-05 12:39:25 +02:00
|
|
|
member := c.QueryParam("member")
|
2016-11-08 20:19:30 +02:00
|
|
|
return c.String(http.StatusOK, "team:" + team + ", member:" + member)
|
2016-04-03 04:32:52 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-11-08 20:19:30 +02:00
|
|
|
Browse to http://localhost:1323/show?team=x-men&member=wolverine and you should see 'team:x-men, member:wolverine' on the page.
|
|
|
|
|
2016-04-03 04:32:52 +02:00
|
|
|
### Form `application/x-www-form-urlencoded`
|
|
|
|
|
2016-04-05 22:09:42 +02:00
|
|
|
`POST` `/save`
|
2016-04-03 04:32:52 +02:00
|
|
|
|
|
|
|
name | value
|
|
|
|
:--- | :---
|
|
|
|
name | Joe Smith
|
|
|
|
email | joe@labstack.com
|
|
|
|
|
|
|
|
|
|
|
|
```go
|
2016-11-08 20:19:30 +02:00
|
|
|
// e.POST("/save", save)
|
2016-04-03 04:32:52 +02:00
|
|
|
func save(c echo.Context) error {
|
|
|
|
// Get name and email
|
|
|
|
name := c.FormValue("name")
|
2016-04-20 10:30:04 +02:00
|
|
|
email := c.FormValue("email")
|
2016-11-08 20:19:30 +02:00
|
|
|
return c.String(http.StatusOK, "name:" + name + ", email:" + email)
|
2016-04-03 04:32:52 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-11-08 20:19:30 +02:00
|
|
|
Run the following command:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
$ curl -F "name=Joe Smith" -F "email=joe@labstack.com" http://localhost:1323/save
|
|
|
|
// => name:Joe Smith, email:joe@labstack.com
|
|
|
|
```
|
|
|
|
|
2016-04-03 04:32:52 +02:00
|
|
|
### Form `multipart/form-data`
|
|
|
|
|
|
|
|
`POST` `/save`
|
|
|
|
|
|
|
|
name | value
|
|
|
|
:--- | :---
|
|
|
|
name | Joe Smith
|
|
|
|
avatar | avatar
|
|
|
|
|
|
|
|
```go
|
|
|
|
func save(c echo.Context) error {
|
2016-11-08 20:19:30 +02:00
|
|
|
// Get name
|
2016-04-03 04:32:52 +02:00
|
|
|
name := c.FormValue("name")
|
|
|
|
// Get avatar
|
2016-11-08 20:19:30 +02:00
|
|
|
avatar, err := c.FormFile("avatar")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Source
|
|
|
|
src, err := avatar.Open()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer src.Close()
|
|
|
|
|
|
|
|
// Destination
|
|
|
|
dst, err := os.Create(avatar.Filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dst.Close()
|
|
|
|
|
|
|
|
// Copy
|
|
|
|
if _, err = io.Copy(dst, src); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.HTML(http.StatusOK, "<b>Thank you! " + name + "</b>")
|
|
|
|
}
|
|
|
|
```
|
2016-04-03 04:32:52 +02:00
|
|
|
|
2016-11-08 20:19:30 +02:00
|
|
|
Run the following command.
|
|
|
|
```sh
|
|
|
|
$ curl -F "name=Joe Smith" -F "avatar=@/path/to/your/avatar.png" http://localhost:1323/save
|
|
|
|
// => <b>Thank you! Joe Smith</b>
|
|
|
|
```
|
|
|
|
|
|
|
|
For checking uploaded image, run the following command.
|
2016-04-08 23:47:56 +02:00
|
|
|
|
2016-11-08 20:19:30 +02:00
|
|
|
```sh
|
|
|
|
cd <project directory>
|
|
|
|
ls avatar.png
|
|
|
|
// => avatar.png
|
2016-04-03 04:32:52 +02:00
|
|
|
```
|
|
|
|
|
|
|
|
### Handling Request
|
|
|
|
|
2016-05-01 21:51:53 +02:00
|
|
|
- Bind `JSON` or `XML` or `form` payload into Go struct based on `Content-Type` request header.
|
2016-04-03 04:32:52 +02:00
|
|
|
- Render response as `JSON` or `XML` with status code.
|
|
|
|
|
|
|
|
```go
|
|
|
|
type User struct {
|
2016-05-01 21:51:53 +02:00
|
|
|
Name string `json:"name" xml:"name" form:"name"`
|
|
|
|
Email string `json:"email" xml:"email" form:"email"`
|
2016-04-03 04:32:52 +02:00
|
|
|
}
|
|
|
|
|
2016-04-19 01:59:58 +02:00
|
|
|
e.POST("/users", func(c echo.Context) error {
|
2016-04-03 04:32:52 +02:00
|
|
|
u := new(User)
|
|
|
|
if err := c.Bind(u); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return c.JSON(http.StatusCreated, u)
|
|
|
|
// or
|
|
|
|
// return c.XML(http.StatusCreated, u)
|
|
|
|
})
|
|
|
|
```
|
|
|
|
|
|
|
|
### Static Content
|
|
|
|
|
|
|
|
Server any file from static directory for path `/static/*`.
|
|
|
|
|
|
|
|
```go
|
|
|
|
e.Static("/static", "static")
|
|
|
|
```
|
|
|
|
|
2016-06-26 03:33:03 +02:00
|
|
|
##### [Learn More](https://echo.labstack.com/guide/static-files)
|
2016-04-03 04:32:52 +02:00
|
|
|
|
2016-05-24 23:51:26 +02:00
|
|
|
### [Template Rendering](https://echo.labstack.com/guide/templates)
|
2016-04-03 04:32:52 +02:00
|
|
|
|
|
|
|
### Middleware
|
|
|
|
|
|
|
|
```go
|
|
|
|
// Root level middleware
|
|
|
|
e.Use(middleware.Logger())
|
|
|
|
e.Use(middleware.Recover())
|
|
|
|
|
|
|
|
// Group level middleware
|
|
|
|
g := e.Group("/admin")
|
|
|
|
g.Use(middleware.BasicAuth(func(username, password string) bool {
|
|
|
|
if username == "joe" && password == "secret" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}))
|
|
|
|
|
|
|
|
// Route level middleware
|
|
|
|
track := func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
println("request to /users")
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
2016-04-19 01:59:58 +02:00
|
|
|
e.GET("/users", func(c echo.Context) error {
|
2016-04-03 04:32:52 +02:00
|
|
|
return c.String(http.StatusOK, "/users")
|
|
|
|
}, track)
|
|
|
|
```
|
|
|
|
|
2016-04-07 05:01:24 +02:00
|
|
|
#### Built-in Middleware
|
|
|
|
|
|
|
|
Middleware | Description
|
|
|
|
:--- | :---
|
2016-05-29 22:59:16 +02:00
|
|
|
[BodyLimit](https://echo.labstack.com/middleware/body-limit) | Limit request body
|
|
|
|
[Logger](https://echo.labstack.com/middleware/logger) | Log HTTP requests
|
|
|
|
[Recover](https://echo.labstack.com/middleware/recover) | Recover from panics
|
|
|
|
[Gzip](https://echo.labstack.com/middleware/gzip) | Send gzip HTTP response
|
|
|
|
[BasicAuth](https://echo.labstack.com/middleware/basic-auth) | HTTP basic authentication
|
|
|
|
[JWTAuth](https://echo.labstack.com/middleware/jwt) | JWT authentication
|
|
|
|
[Secure](https://echo.labstack.com/middleware/secure) | Protection against attacks
|
|
|
|
[CORS](https://echo.labstack.com/middleware/cors) | Cross-Origin Resource Sharing
|
|
|
|
[CSRF](https://echo.labstack.com/middleware/csrf) | Cross-Site Request Forgery
|
|
|
|
[Static](https://echo.labstack.com/middleware/static) | Serve static files
|
2016-09-01 07:15:26 +02:00
|
|
|
[HTTPSRedirect](https://echo.labstack.com/middleware/redirect#httpsredirect-middleware) | Redirect HTTP requests to HTTPS
|
|
|
|
[HTTPSWWWRedirect](https://echo.labstack.com/middleware/redirect#httpswwwredirect-middleware) | Redirect HTTP requests to WWW HTTPS
|
|
|
|
[WWWRedirect](https://echo.labstack.com/middleware/redirect#wwwredirect-middleware) | Redirect non WWW requests to WWW
|
2016-09-01 17:55:27 +02:00
|
|
|
[NonWWWRedirect](https://echo.labstack.com/middleware/redirect#nonwwwredirect-middleware) | Redirect WWW requests to non WWW
|
2016-09-08 02:21:27 +02:00
|
|
|
[AddTrailingSlash](https://echo.labstack.com/middleware/trailing-slash#addtrailingslash-middleware) | Add trailing slash to the request URI
|
|
|
|
[RemoveTrailingSlash](https://echo.labstack.com/middleware/trailing-slash#removetrailingslash-middleware) | Remove trailing slash from the request URI
|
2016-05-29 22:59:16 +02:00
|
|
|
[MethodOverride](https://echo.labstack.com/middleware/method-override) | Override request method
|
2016-05-28 20:39:03 +02:00
|
|
|
|
2016-06-26 03:33:03 +02:00
|
|
|
##### [Learn More](https://echo.labstack.com/middleware/overview)
|
2016-04-03 04:32:52 +02:00
|
|
|
|
2016-04-12 01:49:20 +02:00
|
|
|
#### Third-party Middleware
|
|
|
|
|
|
|
|
Middleware | Description
|
|
|
|
:--- | :---
|
|
|
|
[echoperm](https://github.com/xyproto/echoperm) | Keeping track of users, login states and permissions.
|
2016-06-12 19:49:03 +02:00
|
|
|
[echopprof](https://github.com/mtojek/echopprof) | Adapt net/http/pprof to labstack/echo.
|
2016-04-12 01:49:20 +02:00
|
|
|
|
2016-03-29 22:53:40 +02:00
|
|
|
### Next
|
|
|
|
|
2016-05-24 23:51:26 +02:00
|
|
|
- Head over to [guide](https://echo.labstack.com/guide/installation)
|
|
|
|
- Browse [recipes](https://echo.labstack.com/recipes/hello-world)
|
2015-04-16 19:18:35 +02:00
|
|
|
|
2016-03-29 22:53:40 +02:00
|
|
|
### Need help?
|
|
|
|
|
|
|
|
- [Hop on to chat](https://gitter.im/labstack/echo)
|
|
|
|
- [Open an issue](https://github.com/labstack/echo/issues/new)
|
|
|
|
|
2016-04-03 04:32:52 +02:00
|
|
|
## Support Us
|
2016-03-29 22:53:40 +02:00
|
|
|
|
|
|
|
- :star: the project
|
2016-09-07 07:09:51 +02:00
|
|
|
- [Donate](https://echo.labstack.com/support-echo)
|
2016-04-02 23:19:39 +02:00
|
|
|
- :earth_americas: spread the word
|
2016-03-29 22:53:40 +02:00
|
|
|
- [Contribute](#contribute) to the project
|
|
|
|
|
2015-04-26 01:10:28 +02:00
|
|
|
## Contribute
|
|
|
|
|
|
|
|
**Use issues for everything**
|
2015-04-27 07:41:41 +02:00
|
|
|
|
2016-03-11 08:22:42 +02:00
|
|
|
- Report issues
|
2016-03-29 22:53:40 +02:00
|
|
|
- Discuss on chat before sending a pull request
|
|
|
|
- Suggest new features or enhancements
|
2015-04-26 01:10:28 +02:00
|
|
|
- Improve/fix documentation
|
|
|
|
|
|
|
|
## Credits
|
|
|
|
- [Vishal Rana](https://github.com/vishr) - Author
|
|
|
|
- [Nitin Rana](https://github.com/nr17) - Consultant
|
|
|
|
- [Contributors](https://github.com/labstack/echo/graphs/contributors)
|
2016-03-11 08:22:42 +02:00
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
[MIT](https://github.com/labstack/echo/blob/master/LICENSE)
|