mirror of
https://github.com/labstack/echo.git
synced 2025-01-24 03:16:14 +02:00
Putting func(http.Handler) http.Handler middleware back
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
f00e11010b
commit
2e5d09d225
@ -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)`
|
||||
|
17
echo.go
17
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)
|
||||
|
24
echo_test.go
24
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)
|
||||
|
@ -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")),
|
||||
|
@ -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)`
|
||||
|
Loading…
x
Reference in New Issue
Block a user