- Master branch, website and godoc now points to Echo v2.
- Echo v2 is in beta but if you want to try it out here is the migrating [guide] (https://labstack.com/echo/guide/migrating)
- Looking for v1?
- Installation: Use a package manager (https://github.com/Masterminds/glide, it's nice!) to get stable v1 release/commit or use
go get gopkg.in/labstack/echo.v1. - Godoc: https://godoc.org/gopkg.in/labstack/echo.v1
- Docs: https://github.com/labstack/echo/tree/v1.4/website/content
- Installation: Use a package manager (https://github.com/Masterminds/glide, it's nice!) to get stable v1 release/commit or use
- 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.
- 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
$ go get github.com/labstack/echo/...Create server.go
package main
import (
"net/http""github.com/labstack/echo""github.com/labstack/echo/engine/standard"
)
funcmain() {
e:=echo.New()
e.GET("/", func(c echo.Context) error {
returnc.String(http.StatusOK, "Hello, World!")
})
e.Run(standard.New(":1323"))
}Start server
$ go run server.goBrowse to http://localhost:1323 and you should see Hello, World! on the page.
e.POST("/users", saveUser)
e.GET("/users/:id", getUser)
e.PUT("/users/:id", updateUser)
e.DELETE("/users/:id", deleteUser)funcgetUser(c echo.Context) error {
// User ID from path `users/:id`id:=c.Param("id")
}/show?team=x-men&member=wolverine
funcshow(c echo.Context) error {
// Get team and member from the query stringteam:=c.QueryParam("team")
member:=c.QueryParam("member")
}POST/save
| name | value |
|---|---|
| name | Joe Smith |
| joe@labstack.com |
funcsave(c echo.Context) error {
// Get name and emailname:=c.FormValue("name")
email:=c.FormValue("email")
}POST/save
| name | value |
|---|---|
| name | Joe Smith |
| joe@labstack.com | |
| avatar | avatar |
funcsave(c echo.Context) error {
// Get name and emailname:=c.FormValue("name")
email:=c.FormValue("email")
// Get avataravatar, err:=c.FormFile("avatar")
iferr!=nil {
returnerr
}
// Sourcesrc, err:=avatar.Open()
iferr!=nil {
returnerr
}
defersrc.Close()
// Destinationdst, err:=os.Create(avatar.Filename)
iferr!=nil {
returnerr
}
deferdst.Close()
// Copyif_, err=io.Copy(dst, src); err!=nil {
returnerr
}
returnc.HTML(http.StatusOK, "<b>Thank you!</b>")
}- Bind
JSONorXMLorformpayload into Go struct based onContent-Typerequest header. - Render response as
JSONorXMLwith status code.
typeUserstruct {
Namestring`json:"name" xml:"name" form:"name"`Emailstring`json:"email" xml:"email" form:"email"`
}
e.POST("/users", func(c echo.Context) error {
u:=new(User)
iferr:=c.Bind(u); err!=nil {
returnerr
}
returnc.JSON(http.StatusCreated, u)
// or// return c.XML(http.StatusCreated, u)
})Server any file from static directory for path /static/*.
e.Static("/static", "static")// Root level middlewaree.Use(middleware.Logger())
e.Use(middleware.Recover())
// Group level middlewareg:=e.Group("/admin")
g.Use(middleware.BasicAuth(func(username, passwordstring) bool {
ifusername=="joe"&&password=="secret" {
returntrue
}
returnfalse
}))
// Route level middlewaretrack:=func(next echo.HandlerFunc) echo.HandlerFunc {
returnfunc(c echo.Context) error {
println("request to /users")
returnnext(c)
}
}
e.GET("/users", func(c echo.Context) error {
returnc.String(http.StatusOK, "/users")
}, track)| Middleware | Description |
|---|---|
| BodyLimit | Limit request body |
| Logger | Log HTTP requests |
| Recover | Recover from panics |
| Gzip | Send gzip HTTP response |
| BasicAuth | HTTP basic authentication |
| JWTAuth | JWT authentication |
| Secure | Protection against attacks |
| CORS | Cross-Origin Resource Sharing |
| Static | Serve static files |
| AddTrailingSlash | Add trailing slash to the request URI |
| RemoveTrailingSlash | Remove trailing slash from the request URI |
| MethodOverride | Override request method |
| Middleware | Description |
|---|---|
| echoperm | Keeping track of users, login states and permissions. |
- ⭐ the project
- Donate
- 🌎 spread the word
- Contribute to the project
Use issues for everything
- Report issues
- Discuss on chat before sending a pull request
- Suggest new features or enhancements
- Improve/fix documentation
- Vishal Rana - Author
- Nitin Rana - Consultant
- Contributors
