Skip to content

Commit

Permalink
Merge pull request #73 from labstack/websocket
Browse files Browse the repository at this point in the history
Refactored echo configuration functions
  • Loading branch information
vishr committed May 20, 2015
2 parents 1886fc7 + 60a377a commit a451812
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 59 deletions.
102 changes: 53 additions & 49 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -257,37 +261,20 @@ func (e *Echo) Trace(path string, h Handler) {
e.add(TRACE, path, h)
}

// URI generates a URI from handler.
func (e *Echo) URI(h Handler, params ...interface{}) string {
uri := new(bytes.Buffer)
lp := len(params)
n := 0
func (e *Echo) add(method, path string, h Handler) {
key := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
if path, ok := e.uris[key]; ok {
for i, l := 0, len(path); i < l; i++ {
if path[i] == ':' && n < lp {
for ; i < l && path[i] != '/'; i++ {
}
uri.WriteString(fmt.Sprintf("%v", params[n]))
n++
}
if i < l {
uri.WriteByte(path[i])
}
}
}
return uri.String()
e.uris[key] = path
e.Router.Add(method, e.prefix+path, wrapHandler(h), e)
}

// URL is an alias for URI
func (e *Echo) URL(h Handler, params ...interface{}) string {
return e.URI(h, params...)
// Index serves index file.
func (e *Echo) Index(file string) {
e.ServeFile("/", file)
}

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)
// Favicon serves the default favicon - GET /favicon.ico.
func (e *Echo) Favicon(file string) {
e.ServeFile("/favicon.ico", file)
}

// Static serves static files.
Expand All @@ -307,14 +294,31 @@ func (e *Echo) ServeFile(path, file string) {
})
}

// Index serves index file.
func (e *Echo) Index(file string) {
e.ServeFile("/", file)
// URI generates a URI from handler.
func (e *Echo) URI(h Handler, params ...interface{}) string {
uri := new(bytes.Buffer)
lp := len(params)
n := 0
key := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
if path, ok := e.uris[key]; ok {
for i, l := 0, len(path); i < l; i++ {
if path[i] == ':' && n < lp {
for ; i < l && path[i] != '/'; i++ {
}
uri.WriteString(fmt.Sprintf("%v", params[n]))
n++
}
if i < l {
uri.WriteByte(path[i])
}
}
}
return uri.String()
}

// Favicon serves the default favicon - GET /favicon.ico.
func (e *Echo) Favicon(file string) {
e.ServeFile("/favicon.ico", file)
// URL is an alias for URI
func (e *Echo) URL(h Handler, params ...interface{}) string {
return e.URI(h, params...)
}

func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand Down
2 changes: 1 addition & 1 deletion echo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/middleware/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func main() {
e := echo.New()

// Debug mode
e.Debug(true)
e.SetDebug(true)

//------------
// Middleware
Expand Down
2 changes: 1 addition & 1 deletion examples/website/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

//-------
Expand Down
2 changes: 1 addition & 1 deletion middleware/recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestRecover(t *testing.T) {
e := echo.New()
e.Debug(true)
e.SetDebug(true)
req, _ := http.NewRequest(echo.GET, "/", nil)
w := httptest.NewRecorder()
res := &echo.Response{Writer: w}
Expand Down
18 changes: 12 additions & 6 deletions website/docs/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,30 @@ 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)
for many use cases. Restricting path parameters allows us to use memory efficiently.

### 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

Expand Down

0 comments on commit a451812

Please sign in to comment.