2015-04-19 22:04:30 -07:00
|
|
|
# Echo
|
|
|
|
|
2015-07-01 21:22:51 -07:00
|
|
|
A fast and unfancy micro web framework for Golang.
|
2015-04-19 22:04:30 -07:00
|
|
|
|
|
|
|
---
|
|
|
|
|
2015-04-18 21:53:38 -07:00
|
|
|
## Features
|
|
|
|
|
2015-06-18 14:40:48 -07:00
|
|
|
- Fast HTTP router which smartly prioritize routes.
|
2015-06-03 09:20:32 -07:00
|
|
|
- Extensible middleware, supports:
|
|
|
|
- `echo.MiddlewareFunc`
|
|
|
|
- `func(echo.HandlerFunc) echo.HandlerFunc`
|
|
|
|
- `echo.HandlerFunc`
|
|
|
|
- `func(*echo.Context) error`
|
|
|
|
- `func(http.Handler) http.Handler`
|
|
|
|
- `http.Handler`
|
|
|
|
- `http.HandlerFunc`
|
|
|
|
- `func(http.ResponseWriter, *http.Request)`
|
|
|
|
- Extensible handler, supports:
|
|
|
|
- `echo.HandlerFunc`
|
|
|
|
- `func(*echo.Context) error`
|
|
|
|
- `http.Handler`
|
|
|
|
- `http.HandlerFunc`
|
|
|
|
- `func(http.ResponseWriter, *http.Request)`
|
2015-06-18 14:40:48 -07:00
|
|
|
- Sub-router/Groups
|
|
|
|
- Handy encoding/decoding functions.
|
2015-06-03 09:20:32 -07:00
|
|
|
- Build-in support for:
|
2015-06-18 14:40:48 -07:00
|
|
|
- Static files
|
|
|
|
- WebSocket
|
|
|
|
- API to serve index and favicon.
|
|
|
|
- Centralized HTTP error handling.
|
|
|
|
- Customizable request binding function.
|
2015-06-03 09:20:32 -07:00
|
|
|
- Customizable response rendering function, allowing you to use any HTML template engine.
|
2015-04-18 21:53:38 -07:00
|
|
|
|
2015-06-22 14:25:39 -07:00
|
|
|
## Performance
|
|
|
|
|
2015-06-27 23:34:22 +02:00
|
|
|
<iframe width="600" height="371" seamless frameborder="0" scrolling="no" src="https://docs.google.com/spreadsheets/d/1phsG_NPmEOaTVTw6lasK3CeEwBlbkhzAWPiyrBznm1g/pubchart?oid=178095723&format=interactive"></iframe>
|
2015-06-22 14:25:39 -07:00
|
|
|
|
2015-04-24 15:13:06 -07:00
|
|
|
## Getting Started
|
2015-04-18 21:53:38 -07:00
|
|
|
|
2015-04-24 15:13:06 -07:00
|
|
|
### Installation
|
|
|
|
|
2015-04-25 12:46:27 -07:00
|
|
|
```sh
|
|
|
|
$ go get github.com/labstack/echo
|
|
|
|
```
|
2015-04-18 21:53:38 -07:00
|
|
|
|
2015-07-01 21:22:51 -07:00
|
|
|
### Hello, World!
|
2015-04-19 22:47:58 -07:00
|
|
|
|
2015-04-25 12:46:27 -07:00
|
|
|
Create `server.go` with the following content
|
2015-04-18 21:53:38 -07:00
|
|
|
|
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
2015-04-20 23:24:34 -07:00
|
|
|
mw "github.com/labstack/echo/middleware"
|
2015-04-18 21:53:38 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// Handler
|
2015-05-20 15:59:36 -07:00
|
|
|
func hello(c *echo.Context) error {
|
2015-05-05 21:55:49 -07:00
|
|
|
return c.String(http.StatusOK, "Hello, World!\n")
|
2015-04-18 21:53:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2015-04-25 12:46:27 -07:00
|
|
|
// Echo instance
|
2015-04-18 21:53:38 -07:00
|
|
|
e := echo.New()
|
|
|
|
|
|
|
|
// Middleware
|
2015-05-15 12:29:14 -07:00
|
|
|
e.Use(mw.Logger())
|
2015-05-18 11:33:11 -07:00
|
|
|
e.Use(mw.Recover())
|
2015-04-18 21:53:38 -07:00
|
|
|
|
|
|
|
// Routes
|
|
|
|
e.Get("/", hello)
|
|
|
|
|
|
|
|
// Start server
|
2015-05-10 08:22:49 -07:00
|
|
|
e.Run(":1323")
|
2015-04-18 21:53:38 -07:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2015-04-25 12:46:27 -07:00
|
|
|
`echo.New()` returns a new instance of Echo.
|
2015-04-18 21:53:38 -07:00
|
|
|
|
2015-05-15 12:29:14 -07:00
|
|
|
`e.Use(mw.Logger())` adds logging middleware to the chain. It logs every HTTP request
|
2015-05-05 21:55:49 -07:00
|
|
|
made to the server, producing output
|
2015-04-18 21:53:38 -07:00
|
|
|
|
2015-04-25 12:46:27 -07:00
|
|
|
```sh
|
2015-06-07 19:57:49 -07:00
|
|
|
2015/06/07 18:16:16 GET / 200 13.238µs 14
|
2015-04-25 12:46:27 -07:00
|
|
|
```
|
2015-04-18 21:53:38 -07:00
|
|
|
|
2015-05-10 19:24:35 -07:00
|
|
|
`e.Get("/", hello)` Registers hello handler for HTTP method `GET` and path `/`, so
|
|
|
|
whenever server receives an HTTP request at `/`, hello function is called.
|
2015-04-18 21:53:38 -07:00
|
|
|
|
2015-04-25 12:46:27 -07:00
|
|
|
In hello handler `c.String(http.StatusOK, "Hello, World!\n")` sends a text/plain
|
2015-05-06 15:51:52 -07:00
|
|
|
HTTP response to the client with 200 status code.
|
2015-04-18 21:53:38 -07:00
|
|
|
|
2015-05-10 08:22:49 -07:00
|
|
|
`e.Run(":1323")` Starts HTTP server at network address `:1323`.
|
2015-04-18 21:53:38 -07:00
|
|
|
|
2015-04-25 12:46:27 -07:00
|
|
|
Now start the server using command
|
2015-04-18 21:53:38 -07:00
|
|
|
|
2015-04-25 12:46:27 -07:00
|
|
|
```sh
|
|
|
|
$ go run server.go
|
2015-04-18 21:53:38 -07:00
|
|
|
```
|
2015-04-18 22:27:30 -07:00
|
|
|
|
2015-05-10 08:22:49 -07:00
|
|
|
Browse to [http://localhost:1323](http://localhost:1323) and you should see
|
2015-04-25 12:46:27 -07:00
|
|
|
Hello, World! on the page.
|
|
|
|
|
2015-04-25 16:10:28 -07:00
|
|
|
### Next?
|
2015-06-30 12:10:35 -07:00
|
|
|
- Browse [recipes](https://github.com/labstack/echo/tree/master/recipes)
|
2015-04-25 16:10:28 -07:00
|
|
|
- Head over to [Guide](guide.md)
|
|
|
|
|
2015-04-19 22:04:30 -07:00
|
|
|
## Contribute
|
2015-04-25 12:46:27 -07:00
|
|
|
|
|
|
|
**Use issues for everything**
|
2015-04-26 22:41:41 -07:00
|
|
|
|
2015-04-29 13:54:47 -07:00
|
|
|
- Report issues
|
2015-04-25 12:46:27 -07:00
|
|
|
- Discuss before sending pull request
|
2015-04-19 22:04:30 -07:00
|
|
|
- Suggest new features
|
2015-04-25 12:46:27 -07:00
|
|
|
- Improve/fix documentation
|
2015-04-19 22:04:30 -07:00
|
|
|
|
2015-04-25 16:10:28 -07:00
|
|
|
## Credits
|
|
|
|
- [Vishal Rana](https://github.com/vishr) - Author
|
|
|
|
- [Nitin Rana](https://github.com/nr17) - Consultant
|
|
|
|
- [Contributors](https://github.com/labstack/echo/graphs/contributors)
|
|
|
|
|
2015-04-18 22:27:30 -07:00
|
|
|
## License
|
|
|
|
|
|
|
|
[MIT](https://github.com/labstack/echo/blob/master/LICENSE)
|