mirror of
https://github.com/labstack/echo.git
synced 2024-11-28 08:38:39 +02:00
Fixed examples with new HandlerFunc changes
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
13ac746093
commit
a4375c4991
37
README.md
37
README.md
@ -9,14 +9,14 @@ Echo is a fast HTTP router (zero memory allocation) and micro web framework in G
|
||||
- `echo.MiddlewareFunc`
|
||||
- `func(echo.HandlerFunc) echo.HandlerFunc`
|
||||
- `echo.HandlerFunc`
|
||||
- `func(*echo.Context) *echo.HTTPError`
|
||||
- `func(*echo.Context) error`
|
||||
- `func(http.Handler) http.Handler`
|
||||
- `http.Handler`
|
||||
- `http.HandlerFunc`
|
||||
- `func(http.ResponseWriter, *http.Request)`
|
||||
- Handler
|
||||
- `echo.HandlerFunc`
|
||||
- `func(*echo.Context) *echo.HTTPError`
|
||||
- `func(*echo.Context) error`
|
||||
- `http.Handler`
|
||||
- `http.HandlerFunc`
|
||||
- `func(http.ResponseWriter, *http.Request)`
|
||||
@ -84,7 +84,7 @@ import (
|
||||
)
|
||||
|
||||
// Handler
|
||||
func hello(c *echo.Context) *echo.HTTPError {
|
||||
func hello(c *echo.Context) error {
|
||||
return c.String(http.StatusOK, "Hello, World!\n")
|
||||
}
|
||||
|
||||
@ -218,22 +218,19 @@ var (
|
||||
)
|
||||
|
||||
// Render HTML
|
||||
func (t *Template) Render(w io.Writer, name string, data interface{}) *echo.HTTPError {
|
||||
if err := t.templates.ExecuteTemplate(w, name, data); err != nil {
|
||||
return &echo.HTTPError{Error: err}
|
||||
}
|
||||
return nil
|
||||
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
|
||||
return t.templates.ExecuteTemplate(w, name, data)
|
||||
}
|
||||
|
||||
//----------
|
||||
// Handlers
|
||||
//----------
|
||||
|
||||
func welcome(c *echo.Context) *echo.HTTPError {
|
||||
func welcome(c *echo.Context) error {
|
||||
return c.Render(http.StatusOK, "welcome", "Joe")
|
||||
}
|
||||
|
||||
func createUser(c *echo.Context) *echo.HTTPError {
|
||||
func createUser(c *echo.Context) error {
|
||||
u := new(user)
|
||||
if err := c.Bind(u); err != nil {
|
||||
return err
|
||||
@ -242,11 +239,11 @@ func createUser(c *echo.Context) *echo.HTTPError {
|
||||
return c.JSON(http.StatusCreated, u)
|
||||
}
|
||||
|
||||
func getUsers(c *echo.Context) *echo.HTTPError {
|
||||
func getUsers(c *echo.Context) error {
|
||||
return c.JSON(http.StatusOK, users)
|
||||
}
|
||||
|
||||
func getUser(c *echo.Context) *echo.HTTPError {
|
||||
func getUser(c *echo.Context) error {
|
||||
return c.JSON(http.StatusOK, users[c.P(0)])
|
||||
}
|
||||
|
||||
@ -268,7 +265,7 @@ func main() {
|
||||
s := stats.New()
|
||||
e.Use(s.Handler)
|
||||
// Route
|
||||
e.Get("/stats", func(c *echo.Context) *echo.HTTPError {
|
||||
e.Get("/stats", func(c *echo.Context) error {
|
||||
return c.JSON(http.StatusOK, s.Data())
|
||||
})
|
||||
|
||||
@ -297,7 +294,7 @@ func main() {
|
||||
// Cached templates
|
||||
templates: template.Must(template.ParseFiles("public/views/welcome.html")),
|
||||
}
|
||||
e.Renderer(t)
|
||||
e.SetRenderer(t)
|
||||
e.Get("/welcome", welcome)
|
||||
|
||||
//-------
|
||||
@ -306,20 +303,20 @@ func main() {
|
||||
|
||||
// Group with parent middleware
|
||||
a := e.Group("/admin")
|
||||
a.Use(func(c *echo.Context) *echo.HTTPError {
|
||||
a.Use(func(c *echo.Context) error {
|
||||
// Security middleware
|
||||
return nil
|
||||
})
|
||||
a.Get("", func(c *echo.Context) *echo.HTTPError {
|
||||
a.Get("", func(c *echo.Context) error {
|
||||
return c.String(http.StatusOK, "Welcome admin!")
|
||||
})
|
||||
|
||||
// Group with no parent middleware
|
||||
g := e.Group("/files", func(c *echo.Context) *echo.HTTPError {
|
||||
g := e.Group("/files", func(c *echo.Context) error {
|
||||
// Security middleware
|
||||
return nil
|
||||
})
|
||||
g.Get("", func(c *echo.Context) *echo.HTTPError {
|
||||
g.Get("", func(c *echo.Context) error {
|
||||
return c.String(http.StatusOK, "Your files!")
|
||||
})
|
||||
|
||||
@ -350,7 +347,7 @@ import (
|
||||
)
|
||||
|
||||
// Handler
|
||||
func hello(c *echo.Context) *echo.HTTPError {
|
||||
func hello(c *echo.Context) error {
|
||||
return c.String(http.StatusOK, "Hello, World!\n")
|
||||
}
|
||||
|
||||
@ -359,7 +356,7 @@ func main() {
|
||||
e := echo.New()
|
||||
|
||||
// Debug mode
|
||||
e.Debug(true)
|
||||
e.SetDebug(true)
|
||||
|
||||
//------------
|
||||
// Middleware
|
||||
|
10
echo.go
10
echo.go
@ -120,6 +120,14 @@ var (
|
||||
RendererNotRegistered = errors.New("echo ⇒ renderer not registered")
|
||||
)
|
||||
|
||||
func NewHTTPError(code int, msgs ...string) *HTTPError {
|
||||
he := &HTTPError{Code: code}
|
||||
if len(msgs) == 0 {
|
||||
he.Message = http.StatusText(code)
|
||||
}
|
||||
return he
|
||||
}
|
||||
|
||||
func (e *HTTPError) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
@ -140,7 +148,7 @@ func New() (e *Echo) {
|
||||
|
||||
e.SetMaxParam(5)
|
||||
e.notFoundHandler = func(c *Context) error {
|
||||
return &HTTPError{Code: http.StatusNotFound}
|
||||
return NewHTTPError(http.StatusNotFound)
|
||||
}
|
||||
e.SetHTTPErrorHandler(func(err error, c *Context) {
|
||||
code := http.StatusInternalServerError
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
// Handler
|
||||
func hello(c *echo.Context) *echo.HTTPError {
|
||||
func hello(c *echo.Context) error {
|
||||
return c.String(http.StatusOK, "Hello, World!\n")
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
// Handler
|
||||
func hello(c *echo.Context) *echo.HTTPError {
|
||||
func hello(c *echo.Context) error {
|
||||
return c.String(http.StatusOK, "Hello, World!\n")
|
||||
}
|
||||
|
||||
|
@ -29,22 +29,19 @@ var (
|
||||
)
|
||||
|
||||
// Render HTML
|
||||
func (t *Template) Render(w io.Writer, name string, data interface{}) *echo.HTTPError {
|
||||
if err := t.templates.ExecuteTemplate(w, name, data); err != nil {
|
||||
return &echo.HTTPError{Error: err}
|
||||
}
|
||||
return nil
|
||||
func (t *Template) Render(w io.Writer, name string, data interface{}) error {
|
||||
return t.templates.ExecuteTemplate(w, name, data)
|
||||
}
|
||||
|
||||
//----------
|
||||
// Handlers
|
||||
//----------
|
||||
|
||||
func welcome(c *echo.Context) *echo.HTTPError {
|
||||
func welcome(c *echo.Context) error {
|
||||
return c.Render(http.StatusOK, "welcome", "Joe")
|
||||
}
|
||||
|
||||
func createUser(c *echo.Context) *echo.HTTPError {
|
||||
func createUser(c *echo.Context) error {
|
||||
u := new(user)
|
||||
if err := c.Bind(u); err != nil {
|
||||
return err
|
||||
@ -53,11 +50,11 @@ func createUser(c *echo.Context) *echo.HTTPError {
|
||||
return c.JSON(http.StatusCreated, u)
|
||||
}
|
||||
|
||||
func getUsers(c *echo.Context) *echo.HTTPError {
|
||||
func getUsers(c *echo.Context) error {
|
||||
return c.JSON(http.StatusOK, users)
|
||||
}
|
||||
|
||||
func getUser(c *echo.Context) *echo.HTTPError {
|
||||
func getUser(c *echo.Context) error {
|
||||
return c.JSON(http.StatusOK, users[c.P(0)])
|
||||
}
|
||||
|
||||
@ -79,7 +76,7 @@ func main() {
|
||||
s := stats.New()
|
||||
e.Use(s.Handler)
|
||||
// Route
|
||||
e.Get("/stats", func(c *echo.Context) *echo.HTTPError {
|
||||
e.Get("/stats", func(c *echo.Context) error {
|
||||
return c.JSON(http.StatusOK, s.Data())
|
||||
})
|
||||
|
||||
@ -117,20 +114,20 @@ func main() {
|
||||
|
||||
// Group with parent middleware
|
||||
a := e.Group("/admin")
|
||||
a.Use(func(c *echo.Context) *echo.HTTPError {
|
||||
a.Use(func(c *echo.Context) error {
|
||||
// Security middleware
|
||||
return nil
|
||||
})
|
||||
a.Get("", func(c *echo.Context) *echo.HTTPError {
|
||||
a.Get("", func(c *echo.Context) error {
|
||||
return c.String(http.StatusOK, "Welcome admin!")
|
||||
})
|
||||
|
||||
// Group with no parent middleware
|
||||
g := e.Group("/files", func(c *echo.Context) *echo.HTTPError {
|
||||
g := e.Group("/files", func(c *echo.Context) error {
|
||||
// Security middleware
|
||||
return nil
|
||||
})
|
||||
g.Get("", func(c *echo.Context) *echo.HTTPError {
|
||||
g.Get("", func(c *echo.Context) error {
|
||||
return c.String(http.StatusOK, "Your files!")
|
||||
})
|
||||
|
||||
|
@ -19,7 +19,7 @@ func BasicAuth(fn AuthFunc) echo.HandlerFunc {
|
||||
return func(c *echo.Context) error {
|
||||
auth := c.Request.Header.Get(echo.Authorization)
|
||||
i := 0
|
||||
he := &echo.HTTPError{Code: http.StatusUnauthorized}
|
||||
he := echo.NewHTTPError(http.StatusUnauthorized)
|
||||
|
||||
for ; i < len(auth); i++ {
|
||||
c := auth[i]
|
||||
|
@ -41,16 +41,16 @@ for many use cases. Restricting path parameters allows us to use memory efficien
|
||||
|
||||
Registers a custom `Echo.HTTPErrorHandler`.
|
||||
|
||||
Default handler sends `HTTPError.Message` HTTP response with `HTTPError.Code` status
|
||||
code.
|
||||
Default handler rules
|
||||
|
||||
- If HTTPError.Code is not set it uses `500`".
|
||||
- If HTTPError.Message is not set it uses status code text.
|
||||
- If debug mode is enabled, HTTPError.Message is set to `HTTPError.Error.Error()`.
|
||||
- If error is of type `Echo.HTTPError` it sends HTTP response with status code `HTTPError.Code`
|
||||
and message `HTTPError.Message`.
|
||||
- Else it sends `500 - Internal Server Error`.
|
||||
- If debug mode is enabled, it uses `error.Error()` as status message.
|
||||
|
||||
### Debug
|
||||
|
||||
`echo.SetDebug(on bool)`
|
||||
`Echo.SetDebug(on bool)`
|
||||
|
||||
Enables debug mode.
|
||||
|
||||
@ -67,12 +67,12 @@ code below registers a route for method `GET`, path `/hello` and a handler which
|
||||
`Hello!` HTTP response.
|
||||
|
||||
```go
|
||||
echo.Get("/hello", func(*echo.Context) *HTTPError {
|
||||
echo.Get("/hello", func(*echo.Context) error {
|
||||
return c.String(http.StatusOK, "Hello!")
|
||||
})
|
||||
```
|
||||
|
||||
Echo's default handler is `func(*echo.Context) *echo.HTTPError` where `echo.Context`
|
||||
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.
|
||||
|
||||
@ -89,7 +89,7 @@ or by index `echo.Context.P(i uint8) string`. Getting parameter by index gives a
|
||||
slightly better performance.
|
||||
|
||||
```go
|
||||
echo.Get("/users/:id", func(c *echo.Context) *HTTPError {
|
||||
echo.Get("/users/:id", func(c *echo.Context) error {
|
||||
// By name
|
||||
id := c.Param("id")
|
||||
|
||||
@ -119,15 +119,15 @@ match
|
||||
#### Example
|
||||
|
||||
```go
|
||||
e.Get("/users/:id", func(c *echo.Context) *HTTPError {
|
||||
e.Get("/users/:id", func(c *echo.Context) error {
|
||||
return c.String(http.StatusOK, "/users/:id")
|
||||
})
|
||||
|
||||
e.Get("/users/new", func(c *echo.Context) *HTTPError {
|
||||
e.Get("/users/new", func(c *echo.Context) error {
|
||||
return c.String(http.StatusOK, "/users/new")
|
||||
})
|
||||
|
||||
e.Get("/users/1/files/*", func(c *echo.Context) *HTTPError {
|
||||
e.Get("/users/1/files/*", func(c *echo.Context) error {
|
||||
return c.String(http.StatusOK, "/users/1/files/*")
|
||||
})
|
||||
```
|
||||
@ -152,7 +152,7 @@ application.
|
||||
|
||||
```go
|
||||
// Handler
|
||||
h := func(*echo.Context) *HTTPError {
|
||||
h := func(*echo.Context) error {
|
||||
return c.String(http.StatusOK, "OK")
|
||||
}
|
||||
|
||||
@ -169,7 +169,7 @@ e.Get("/users/:id", h)
|
||||
### JSON
|
||||
|
||||
```go
|
||||
context.JSON(code int, v interface{}) *HTTPError
|
||||
context.JSON(code int, v interface{}) error
|
||||
```
|
||||
|
||||
Sends a JSON HTTP response with status code.
|
||||
@ -177,7 +177,7 @@ Sends a JSON HTTP response with status code.
|
||||
### String
|
||||
|
||||
```go
|
||||
context.String(code int, s string) *HTTPError
|
||||
context.String(code int, s string) error
|
||||
```
|
||||
|
||||
Sends a text/plain HTTP response with status code.
|
||||
@ -185,7 +185,7 @@ Sends a text/plain HTTP response with status code.
|
||||
### HTML
|
||||
|
||||
```go
|
||||
func (c *Context) HTML(code int, html string) *HTTPError
|
||||
func (c *Context) HTML(code int, html string) error
|
||||
```
|
||||
|
||||
Sends an HTML HTTP response with status code.
|
||||
@ -228,8 +228,8 @@ e.Favicon("public/favicon.ico")
|
||||
|
||||
## Error Handling
|
||||
|
||||
Echo advocates centralized HTTP error handling by returning `*echo.HTTPError` from
|
||||
middleware and handlers.
|
||||
Echo advocates centralized HTTP error handling by returning `error` from middleware
|
||||
and handlers.
|
||||
|
||||
It allows you to
|
||||
|
||||
@ -237,8 +237,8 @@ It allows you to
|
||||
- Customize HTTP responses.
|
||||
- Recover from panics inside middleware or handlers.
|
||||
|
||||
For example, when a basic auth middleware finds invalid credentials it returns 401
|
||||
"Unauthorized" error, aborting the current HTTP request.
|
||||
For example, when a basic auth middleware finds invalid credentials it returns
|
||||
`401 - Unauthorized` error, aborting the current HTTP request.
|
||||
|
||||
```go
|
||||
package main
|
||||
@ -251,18 +251,18 @@ import (
|
||||
|
||||
func main() {
|
||||
e := echo.New()
|
||||
e.Use(func(c *echo.Context) *echo.HTTPError {
|
||||
e.Use(func(c *echo.Context) error {
|
||||
// Extract the credentials from HTTP request header and perform a security
|
||||
// check
|
||||
|
||||
// For invalid credentials
|
||||
return &echo.HTTPError{Code: http.StatusUnauthorized}
|
||||
return echo.NewHTTPError(http.StatusUnauthorized)
|
||||
})
|
||||
e.Get("/welcome", welcome)
|
||||
e.Run(":1323")
|
||||
}
|
||||
|
||||
func welcome(c *echo.Context) *echo.HTTPError {
|
||||
func welcome(c *echo.Context) error {
|
||||
return c.String(http.StatusOK, "Welcome!")
|
||||
}
|
||||
```
|
||||
|
@ -16,14 +16,14 @@ Echo is a fast HTTP router (zero memory allocation) and micro web framework in G
|
||||
- `echo.MiddlewareFunc`
|
||||
- `func(echo.HandlerFunc) echo.HandlerFunc`
|
||||
- `echo.HandlerFunc`
|
||||
- `func(*echo.Context) *echo.HTTPError`
|
||||
- `func(*echo.Context) error`
|
||||
- `func(http.Handler) http.Handler`
|
||||
- `http.Handler`
|
||||
- `http.HandlerFunc`
|
||||
- `func(http.ResponseWriter, *http.Request)`
|
||||
- Handler
|
||||
- `echo.HandlerFunc`
|
||||
- `func(*echo.Context) *echo.HTTPError`
|
||||
- `func(*echo.Context) error`
|
||||
- `http.Handler`
|
||||
- `http.HandlerFunc`
|
||||
- `func(http.ResponseWriter, *http.Request)`
|
||||
@ -57,7 +57,7 @@ import (
|
||||
)
|
||||
|
||||
// Handler
|
||||
func hello(c *echo.Context) *echo.HTTPError {
|
||||
func hello(c *echo.Context) error {
|
||||
return c.String(http.StatusOK, "Hello, World!\n")
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user