2016-03-11 07:40:25 +02:00
# *NOTICE*
- Master branch, website and godoc now points to Echo v2.
2016-04-06 17:21:31 +02:00
- Echo v2 is in beta but if you want to try it out here is the migrating [guide] (https://labstack.com/echo/guide/migrating/)
2016-03-11 07:40:25 +02:00
- 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
2016-03-22 04:57:26 +02:00
- Docs: https://github.com/labstack/echo/tree/v1.4/website/content
2015-12-22 01:20:49 +02:00
2015-10-08 22:54:31 +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)
2015-06-30 21:10:35 +02:00
2016-04-03 04:32:52 +02:00
#### Echo is a fast and unfancy web framework for Go (Golang). Up to 10x faster than the rest.
2016-02-09 21:46:08 +02:00
2016-03-11 08:22:42 +02:00
## Features
2016-04-03 04:32:52 +02:00
- Optimized HTTP router which smartly prioritize routes.
- Build robust and scalable RESTful APIs.
2016-03-11 08:22:42 +02:00
- Run with standard HTTP server or FastHTTP server.
2016-04-03 04:32:52 +02:00
- Group APIs.
2016-03-11 08:22:42 +02:00
- Extensible middleware framework.
2016-04-03 04:32:52 +02:00
- Define middleware at root, group or route level.
2016-03-11 08:22:42 +02:00
- Handy functions to send variety of HTTP responses.
- Centralized HTTP error handling.
- Template rendering with any template engine.
2016-04-03 04:32:52 +02:00
- 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-04-03 04:32:52 +02:00
![Performance ](http://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-03-27 19:25:31 +02:00
$ go get 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-02-09 21:46:08 +02:00
"github.com/labstack/echo"
2016-04-05 15:41:06 +02:00
"github.com/labstack/echo/engine/standard"
2016-02-09 21:46:08 +02:00
)
func main() {
e := echo.New()
2016-04-05 15:41:06 +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-04-05 15:41:06 +02:00
e.Run(standard.New(":1323"))
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
e.Post("/users", saveUser)
e.Get("/users/:id", getUser)
e.Put("/users/:id", updateUser)
e.Delete("/users/:id", deleteUser)
```
### Path Parameters
```go
func getUser(c echo.Context) error {
// User ID from path `users/:id`
id := c.Param("id")
}
```
### Query Parameters
`/show?team=x-men&member=wolverine`
```go
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-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
func save(c echo.Context) error {
// Get name and email
name := c.FormValue("name")
email := c.FormParam("email")
}
```
### Form `multipart/form-data`
`POST` `/save`
name | value
:--- | :---
name | Joe Smith
email | joe@labstack.com
avatar | avatar
```go
func save(c echo.Context) error {
// Get name and email
name := c.FormValue("name")
email := c.FormParam("email")
//------------
// Get avatar
//------------
avatar, err := c.FormFile("avatar")
if err != nil {
return err
}
src, err := file.Open()
if err != nil {
return err
}
defer src.Close()
// Destination
file, err := os.Create(file.Filename)
if err != nil {
return err
}
defer file.Close()
// Copy
if _, err = io.Copy(file, avatar); err != nil {
return err
}
}
```
### Handling Request
- Bind `JSON` or `XML` payload into Go struct based on `Content-Type` request header.
- Render response as `JSON` or `XML` with status code.
```go
type User struct {
Name string `json:"name" xml:"name"`
Email string `json:"email" xml:"email"`
}
e.Post("/users", func(c echo.Context) error {
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-04-07 05:01:24 +02:00
##### [More...](https://labstack.com/echo/guide/static-files/)
2016-04-03 04:32:52 +02:00
### [Template Rendering](https://labstack.com/echo/guide/templates/)
### 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)
}
}
e.Get("/users", func(c echo.Context) error {
return c.String(http.StatusOK, "/users")
}, track)
```
2016-04-07 05:01:24 +02:00
#### Built-in Middleware
Middleware | Description
:--- | :---
[Logger ](https://labstack.com/echo/guide/middleware/#logger-middleware:37ab2f15ff048f67959bcac0a6032f32 ) | Log HTTP requests
[Recover ](https://labstack.com/echo/guide/middleware/#recover-middleware:37ab2f15ff048f67959bcac0a6032f32 ) | Recover from panics
[Gzip ](https://labstack.com/echo/guide/middleware/#gzip-middleware:37ab2f15ff048f67959bcac0a6032f32 ) | Send gzip HTTP response
[BasicAuth ](https://labstack.com/echo/guide/middleware/#basicauth-middleware:37ab2f15ff048f67959bcac0a6032f32 ) | HTTP basic authentication
2016-04-08 01:57:57 +02:00
[CORS ](https://labstack.com/echo/guide/middleware/#cors-middleware:37ab2f15ff048f67959bcac0a6032f32 ) | Cross-Origin Resource Sharing
2016-04-07 05:01:24 +02:00
[Static ](https://labstack.com/echo/guide/static-files/#using-static-middleware:123f9d1043075fe4874616541b409e4d ) | Serve static files
[AddTrailingSlash ](https://labstack.com/echo/guide/middleware/#addtrailingslash-middleware:37ab2f15ff048f67959bcac0a6032f32 ) | Add trailing slash to the request URI
[RemoveTrailingSlash ](https://labstack.com/echo/guide/middleware/#removetrailingslash-middleware:37ab2f15ff048f67959bcac0a6032f32 ) | Remove trailing slash from the request URI
##### [More...](https://labstack.com/echo/guide/middleware/)
2016-04-03 04:32:52 +02:00
2016-03-29 22:53:40 +02:00
### Next
2016-04-03 04:32:52 +02:00
- Head over to [guide ](https://labstack.com/echo/guide/installation/ )
- Browse [recipes ](https://labstack.com/echo/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
- [Donate ](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=JD5R56K84A8G4&lc=US&item_name=LabStack&item_number=echo¤cy_code=USD&bn=PP-DonationsBF:btn_donate_LG.gif:NonHosted )
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 )