2015-10-01 18:24:38 -07:00
|
|
|
---
|
|
|
|
title: Customization
|
|
|
|
menu:
|
2015-10-08 13:54:31 -07:00
|
|
|
side:
|
2015-10-01 18:24:38 -07:00
|
|
|
parent: guide
|
2015-10-05 15:52:07 -07:00
|
|
|
weight: 2
|
2015-10-01 18:24:38 -07:00
|
|
|
---
|
|
|
|
|
|
|
|
### HTTP error handler
|
|
|
|
|
2015-11-19 07:40:24 -08:00
|
|
|
`Echo#SetHTTPErrorHandler(h HTTPErrorHandler)`
|
2015-10-01 18:24:38 -07:00
|
|
|
|
2015-11-19 07:40:24 -08:00
|
|
|
Registers a custom `Echo#HTTPErrorHandler`.
|
2015-10-01 18:24:38 -07:00
|
|
|
|
2015-10-02 19:48:47 -07:00
|
|
|
Default handler rules:
|
2015-10-01 18:24:38 -07:00
|
|
|
|
2015-11-19 07:40:24 -08:00
|
|
|
- If error is of type `Echo#HTTPError` it sends HTTP response with status code `HTTPError.Code`
|
2015-10-01 18:24:38 -07:00
|
|
|
and message `HTTPError.Message`.
|
|
|
|
- Else it sends `500 - Internal Server Error`.
|
|
|
|
- If debug mode is enabled, it uses `error.Error()` as status message.
|
|
|
|
|
|
|
|
### Debug
|
|
|
|
|
2015-11-19 07:40:24 -08:00
|
|
|
`Echo#SetDebug(on bool)`
|
2015-10-01 18:24:38 -07:00
|
|
|
|
|
|
|
Enables/disables debug mode.
|
|
|
|
|
2015-11-20 19:13:22 -08:00
|
|
|
### Log output
|
2015-10-01 18:24:38 -07:00
|
|
|
|
2015-11-20 19:13:22 -08:00
|
|
|
`echo#SetOutput(w io.Writer)`
|
|
|
|
|
|
|
|
SetOutput sets the output destination for the global logger.
|
|
|
|
|
|
|
|
### Log level
|
|
|
|
|
|
|
|
`echo#SetLogLevel(l log.Level)`
|
|
|
|
|
|
|
|
SetLogLevel sets the log level for global logger. The default value is `log.INFO`.
|
2015-11-19 07:40:24 -08:00
|
|
|
|
2015-11-23 20:33:13 -08:00
|
|
|
### Auto index
|
|
|
|
|
|
|
|
`Echo#AutoIndex(on bool)`
|
|
|
|
|
|
|
|
AutoIndex enables automatically creates a directory listing if the directory doesn't
|
|
|
|
contain an index page.
|
|
|
|
|
|
|
|
*Example*
|
|
|
|
|
|
|
|
```go
|
|
|
|
e := echo.New()
|
|
|
|
e.AutoIndex(true)
|
|
|
|
e.ServerDir("/", "/Users/vr/Projects/echo")
|
|
|
|
e.Run(":1323")
|
|
|
|
```
|
|
|
|
|
|
|
|
Browse to `http://localhost:1323/` to see the directory listing.
|
|
|
|
|
2015-11-19 07:40:24 -08:00
|
|
|
### Hook
|
|
|
|
|
|
|
|
`Echo#Hook(http.HandlerFunc)`
|
|
|
|
|
|
|
|
Hook registers a callback which is invoked from `Echo#ServerHTTP` as the first
|
|
|
|
statement. Hook is useful if you want to modify response/response objects even
|
|
|
|
before it hits the router or any middleware.
|
|
|
|
|
|
|
|
For example, the following hook strips the trailing slash from the request path.
|
|
|
|
|
|
|
|
```go
|
|
|
|
e.Hook(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
path := r.URL.Path
|
|
|
|
l := len(path) - 1
|
|
|
|
if path != "/" && path[l] == '/' {
|
|
|
|
r.URL.Path = path[:l]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
```
|