diff --git a/echo.go b/echo.go index 8fb537a2..85f9df13 100644 --- a/echo.go +++ b/echo.go @@ -134,23 +134,23 @@ func New() (e *Echo) { // Defaults //---------- - e.MaxParam(5) + e.SetMaxParam(5) e.notFoundHandler = func(c *Context) *HTTPError { return &HTTPError{Code: http.StatusNotFound} } - e.HTTPErrorHandler(func(he *HTTPError, c *Context) { + e.SetHTTPErrorHandler(func(he *HTTPError, c *Context) { if he.Code == 0 { he.Code = http.StatusInternalServerError } if he.Message == "" { 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) }) - e.Binder(func(r *http.Request, v interface{}) *HTTPError { + e.SetBinder(func(r *http.Request, v interface{}) *HTTPError { ct := r.Header.Get(ContentType) err := UnsupportedMediaType if strings.HasPrefix(ct, ApplicationJSON) { @@ -178,33 +178,37 @@ func (e *Echo) Group(pfx string, m ...Middleware) *Echo { 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. -func (e *Echo) MaxParam(n uint8) { +func (e *Echo) SetMaxParam(n uint8) { e.maxParam = n } -// HTTPErrorHandler registers an HTTP error handler. -func (e *Echo) HTTPErrorHandler(h HTTPErrorHandler) { +// SetHTTPErrorHandler registers an Echo.HTTPErrorHandler. +func (e *Echo) SetHTTPErrorHandler(h HTTPErrorHandler) { e.httpErrorHandler = h } -// Binder registers a custom binder. It's invoked by Context.Bind API. -func (e *Echo) Binder(b BindFunc) { +// SetBinder registers a custom binder. It's invoked by Context.Bind(). +func (e *Echo) SetBinder(b BindFunc) { e.binder = b } -// Renderer registers an HTML template renderer. It's invoked by Context.Render -// API. -func (e *Echo) Renderer(r Renderer) { +// SetRenderer registers an HTML template renderer. It's invoked by Context.Render(). +func (e *Echo) SetRenderer(r Renderer) { e.renderer = r } -// Debug runs the application in debug mode. -func (e *Echo) Debug(on bool) { +// SetDebug sets debug mode. +func (e *Echo) SetDebug(on bool) { e.debug = on } +// Debug returns debug mode. +func (e *Echo) Debug() bool { + return e.debug +} + // Use adds handler to the middleware chain. func (e *Echo) Use(m ...Middleware) { for _, h := range m { @@ -257,6 +261,39 @@ func (e *Echo) Trace(path string, h Handler) { 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. func (e *Echo) URI(h Handler, params ...interface{}) string { uri := new(bytes.Buffer) @@ -284,39 +321,6 @@ func (e *Echo) URL(h Handler, params ...interface{}) string { 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) { c := e.pool.Get().(*Context) h, echo := e.Router.Find(r.Method, r.URL.Path, c) diff --git a/echo_test.go b/echo_test.go index beb2d4b8..aecc8a5d 100644 --- a/echo_test.go +++ b/echo_test.go @@ -22,7 +22,7 @@ var u1 = user{ // TODO: Improve me! func TestEchoMaxParam(t *testing.T) { e := New() - e.MaxParam(8) + e.SetMaxParam(8) if e.maxParam != 8 { t.Errorf("max param should be 8, found %d", e.maxParam) } diff --git a/examples/website/server.go b/examples/website/server.go index 81868962..8e74d020 100644 --- a/examples/website/server.go +++ b/examples/website/server.go @@ -108,7 +108,7 @@ func main() { // Cached templates templates: template.Must(template.ParseFiles("public/views/welcome.html")), } - e.Renderer(t) + e.SetRenderer(t) e.Get("/welcome", welcome) //------- diff --git a/website/docs/guide.md b/website/docs/guide.md index cd9d175e..65a6af0e 100644 --- a/website/docs/guide.md +++ b/website/docs/guide.md @@ -29,7 +29,7 @@ Specific version of Echo can be installed using any [package manager](https://gi ### Max path parameters -`echo.MaxParam(n uint8)` +`Echo.SetMaxParam(n uint8)` 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) @@ -37,16 +37,22 @@ for many use cases. Restricting path parameters allows us to use memory efficien ### 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 code. -- If HTTPError.Code is not specified it uses "500 - Internal Server Error". -- If HTTPError.Message is not specified it uses HTTPError.Error.Error() or the status -code text. +- 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()`. + +### Debug + +`echo.SetDebug(on bool)` + +Enables debug mode. ## Routing