mirror of
https://github.com/labstack/echo.git
synced 2025-02-03 13:11:39 +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)`
|
||||||
- `func(*echo.Context) error`
|
- `func(*echo.Context) error`
|
||||||
- `func(echo.HandlerFunc) echo.HandlerFunc`
|
- `func(echo.HandlerFunc) echo.HandlerFunc`
|
||||||
|
- `func(http.Handler) http.Handler`
|
||||||
- `http.Handler`
|
- `http.Handler`
|
||||||
- `http.HandlerFunc`
|
- `http.HandlerFunc`
|
||||||
- `func(http.ResponseWriter, *http.Request)`
|
- `func(http.ResponseWriter, *http.Request)`
|
||||||
|
17
echo.go
17
echo.go
@ -309,24 +309,35 @@ func wrapM(m Middleware) MiddlewareFunc {
|
|||||||
}
|
}
|
||||||
case func(HandlerFunc) HandlerFunc:
|
case func(HandlerFunc) HandlerFunc:
|
||||||
return m
|
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:
|
case http.Handler, http.HandlerFunc:
|
||||||
return func(h HandlerFunc) HandlerFunc {
|
return func(h HandlerFunc) HandlerFunc {
|
||||||
return func(c *Context) error {
|
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)
|
return h(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case func(http.ResponseWriter, *http.Request):
|
case func(http.ResponseWriter, *http.Request):
|
||||||
return func(h HandlerFunc) HandlerFunc {
|
return func(h HandlerFunc) HandlerFunc {
|
||||||
return func(c *Context) error {
|
return func(c *Context) error {
|
||||||
m(c.Response, c.Request)
|
m(c.Response.Writer, c.Request)
|
||||||
return h(c)
|
return h(c)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case func(http.ResponseWriter, *http.Request) error:
|
case func(http.ResponseWriter, *http.Request) error:
|
||||||
return func(h HandlerFunc) HandlerFunc {
|
return func(h HandlerFunc) HandlerFunc {
|
||||||
return func(c *Context) error {
|
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 err
|
||||||
}
|
}
|
||||||
return h(c)
|
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
|
// func(http.Handler) http.Handler
|
||||||
// e.Use(func(h http.Handler) http.Handler {
|
e.Use(func(h http.Handler) http.Handler {
|
||||||
// return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// b.WriteString("f")
|
b.WriteString("f")
|
||||||
// h.ServeHTTP(w, r)
|
h.ServeHTTP(w, r)
|
||||||
// })
|
})
|
||||||
// })
|
})
|
||||||
|
|
||||||
// func(http.ResponseWriter, *http.Request)
|
// func(http.ResponseWriter, *http.Request)
|
||||||
e.Use(func(w http.ResponseWriter, r *http.Request) {
|
e.Use(func(w http.ResponseWriter, r *http.Request) {
|
||||||
b.WriteString("f")
|
b.WriteString("g")
|
||||||
})
|
})
|
||||||
|
|
||||||
// func(http.ResponseWriter, *http.Request) error
|
// func(http.ResponseWriter, *http.Request) error
|
||||||
e.Use(func(w http.ResponseWriter, r *http.Request) error {
|
e.Use(func(w http.ResponseWriter, r *http.Request) error {
|
||||||
b.WriteString("g")
|
b.WriteString("h")
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
// Route
|
// Route
|
||||||
e.Get("/hello", func(c *Context) {
|
e.Get("/hello", func(c *Context) {
|
||||||
c.String(200, "world")
|
c.String(http.StatusOK, "world")
|
||||||
})
|
})
|
||||||
|
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
r, _ := http.NewRequest(GET, "/hello", nil)
|
r, _ := http.NewRequest(GET, "/hello", nil)
|
||||||
e.ServeHTTP(w, r)
|
e.ServeHTTP(w, r)
|
||||||
if b.String() != "abcdefg" {
|
if b.String() != "abcdefgh" {
|
||||||
t.Errorf("buffer should be abcdefg, found %s", b.String())
|
t.Errorf("buffer should be abcdefgh, found %s", b.String())
|
||||||
}
|
}
|
||||||
if w.Body.String() != "world" {
|
if w.Body.String() != "world" {
|
||||||
t.Error("body should be world")
|
t.Error("body should be world")
|
||||||
@ -247,7 +247,7 @@ func TestEchoNotFound(t *testing.T) {
|
|||||||
|
|
||||||
// Customized NotFound handler
|
// Customized NotFound handler
|
||||||
e.NotFoundHandler(func(c *Context) {
|
e.NotFoundHandler(func(c *Context) {
|
||||||
c.String(404, "not found")
|
c.String(http.StatusNotFound, "not found")
|
||||||
})
|
})
|
||||||
w = httptest.NewRecorder()
|
w = httptest.NewRecorder()
|
||||||
e.ServeHTTP(w, r)
|
e.ServeHTTP(w, r)
|
||||||
|
@ -7,6 +7,8 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
|
|
||||||
"github.com/labstack/echo"
|
"github.com/labstack/echo"
|
||||||
|
"github.com/rs/cors"
|
||||||
|
"github.com/thoas/stats"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -57,6 +59,21 @@ func main() {
|
|||||||
// Middleware
|
// Middleware
|
||||||
e.Use(echo.Logger)
|
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
|
// Serve index file
|
||||||
e.Index("public/index.html")
|
e.Index("public/index.html")
|
||||||
|
|
||||||
@ -66,6 +83,7 @@ func main() {
|
|||||||
//--------
|
//--------
|
||||||
// Routes
|
// Routes
|
||||||
//--------
|
//--------
|
||||||
|
|
||||||
e.Post("/users", createUser)
|
e.Post("/users", createUser)
|
||||||
e.Get("/users", getUsers)
|
e.Get("/users", getUsers)
|
||||||
e.Get("/users/:id", getUser)
|
e.Get("/users/:id", getUser)
|
||||||
@ -73,6 +91,7 @@ func main() {
|
|||||||
//-----------
|
//-----------
|
||||||
// Templates
|
// Templates
|
||||||
//-----------
|
//-----------
|
||||||
|
|
||||||
t := &Template{
|
t := &Template{
|
||||||
// Cached templates
|
// Cached templates
|
||||||
templates: template.Must(template.ParseFiles("public/views/welcome.html")),
|
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)`
|
||||||
- `func(*echo.Context) error`
|
- `func(*echo.Context) error`
|
||||||
- `func(echo.HandlerFunc) echo.HandlerFunc`
|
- `func(echo.HandlerFunc) echo.HandlerFunc`
|
||||||
|
- `func(http.Handler) http.Handler`
|
||||||
- `http.Handler`
|
- `http.Handler`
|
||||||
- `http.HandlerFunc`
|
- `http.HandlerFunc`
|
||||||
- `func(http.ResponseWriter, *http.Request)`
|
- `func(http.ResponseWriter, *http.Request)`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user