mirror of
https://github.com/labstack/echo.git
synced 2025-01-12 01:22:21 +02:00
Refactored echo configuration functions
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
cd1e235ccc
commit
ac4d019a9a
104
echo.go
104
echo.go
@ -134,23 +134,23 @@ func New() (e *Echo) {
|
|||||||
// Defaults
|
// Defaults
|
||||||
//----------
|
//----------
|
||||||
|
|
||||||
e.MaxParam(5)
|
e.SetMaxParam(5)
|
||||||
e.notFoundHandler = func(c *Context) *HTTPError {
|
e.notFoundHandler = func(c *Context) *HTTPError {
|
||||||
return &HTTPError{Code: http.StatusNotFound}
|
return &HTTPError{Code: http.StatusNotFound}
|
||||||
}
|
}
|
||||||
e.HTTPErrorHandler(func(he *HTTPError, c *Context) {
|
e.SetHTTPErrorHandler(func(he *HTTPError, c *Context) {
|
||||||
if he.Code == 0 {
|
if he.Code == 0 {
|
||||||
he.Code = http.StatusInternalServerError
|
he.Code = http.StatusInternalServerError
|
||||||
}
|
}
|
||||||
if he.Message == "" {
|
if he.Message == "" {
|
||||||
he.Message = http.StatusText(he.Code)
|
he.Message = http.StatusText(he.Code)
|
||||||
if e.debug && he.Error != nil {
|
}
|
||||||
he.Message = he.Error.Error()
|
if e.debug && he.Error != nil {
|
||||||
}
|
he.Message = he.Error.Error()
|
||||||
}
|
}
|
||||||
http.Error(c.Response, he.Message, he.Code)
|
http.Error(c.Response, he.Message, he.Code)
|
||||||
})
|
})
|
||||||
e.Binder(func(r *http.Request, v interface{}) *HTTPError {
|
e.SetBinder(func(r *http.Request, v interface{}) *HTTPError {
|
||||||
ct := r.Header.Get(ContentType)
|
ct := r.Header.Get(ContentType)
|
||||||
err := UnsupportedMediaType
|
err := UnsupportedMediaType
|
||||||
if strings.HasPrefix(ct, ApplicationJSON) {
|
if strings.HasPrefix(ct, ApplicationJSON) {
|
||||||
@ -178,33 +178,37 @@ func (e *Echo) Group(pfx string, m ...Middleware) *Echo {
|
|||||||
return &g
|
return &g
|
||||||
}
|
}
|
||||||
|
|
||||||
// MaxParam sets the maximum number of path parameters allowed for the application.
|
// SetMaxParam sets the maximum number of path parameters allowed for the application.
|
||||||
// Default value is 5, good enough for many use cases.
|
// Default value is 5, good enough for many use cases.
|
||||||
func (e *Echo) MaxParam(n uint8) {
|
func (e *Echo) SetMaxParam(n uint8) {
|
||||||
e.maxParam = n
|
e.maxParam = n
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTTPErrorHandler registers an HTTP error handler.
|
// SetHTTPErrorHandler registers an Echo.HTTPErrorHandler.
|
||||||
func (e *Echo) HTTPErrorHandler(h HTTPErrorHandler) {
|
func (e *Echo) SetHTTPErrorHandler(h HTTPErrorHandler) {
|
||||||
e.httpErrorHandler = h
|
e.httpErrorHandler = h
|
||||||
}
|
}
|
||||||
|
|
||||||
// Binder registers a custom binder. It's invoked by Context.Bind API.
|
// SetBinder registers a custom binder. It's invoked by Context.Bind().
|
||||||
func (e *Echo) Binder(b BindFunc) {
|
func (e *Echo) SetBinder(b BindFunc) {
|
||||||
e.binder = b
|
e.binder = b
|
||||||
}
|
}
|
||||||
|
|
||||||
// Renderer registers an HTML template renderer. It's invoked by Context.Render
|
// SetRenderer registers an HTML template renderer. It's invoked by Context.Render().
|
||||||
// API.
|
func (e *Echo) SetRenderer(r Renderer) {
|
||||||
func (e *Echo) Renderer(r Renderer) {
|
|
||||||
e.renderer = r
|
e.renderer = r
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debug runs the application in debug mode.
|
// SetDebug sets debug mode.
|
||||||
func (e *Echo) Debug(on bool) {
|
func (e *Echo) SetDebug(on bool) {
|
||||||
e.debug = on
|
e.debug = on
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Debug returns debug mode.
|
||||||
|
func (e *Echo) Debug() bool {
|
||||||
|
return e.debug
|
||||||
|
}
|
||||||
|
|
||||||
// Use adds handler to the middleware chain.
|
// Use adds handler to the middleware chain.
|
||||||
func (e *Echo) Use(m ...Middleware) {
|
func (e *Echo) Use(m ...Middleware) {
|
||||||
for _, h := range m {
|
for _, h := range m {
|
||||||
@ -257,6 +261,39 @@ func (e *Echo) Trace(path string, h Handler) {
|
|||||||
e.add(TRACE, path, h)
|
e.add(TRACE, path, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *Echo) add(method, path string, h Handler) {
|
||||||
|
key := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
|
||||||
|
e.uris[key] = path
|
||||||
|
e.Router.Add(method, e.prefix+path, wrapHandler(h), e)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Index serves index file.
|
||||||
|
func (e *Echo) Index(file string) {
|
||||||
|
e.ServeFile("/", file)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Favicon serves the default favicon - GET /favicon.ico.
|
||||||
|
func (e *Echo) Favicon(file string) {
|
||||||
|
e.ServeFile("/favicon.ico", file)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Static serves static files.
|
||||||
|
func (e *Echo) Static(path, root string) {
|
||||||
|
fs := http.StripPrefix(path, http.FileServer(http.Dir(root)))
|
||||||
|
e.Get(path+"*", func(c *Context) *HTTPError {
|
||||||
|
fs.ServeHTTP(c.Response, c.Request)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServeFile serves a file.
|
||||||
|
func (e *Echo) ServeFile(path, file string) {
|
||||||
|
e.Get(path, func(c *Context) *HTTPError {
|
||||||
|
http.ServeFile(c.Response, c.Request, file)
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// URI generates a URI from handler.
|
// URI generates a URI from handler.
|
||||||
func (e *Echo) URI(h Handler, params ...interface{}) string {
|
func (e *Echo) URI(h Handler, params ...interface{}) string {
|
||||||
uri := new(bytes.Buffer)
|
uri := new(bytes.Buffer)
|
||||||
@ -284,39 +321,6 @@ func (e *Echo) URL(h Handler, params ...interface{}) string {
|
|||||||
return e.URI(h, params...)
|
return e.URI(h, params...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Echo) add(method, path string, h Handler) {
|
|
||||||
key := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
|
|
||||||
e.uris[key] = path
|
|
||||||
e.Router.Add(method, e.prefix+path, wrapHandler(h), e)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Static serves static files.
|
|
||||||
func (e *Echo) Static(path, root string) {
|
|
||||||
fs := http.StripPrefix(path, http.FileServer(http.Dir(root)))
|
|
||||||
e.Get(path+"*", func(c *Context) *HTTPError {
|
|
||||||
fs.ServeHTTP(c.Response, c.Request)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// ServeFile serves a file.
|
|
||||||
func (e *Echo) ServeFile(path, file string) {
|
|
||||||
e.Get(path, func(c *Context) *HTTPError {
|
|
||||||
http.ServeFile(c.Response, c.Request, file)
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// Index serves index file.
|
|
||||||
func (e *Echo) Index(file string) {
|
|
||||||
e.ServeFile("/", file)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Favicon serves the default favicon - GET /favicon.ico.
|
|
||||||
func (e *Echo) Favicon(file string) {
|
|
||||||
e.ServeFile("/favicon.ico", file)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
c := e.pool.Get().(*Context)
|
c := e.pool.Get().(*Context)
|
||||||
h, echo := e.Router.Find(r.Method, r.URL.Path, c)
|
h, echo := e.Router.Find(r.Method, r.URL.Path, c)
|
||||||
|
@ -22,7 +22,7 @@ var u1 = user{
|
|||||||
// TODO: Improve me!
|
// TODO: Improve me!
|
||||||
func TestEchoMaxParam(t *testing.T) {
|
func TestEchoMaxParam(t *testing.T) {
|
||||||
e := New()
|
e := New()
|
||||||
e.MaxParam(8)
|
e.SetMaxParam(8)
|
||||||
if e.maxParam != 8 {
|
if e.maxParam != 8 {
|
||||||
t.Errorf("max param should be 8, found %d", e.maxParam)
|
t.Errorf("max param should be 8, found %d", e.maxParam)
|
||||||
}
|
}
|
||||||
|
@ -108,7 +108,7 @@ func main() {
|
|||||||
// Cached templates
|
// Cached templates
|
||||||
templates: template.Must(template.ParseFiles("public/views/welcome.html")),
|
templates: template.Must(template.ParseFiles("public/views/welcome.html")),
|
||||||
}
|
}
|
||||||
e.Renderer(t)
|
e.SetRenderer(t)
|
||||||
e.Get("/welcome", welcome)
|
e.Get("/welcome", welcome)
|
||||||
|
|
||||||
//-------
|
//-------
|
||||||
|
@ -29,7 +29,7 @@ Specific version of Echo can be installed using any [package manager](https://gi
|
|||||||
|
|
||||||
### Max path parameters
|
### Max path parameters
|
||||||
|
|
||||||
`echo.MaxParam(n uint8)`
|
`Echo.SetMaxParam(n uint8)`
|
||||||
|
|
||||||
Sets the maximum number of path parameters allowed for the application.
|
Sets the maximum number of path parameters allowed for the application.
|
||||||
Default value is **5**, [good enough](https://github.com/interagent/http-api-design#minimize-path-nesting)
|
Default value is **5**, [good enough](https://github.com/interagent/http-api-design#minimize-path-nesting)
|
||||||
@ -37,16 +37,22 @@ for many use cases. Restricting path parameters allows us to use memory efficien
|
|||||||
|
|
||||||
### HTTP error handler
|
### HTTP error handler
|
||||||
|
|
||||||
`echo.HTTPErrorHandler(h HTTPErrorHandler)`
|
`Echo.SetHTTPErrorHandler(h HTTPErrorHandler)`
|
||||||
|
|
||||||
Registers a custom centralized HTTP error handler `func(*HTTPError, *Context)`.
|
Registers a custom `Echo.HTTPErrorHandler`.
|
||||||
|
|
||||||
Default handler sends `HTTPError.Message` HTTP response with `HTTPError.Code` status
|
Default handler sends `HTTPError.Message` HTTP response with `HTTPError.Code` status
|
||||||
code.
|
code.
|
||||||
|
|
||||||
- If HTTPError.Code is not specified it uses "500 - Internal Server Error".
|
- If HTTPError.Code is not set it uses `500`".
|
||||||
- If HTTPError.Message is not specified it uses HTTPError.Error.Error() or the status
|
- If HTTPError.Message is not set it uses status code text.
|
||||||
code text.
|
- If debug mode is enabled, HTTPError.Message is set to `HTTPError.Error.Error()`.
|
||||||
|
|
||||||
|
### Debug
|
||||||
|
|
||||||
|
`echo.SetDebug(on bool)`
|
||||||
|
|
||||||
|
Enables debug mode.
|
||||||
|
|
||||||
## Routing
|
## Routing
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user