diff --git a/README.md b/README.md index 83fe81fc..c6d1a656 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ Echo is a fast HTTP router (zero memory allocation) and micro web framework in G - `func(*echo.Context)` - `func(*echo.Context) error` - `func(echo.HandlerFunc) echo.HandlerFunc` + - `func(http.Handler) http.Handler` - `http.Handler` - `http.HandlerFunc` - `func(http.ResponseWriter, *http.Request)` diff --git a/echo.go b/echo.go index 58cd5e73..e807d7d5 100644 --- a/echo.go +++ b/echo.go @@ -309,24 +309,35 @@ func wrapM(m Middleware) MiddlewareFunc { } case func(HandlerFunc) HandlerFunc: return m + case func(http.Handler) http.Handler: + return func(h HandlerFunc) HandlerFunc { + return func(c *Context) (err error) { + m(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + c.Response.Writer = w + c.Request = r + err = h(c) + })).ServeHTTP(c.Response.Writer, c.Request) + return + } + } case http.Handler, http.HandlerFunc: return func(h HandlerFunc) HandlerFunc { return func(c *Context) error { - m.(http.Handler).ServeHTTP(c.Response, c.Request) + m.(http.Handler).ServeHTTP(c.Response.Writer, c.Request) return h(c) } } case func(http.ResponseWriter, *http.Request): return func(h HandlerFunc) HandlerFunc { return func(c *Context) error { - m(c.Response, c.Request) + m(c.Response.Writer, c.Request) return h(c) } } case func(http.ResponseWriter, *http.Request) error: return func(h HandlerFunc) HandlerFunc { return func(c *Context) error { - if err := m(c.Response, c.Request); err != nil { + if err := m(c.Response.Writer, c.Request); err != nil { return err } return h(c) diff --git a/echo_test.go b/echo_test.go index 8350da54..9a40904f 100644 --- a/echo_test.go +++ b/echo_test.go @@ -84,34 +84,34 @@ func TestEchoMiddleware(t *testing.T) { }))) // func(http.Handler) http.Handler - // e.Use(func(h http.Handler) http.Handler { - // return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // b.WriteString("f") - // h.ServeHTTP(w, r) - // }) - // }) + e.Use(func(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + b.WriteString("f") + h.ServeHTTP(w, r) + }) + }) // func(http.ResponseWriter, *http.Request) e.Use(func(w http.ResponseWriter, r *http.Request) { - b.WriteString("f") + b.WriteString("g") }) // func(http.ResponseWriter, *http.Request) error e.Use(func(w http.ResponseWriter, r *http.Request) error { - b.WriteString("g") + b.WriteString("h") return nil }) // Route e.Get("/hello", func(c *Context) { - c.String(200, "world") + c.String(http.StatusOK, "world") }) w := httptest.NewRecorder() r, _ := http.NewRequest(GET, "/hello", nil) e.ServeHTTP(w, r) - if b.String() != "abcdefg" { - t.Errorf("buffer should be abcdefg, found %s", b.String()) + if b.String() != "abcdefgh" { + t.Errorf("buffer should be abcdefgh, found %s", b.String()) } if w.Body.String() != "world" { t.Error("body should be world") @@ -247,7 +247,7 @@ func TestEchoNotFound(t *testing.T) { // Customized NotFound handler e.NotFoundHandler(func(c *Context) { - c.String(404, "not found") + c.String(http.StatusNotFound, "not found") }) w = httptest.NewRecorder() e.ServeHTTP(w, r) diff --git a/examples/web/server.go b/examples/web/server.go index 7c2004b8..f31420df 100644 --- a/examples/web/server.go +++ b/examples/web/server.go @@ -7,6 +7,8 @@ import ( "html/template" "github.com/labstack/echo" + "github.com/rs/cors" + "github.com/thoas/stats" ) type ( @@ -57,6 +59,21 @@ func main() { // Middleware e.Use(echo.Logger) + //------------------------ + // Third-party middleware + //------------------------ + + // https://github.com/rs/cors + e.Use(cors.Default().Handler) + + // https://github.com/thoas/stats + s := stats.New() + e.Use(s.Handler) + // Route + e.Get("/stats", func(c *echo.Context) { + c.JSON(http.StatusOK, s.Data()) + }) + // Serve index file e.Index("public/index.html") @@ -66,6 +83,7 @@ func main() { //-------- // Routes //-------- + e.Post("/users", createUser) e.Get("/users", getUsers) e.Get("/users/:id", getUser) @@ -73,6 +91,7 @@ func main() { //----------- // Templates //----------- + t := &Template{ // Cached templates templates: template.Must(template.ParseFiles("public/views/welcome.html")), diff --git a/website/docs/index.md b/website/docs/index.md index 1dbd5069..a3e1826d 100644 --- a/website/docs/index.md +++ b/website/docs/index.md @@ -16,6 +16,7 @@ Echo is a fast HTTP router (zero memory allocation) and micro web framework in G - `func(*echo.Context)` - `func(*echo.Context) error` - `func(echo.HandlerFunc) echo.HandlerFunc` + - `func(http.Handler) http.Handler` - `http.Handler` - `http.HandlerFunc` - `func(http.ResponseWriter, *http.Request)`