Skip to content

Commit a451812

Browse files
committed
Merge pull request #73 from labstack/websocket
Refactored echo configuration functions
2 parents 1886fc7 + 60a377a commit a451812

File tree

6 files changed

+69
-59
lines changed

6 files changed

+69
-59
lines changed

echo.go

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -134,23 +134,23 @@ func New() (e *Echo) {
134134
// Defaults
135135
//----------
136136

137-
e.MaxParam(5)
137+
e.SetMaxParam(5)
138138
e.notFoundHandler = func(c *Context) *HTTPError {
139139
return &HTTPError{Code: http.StatusNotFound}
140140
}
141-
e.HTTPErrorHandler(func(he *HTTPError, c *Context) {
141+
e.SetHTTPErrorHandler(func(he *HTTPError, c *Context) {
142142
if he.Code == 0 {
143143
he.Code = http.StatusInternalServerError
144144
}
145145
if he.Message == "" {
146146
he.Message = http.StatusText(he.Code)
147-
if e.debug && he.Error != nil {
148-
he.Message = he.Error.Error()
149-
}
147+
}
148+
if e.debug && he.Error != nil {
149+
he.Message = he.Error.Error()
150150
}
151151
http.Error(c.Response, he.Message, he.Code)
152152
})
153-
e.Binder(func(r *http.Request, v interface{}) *HTTPError {
153+
e.SetBinder(func(r *http.Request, v interface{}) *HTTPError {
154154
ct := r.Header.Get(ContentType)
155155
err := UnsupportedMediaType
156156
if strings.HasPrefix(ct, ApplicationJSON) {
@@ -178,33 +178,37 @@ func (e *Echo) Group(pfx string, m ...Middleware) *Echo {
178178
return &g
179179
}
180180

181-
// MaxParam sets the maximum number of path parameters allowed for the application.
181+
// SetMaxParam sets the maximum number of path parameters allowed for the application.
182182
// Default value is 5, good enough for many use cases.
183-
func (e *Echo) MaxParam(n uint8) {
183+
func (e *Echo) SetMaxParam(n uint8) {
184184
e.maxParam = n
185185
}
186186

187-
// HTTPErrorHandler registers an HTTP error handler.
188-
func (e *Echo) HTTPErrorHandler(h HTTPErrorHandler) {
187+
// SetHTTPErrorHandler registers an Echo.HTTPErrorHandler.
188+
func (e *Echo) SetHTTPErrorHandler(h HTTPErrorHandler) {
189189
e.httpErrorHandler = h
190190
}
191191

192-
// Binder registers a custom binder. It's invoked by Context.Bind API.
193-
func (e *Echo) Binder(b BindFunc) {
192+
// SetBinder registers a custom binder. It's invoked by Context.Bind().
193+
func (e *Echo) SetBinder(b BindFunc) {
194194
e.binder = b
195195
}
196196

197-
// Renderer registers an HTML template renderer. It's invoked by Context.Render
198-
// API.
199-
func (e *Echo) Renderer(r Renderer) {
197+
// SetRenderer registers an HTML template renderer. It's invoked by Context.Render().
198+
func (e *Echo) SetRenderer(r Renderer) {
200199
e.renderer = r
201200
}
202201

203-
// Debug runs the application in debug mode.
204-
func (e *Echo) Debug(on bool) {
202+
// SetDebug sets debug mode.
203+
func (e *Echo) SetDebug(on bool) {
205204
e.debug = on
206205
}
207206

207+
// Debug returns debug mode.
208+
func (e *Echo) Debug() bool {
209+
return e.debug
210+
}
211+
208212
// Use adds handler to the middleware chain.
209213
func (e *Echo) Use(m ...Middleware) {
210214
for _, h := range m {
@@ -257,37 +261,20 @@ func (e *Echo) Trace(path string, h Handler) {
257261
e.add(TRACE, path, h)
258262
}
259263

260-
// URI generates a URI from handler.
261-
func (e *Echo) URI(h Handler, params ...interface{}) string {
262-
uri := new(bytes.Buffer)
263-
lp := len(params)
264-
n := 0
264+
func (e *Echo) add(method, path string, h Handler) {
265265
key := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
266-
if path, ok := e.uris[key]; ok {
267-
for i, l := 0, len(path); i < l; i++ {
268-
if path[i] == ':' && n < lp {
269-
for ; i < l && path[i] != '/'; i++ {
270-
}
271-
uri.WriteString(fmt.Sprintf("%v", params[n]))
272-
n++
273-
}
274-
if i < l {
275-
uri.WriteByte(path[i])
276-
}
277-
}
278-
}
279-
return uri.String()
266+
e.uris[key] = path
267+
e.Router.Add(method, e.prefix+path, wrapHandler(h), e)
280268
}
281269

282-
// URL is an alias for URI
283-
func (e *Echo) URL(h Handler, params ...interface{}) string {
284-
return e.URI(h, params...)
270+
// Index serves index file.
271+
func (e *Echo) Index(file string) {
272+
e.ServeFile("/", file)
285273
}
286274

287-
func (e *Echo) add(method, path string, h Handler) {
288-
key := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
289-
e.uris[key] = path
290-
e.Router.Add(method, e.prefix+path, wrapHandler(h), e)
275+
// Favicon serves the default favicon - GET /favicon.ico.
276+
func (e *Echo) Favicon(file string) {
277+
e.ServeFile("/favicon.ico", file)
291278
}
292279

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

310-
// Index serves index file.
311-
func (e *Echo) Index(file string) {
312-
e.ServeFile("/", file)
297+
// URI generates a URI from handler.
298+
func (e *Echo) URI(h Handler, params ...interface{}) string {
299+
uri := new(bytes.Buffer)
300+
lp := len(params)
301+
n := 0
302+
key := runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()
303+
if path, ok := e.uris[key]; ok {
304+
for i, l := 0, len(path); i < l; i++ {
305+
if path[i] == ':' && n < lp {
306+
for ; i < l && path[i] != '/'; i++ {
307+
}
308+
uri.WriteString(fmt.Sprintf("%v", params[n]))
309+
n++
310+
}
311+
if i < l {
312+
uri.WriteByte(path[i])
313+
}
314+
}
315+
}
316+
return uri.String()
313317
}
314318

315-
// Favicon serves the default favicon - GET /favicon.ico.
316-
func (e *Echo) Favicon(file string) {
317-
e.ServeFile("/favicon.ico", file)
319+
// URL is an alias for URI
320+
func (e *Echo) URL(h Handler, params ...interface{}) string {
321+
return e.URI(h, params...)
318322
}
319323

320324
func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {

echo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var u1 = user{
2222
// TODO: Improve me!
2323
func TestEchoMaxParam(t *testing.T) {
2424
e := New()
25-
e.MaxParam(8)
25+
e.SetMaxParam(8)
2626
if e.maxParam != 8 {
2727
t.Errorf("max param should be 8, found %d", e.maxParam)
2828
}

examples/middleware/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func main() {
1717
e := echo.New()
1818

1919
// Debug mode
20-
e.Debug(true)
20+
e.SetDebug(true)
2121

2222
//------------
2323
// Middleware

examples/website/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func main() {
108108
// Cached templates
109109
templates: template.Must(template.ParseFiles("public/views/welcome.html")),
110110
}
111-
e.Renderer(t)
111+
e.SetRenderer(t)
112112
e.Get("/welcome", welcome)
113113

114114
//-------

middleware/recover_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
func TestRecover(t *testing.T) {
1212
e := echo.New()
13-
e.Debug(true)
13+
e.SetDebug(true)
1414
req, _ := http.NewRequest(echo.GET, "/", nil)
1515
w := httptest.NewRecorder()
1616
res := &echo.Response{Writer: w}

website/docs/guide.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,30 @@ Specific version of Echo can be installed using any [package manager](https://gi
2929

3030
### Max path parameters
3131

32-
`echo.MaxParam(n uint8)`
32+
`Echo.SetMaxParam(n uint8)`
3333

3434
Sets the maximum number of path parameters allowed for the application.
3535
Default value is **5**, [good enough](https://github.com/interagent/http-api-design#minimize-path-nesting)
3636
for many use cases. Restricting path parameters allows us to use memory efficiently.
3737

3838
### HTTP error handler
3939

40-
`echo.HTTPErrorHandler(h HTTPErrorHandler)`
40+
`Echo.SetHTTPErrorHandler(h HTTPErrorHandler)`
4141

42-
Registers a custom centralized HTTP error handler `func(*HTTPError, *Context)`.
42+
Registers a custom `Echo.HTTPErrorHandler`.
4343

4444
Default handler sends `HTTPError.Message` HTTP response with `HTTPError.Code` status
4545
code.
4646

47-
- If HTTPError.Code is not specified it uses "500 - Internal Server Error".
48-
- If HTTPError.Message is not specified it uses HTTPError.Error.Error() or the status
49-
code text.
47+
- If HTTPError.Code is not set it uses `500`".
48+
- If HTTPError.Message is not set it uses status code text.
49+
- If debug mode is enabled, HTTPError.Message is set to `HTTPError.Error.Error()`.
50+
51+
### Debug
52+
53+
`echo.SetDebug(on bool)`
54+
55+
Enables debug mode.
5056

5157
## Routing
5258

0 commit comments

Comments
 (0)