mirror of
https://github.com/labstack/echo.git
synced 2025-06-25 00:47:01 +02:00
Added echo.HandlerFunc to handlers
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
@ -16,8 +16,9 @@ Echo is a fast HTTP router (zero memory allocation) and micro web framework in G
|
|||||||
- `func(http.ResponseWriter, *http.Request)`
|
- `func(http.ResponseWriter, *http.Request)`
|
||||||
- `func(http.ResponseWriter, *http.Request) error`
|
- `func(http.ResponseWriter, *http.Request) error`
|
||||||
- Handler
|
- Handler
|
||||||
- `func(*echo.Context)`
|
- `echo.HandlerFunc`
|
||||||
- `func(*echo.Context) error`
|
- `func(*echo.Context) error`
|
||||||
|
- `func(*echo.Context)`
|
||||||
- `http.Handler`
|
- `http.Handler`
|
||||||
- `http.HandlerFunc`
|
- `http.HandlerFunc`
|
||||||
- `func(http.ResponseWriter, *http.Request)`
|
- `func(http.ResponseWriter, *http.Request)`
|
||||||
@ -110,7 +111,7 @@ func main() {
|
|||||||
## Contribute
|
## Contribute
|
||||||
|
|
||||||
**Use issues for everything**
|
**Use issues for everything**
|
||||||
|
|
||||||
- Report problems
|
- Report problems
|
||||||
- Discuss before sending pull request
|
- Discuss before sending pull request
|
||||||
- Suggest new features
|
- Suggest new features
|
||||||
|
6
echo.go
6
echo.go
@ -398,13 +398,15 @@ func wrapM(m Middleware) MiddlewareFunc {
|
|||||||
// wraps Handler
|
// wraps Handler
|
||||||
func wrapH(h Handler) HandlerFunc {
|
func wrapH(h Handler) HandlerFunc {
|
||||||
switch h := h.(type) {
|
switch h := h.(type) {
|
||||||
|
case HandlerFunc:
|
||||||
|
return h
|
||||||
|
case func(*Context) error:
|
||||||
|
return h
|
||||||
case func(*Context):
|
case func(*Context):
|
||||||
return func(c *Context) error {
|
return func(c *Context) error {
|
||||||
h(c)
|
h(c)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
case func(*Context) error:
|
|
||||||
return h
|
|
||||||
case http.Handler, http.HandlerFunc:
|
case http.Handler, http.HandlerFunc:
|
||||||
return func(c *Context) error {
|
return func(c *Context) error {
|
||||||
h.(http.Handler).ServeHTTP(c.Response, c.Request)
|
h.(http.Handler).ServeHTTP(c.Response, c.Request)
|
||||||
|
35
echo_test.go
35
echo_test.go
@ -121,7 +121,7 @@ func TestEchoMiddleware(t *testing.T) {
|
|||||||
func TestEchoHandler(t *testing.T) {
|
func TestEchoHandler(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
|
|
||||||
// func(*echo.Context)
|
// func(*echo.Context) error
|
||||||
e.Get("/1", func(c *Context) {
|
e.Get("/1", func(c *Context) {
|
||||||
c.String(http.StatusOK, "1")
|
c.String(http.StatusOK, "1")
|
||||||
})
|
})
|
||||||
@ -132,7 +132,7 @@ func TestEchoHandler(t *testing.T) {
|
|||||||
t.Error("body should be 1")
|
t.Error("body should be 1")
|
||||||
}
|
}
|
||||||
|
|
||||||
// func(*echo.Context) error
|
// HandlerFunc
|
||||||
e.Get("/2", func(c *Context) {
|
e.Get("/2", func(c *Context) {
|
||||||
c.String(http.StatusOK, "2")
|
c.String(http.StatusOK, "2")
|
||||||
})
|
})
|
||||||
@ -143,10 +143,10 @@ func TestEchoHandler(t *testing.T) {
|
|||||||
t.Error("body should be 2")
|
t.Error("body should be 2")
|
||||||
}
|
}
|
||||||
|
|
||||||
// http.Handler/http.HandlerFunc
|
// func(*echo.Context)
|
||||||
e.Get("/3", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
e.Get("/3", func(c *Context) {
|
||||||
w.Write([]byte("3"))
|
c.String(http.StatusOK, "3")
|
||||||
}))
|
})
|
||||||
w = httptest.NewRecorder()
|
w = httptest.NewRecorder()
|
||||||
r, _ = http.NewRequest(GET, "/3", nil)
|
r, _ = http.NewRequest(GET, "/3", nil)
|
||||||
e.ServeHTTP(w, r)
|
e.ServeHTTP(w, r)
|
||||||
@ -154,10 +154,10 @@ func TestEchoHandler(t *testing.T) {
|
|||||||
t.Error("body should be 3")
|
t.Error("body should be 3")
|
||||||
}
|
}
|
||||||
|
|
||||||
// func(http.ResponseWriter, *http.Request)
|
// http.Handler/http.HandlerFunc
|
||||||
e.Get("/4", func(w http.ResponseWriter, r *http.Request) {
|
e.Get("/4", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("4"))
|
w.Write([]byte("4"))
|
||||||
})
|
}))
|
||||||
w = httptest.NewRecorder()
|
w = httptest.NewRecorder()
|
||||||
r, _ = http.NewRequest(GET, "/4", nil)
|
r, _ = http.NewRequest(GET, "/4", nil)
|
||||||
e.ServeHTTP(w, r)
|
e.ServeHTTP(w, r)
|
||||||
@ -165,10 +165,9 @@ func TestEchoHandler(t *testing.T) {
|
|||||||
t.Error("body should be 4")
|
t.Error("body should be 4")
|
||||||
}
|
}
|
||||||
|
|
||||||
// func(http.ResponseWriter, *http.Request) error
|
// func(http.ResponseWriter, *http.Request)
|
||||||
e.Get("/5", func(w http.ResponseWriter, r *http.Request) error {
|
e.Get("/5", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Write([]byte("5"))
|
w.Write([]byte("5"))
|
||||||
return nil
|
|
||||||
})
|
})
|
||||||
w = httptest.NewRecorder()
|
w = httptest.NewRecorder()
|
||||||
r, _ = http.NewRequest(GET, "/5", nil)
|
r, _ = http.NewRequest(GET, "/5", nil)
|
||||||
@ -176,6 +175,18 @@ func TestEchoHandler(t *testing.T) {
|
|||||||
if w.Body.String() != "5" {
|
if w.Body.String() != "5" {
|
||||||
t.Error("body should be 5")
|
t.Error("body should be 5")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// func(http.ResponseWriter, *http.Request) error
|
||||||
|
e.Get("/6", func(w http.ResponseWriter, r *http.Request) error {
|
||||||
|
w.Write([]byte("6"))
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
w = httptest.NewRecorder()
|
||||||
|
r, _ = http.NewRequest(GET, "/6", nil)
|
||||||
|
e.ServeHTTP(w, r)
|
||||||
|
if w.Body.String() != "6" {
|
||||||
|
t.Error("body should be 6")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEchoGroup(t *testing.T) {
|
func TestEchoGroup(t *testing.T) {
|
||||||
|
@ -22,8 +22,9 @@ Echo is a fast HTTP router (zero memory allocation) and micro web framework in G
|
|||||||
- `func(http.ResponseWriter, *http.Request)`
|
- `func(http.ResponseWriter, *http.Request)`
|
||||||
- `func(http.ResponseWriter, *http.Request) error`
|
- `func(http.ResponseWriter, *http.Request) error`
|
||||||
- Handler
|
- Handler
|
||||||
- `func(*echo.Context)`
|
- `echo.HandlerFunc`
|
||||||
- `func(*echo.Context) error`
|
- `func(*echo.Context) error`
|
||||||
|
- `func(*echo.Context)`
|
||||||
- `http.Handler`
|
- `http.Handler`
|
||||||
- `http.HandlerFunc`
|
- `http.HandlerFunc`
|
||||||
- `func(http.ResponseWriter, *http.Request)`
|
- `func(http.ResponseWriter, *http.Request)`
|
||||||
@ -112,7 +113,7 @@ Hello, World! on the page.
|
|||||||
## Contribute
|
## Contribute
|
||||||
|
|
||||||
**Use issues for everything**
|
**Use issues for everything**
|
||||||
|
|
||||||
- Report problems
|
- Report problems
|
||||||
- Discuss before sending pull request
|
- Discuss before sending pull request
|
||||||
- Suggest new features
|
- Suggest new features
|
||||||
|
Reference in New Issue
Block a user