diff --git a/examples/middleware/server.go b/examples/middleware/server.go index ca321dd9..503210c3 100644 --- a/examples/middleware/server.go +++ b/examples/middleware/server.go @@ -30,8 +30,8 @@ func main() { e.Use(mw.Recover()) // Basic auth - e.Use(mw.BasicAuth(func(u, p string) bool { - if u == "joe" && p == "secret" { + e.Use(mw.BasicAuth(func(usr, pwd string) bool { + if usr == "joe" && pwd == "secret" { return true } return false diff --git a/middleware/recover.go b/middleware/recover.go index c430cb3a..b62edb52 100644 --- a/middleware/recover.go +++ b/middleware/recover.go @@ -9,7 +9,7 @@ import ( ) // Recover returns a middleware which recovers from panics anywhere in the chain -// and handles the control to centralized HTTPErrorHandler. +// and handles the control to the centralized HTTPErrorHandler. func Recover() echo.MiddlewareFunc { // TODO: Provide better stack trace `https://github.com/go-errors/errors` `https://github.com/docker/libcontainer/tree/master/stacktrace` return func(h echo.HandlerFunc) echo.HandlerFunc { diff --git a/website/docs/guide.md b/website/docs/guide.md index f6a694d9..7bc1be16 100644 --- a/website/docs/guide.md +++ b/website/docs/guide.md @@ -59,7 +59,7 @@ code below registers a route for method `GET`, path `/hello` and a handler which `Hello!` HTTP response. ```go -echo.Get("/hello", func(*echo.Context) error { +echo.Get("/hello", func(c *echo.Context) error { return c.String(http.StatusOK, "Hello!") }) ``` @@ -68,11 +68,27 @@ Echo's default handler is `func(*echo.Context) error` where `echo.Context` primarily holds HTTP request and response objects. Echo also has a support for other types of handlers. - - ### Group -*WIP* +`Echo.Group(prefix string, m ...Middleware) *Group` + +Routes with common prefix can be grouped to define a new sub-router with optional +middleware. If middleware is passed to the function, it overrides parent middleware +- helpful if you want a completly new middleware stack for the group. To add middleware +later 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 +echo.Group("/admin") +e.Use(mw.BasicAuth(func(usr, pwd string) bool { + if usr == "joe" && pwd == "secret" { + return true + } + return false +})) +``` ### Path parameter @@ -154,7 +170,93 @@ e.Get("/users/:id", h) ## Middleware -[*WIP*](https://github.com/labstack/echo/tree/master/examples/middleware) +Middleware is function which is chained in the HTTP request-response cycle. Middleware +has access to the request and response objects which it utilizes to perform a specific +action for example, logging every request. Echo supports variety of [middleware](/#features). + +### Logger + +Logs each HTTP request with method, path, status, response time and bytes served. + +*Example* + +```go +e.Use(Logger()) + +// Output: `2015/06/07 18:16:16 GET / 200 13.238µs 14` +``` + +### BasicAuth + +BasicAuth middleware provides an HTTP basic authentication. + +- For valid credentials it calls the next handler in the chain. +- For invalid Authorization header it sends "404 - Bad Request" response. +- For invalid credentials, it sends "401 - Unauthorized" response. + +*Example* + +```go +echo.Group("/admin") +e.Use(mw.BasicAuth(func(usr, pwd string) bool { + if usr == "joe" && pwd == "secret" { + return true + } + return false +})) +``` + +### Gzip + +Gzip middleware compresses HTTP response using gzip compression scheme. + +*Example* + +```go +e.Use(mw.Gzip()) +``` + +### Recover + +Recover middleware recovers from panics anywhere in the chain and handles the control +to the centralized [HTTPErrorHandler](#error-handling). + +*Example* + +```go +e.Use(mw.Recover()) +``` + +### StripTrailingSlash + +StripTrailingSlash middleware removes the trailing slash from request path. + +*Example* + +```go +e.Use(mw.StripTrailingSlash()) +``` + +### RedirectToSlash +RedirectToSlash middleware redirects requests without trailing slash path to trailing +slash path. + +*Options* +```go +RedirectToSlashOptions struct { + Code int +} +``` + +*Example* + +```go +e.Use(mw.RedirectToSlash()) +``` + +*Note*: StripTrailingSlash and RedirectToSlash should not be used together. + +[Examples](https://github.com/labstack/echo/tree/master/examples/middleware) ## Response diff --git a/website/docs/index.md b/website/docs/index.md index ee70ebbe..d01f91ac 100644 --- a/website/docs/index.md +++ b/website/docs/index.md @@ -86,9 +86,7 @@ func main() { made to the server, producing output ```sh -2015/04/25 12:15:20 GET / 200 7.544µs -2015/04/25 12:15:26 GET / 200 3.681µs -2015/04/25 12:15:29 GET / 200 5.434µs +2015/06/07 18:16:16 GET / 200 13.238µs 14 ``` `e.Get("/", hello)` Registers hello handler for HTTP method `GET` and path `/`, so diff --git a/website/echo/base.html b/website/echo/base.html new file mode 100644 index 00000000..4f0260c1 --- /dev/null +++ b/website/echo/base.html @@ -0,0 +1,78 @@ + + +
+ + + + {% if page_description %}{% endif %} + {% if site_author %}{% endif %} + {% if canonical_url %}{% endif %} + {% if favicon %} + {% else %}{% endif %} + +