@@ -61,7 +61,10 @@ type (
61
61
62
62
MiddlewareFunc func (HandlerFunc ) HandlerFunc
63
63
64
- // Handler interface{}
64
+ Handler interface {
65
+ Handle (Context ) error
66
+ }
67
+
65
68
HandlerFunc func (Context ) error
66
69
67
70
// HTTPErrorHandler is a centralized HTTP error handler.
@@ -181,13 +184,13 @@ var (
181
184
// Error handlers
182
185
//----------------
183
186
184
- notFoundHandler = func (c Context ) error {
187
+ notFoundHandler = HandlerFunc ( func (c Context ) error {
185
188
return NewHTTPError (http .StatusNotFound )
186
- }
189
+ })
187
190
188
- methodNotAllowedHandler = func (c Context ) error {
191
+ methodNotAllowedHandler = HandlerFunc ( func (c Context ) error {
189
192
return NewHTTPError (http .StatusMethodNotAllowed )
190
- }
193
+ })
191
194
)
192
195
193
196
// New creates an instance of Echo.
@@ -204,22 +207,7 @@ func New() (e *Echo) {
204
207
//----------
205
208
206
209
e .HTTP2 (true )
207
- e .defaultHTTPErrorHandler = func (err error , c Context ) {
208
- code := http .StatusInternalServerError
209
- msg := http .StatusText (code )
210
- if he , ok := err .(* HTTPError ); ok {
211
- code = he .code
212
- msg = he .message
213
- }
214
- if e .debug {
215
- msg = err .Error ()
216
- }
217
- if ! c .Response ().Committed () {
218
- c .String (code , msg )
219
- }
220
- e .logger .Error (err )
221
- }
222
- e .SetHTTPErrorHandler (e .defaultHTTPErrorHandler )
210
+ e .SetHTTPErrorHandler (e .DefaultHTTPErrorHandler )
223
211
e .SetBinder (& binder {})
224
212
225
213
// Logger
@@ -232,6 +220,10 @@ func (f MiddlewareFunc) Process(h HandlerFunc) HandlerFunc {
232
220
return f (h )
233
221
}
234
222
223
+ func (f HandlerFunc ) Handle (c Context ) error {
224
+ return f (c )
225
+ }
226
+
235
227
// Router returns router.
236
228
func (e * Echo ) Router () * Router {
237
229
return e .router
@@ -254,7 +246,19 @@ func (e *Echo) HTTP2(on bool) {
254
246
255
247
// DefaultHTTPErrorHandler invokes the default HTTP error handler.
256
248
func (e * Echo ) DefaultHTTPErrorHandler (err error , c Context ) {
257
- e .defaultHTTPErrorHandler (err , c )
249
+ code := http .StatusInternalServerError
250
+ msg := http .StatusText (code )
251
+ if he , ok := err .(* HTTPError ); ok {
252
+ code = he .code
253
+ msg = he .message
254
+ }
255
+ if e .debug {
256
+ msg = err .Error ()
257
+ }
258
+ if ! c .Response ().Committed () {
259
+ c .String (code , msg )
260
+ }
261
+ e .logger .Error (err )
258
262
}
259
263
260
264
// SetHTTPErrorHandler registers a custom Echo.HTTPErrorHandler.
@@ -295,75 +299,75 @@ func (e *Echo) Hook(h engine.HandlerFunc) {
295
299
}
296
300
297
301
// Use adds handler to the middleware chain.
298
- func (e * Echo ) Use (m ... MiddlewareFunc ) {
299
- for _ , h := range m {
300
- e .middleware = append (e .middleware , h )
302
+ func (e * Echo ) Use (middleware ... interface {} ) {
303
+ for _ , m := range middleware {
304
+ e .middleware = append (e .middleware , wrapMiddleware ( m ) )
301
305
}
302
306
}
303
307
304
308
// Connect adds a CONNECT route > handler to the router.
305
- func (e * Echo ) Connect (path string , h HandlerFunc ) {
306
- e .add (CONNECT , path , h )
309
+ func (e * Echo ) Connect (path string , handler interface {} ) {
310
+ e .add (CONNECT , path , handler )
307
311
}
308
312
309
313
// Delete adds a DELETE route > handler to the router.
310
- func (e * Echo ) Delete (path string , h HandlerFunc ) {
311
- e .add (DELETE , path , h )
314
+ func (e * Echo ) Delete (path string , handler interface {} ) {
315
+ e .add (DELETE , path , handler )
312
316
}
313
317
314
318
// Get adds a GET route > handler to the router.
315
- func (e * Echo ) Get (path string , h HandlerFunc ) {
316
- e .add (GET , path , h )
319
+ func (e * Echo ) Get (path string , handler interface {} ) {
320
+ e .add (GET , path , handler )
317
321
}
318
322
319
323
// Head adds a HEAD route > handler to the router.
320
- func (e * Echo ) Head (path string , h HandlerFunc ) {
321
- e .add (HEAD , path , h )
324
+ func (e * Echo ) Head (path string , handler interface {} ) {
325
+ e .add (HEAD , path , handler )
322
326
}
323
327
324
328
// Options adds an OPTIONS route > handler to the router.
325
- func (e * Echo ) Options (path string , h HandlerFunc ) {
326
- e .add (OPTIONS , path , h )
329
+ func (e * Echo ) Options (path string , handler interface {} ) {
330
+ e .add (OPTIONS , path , handler )
327
331
}
328
332
329
333
// Patch adds a PATCH route > handler to the router.
330
- func (e * Echo ) Patch (path string , h HandlerFunc ) {
331
- e .add (PATCH , path , h )
334
+ func (e * Echo ) Patch (path string , handler interface {} ) {
335
+ e .add (PATCH , path , handler )
332
336
}
333
337
334
338
// Post adds a POST route > handler to the router.
335
- func (e * Echo ) Post (path string , h HandlerFunc ) {
336
- e .add (POST , path , h )
339
+ func (e * Echo ) Post (path string , handler interface {} ) {
340
+ e .add (POST , path , handler )
337
341
}
338
342
339
343
// Put adds a PUT route > handler to the router.
340
- func (e * Echo ) Put (path string , h HandlerFunc ) {
341
- e .add (PUT , path , h )
344
+ func (e * Echo ) Put (path string , handler interface {} ) {
345
+ e .add (PUT , path , handler )
342
346
}
343
347
344
348
// Trace adds a TRACE route > handler to the router.
345
- func (e * Echo ) Trace (path string , h HandlerFunc ) {
346
- e .add (TRACE , path , h )
349
+ func (e * Echo ) Trace (path string , handler interface {} ) {
350
+ e .add (TRACE , path , handler )
347
351
}
348
352
349
353
// Any adds a route > handler to the router for all HTTP methods.
350
- func (e * Echo ) Any (path string , h HandlerFunc ) {
354
+ func (e * Echo ) Any (path string , handler interface {} ) {
351
355
for _ , m := range methods {
352
- e .add (m , path , h )
356
+ e .add (m , path , handler )
353
357
}
354
358
}
355
359
356
360
// Match adds a route > handler to the router for multiple HTTP methods provided.
357
- func (e * Echo ) Match (methods []string , path string , h HandlerFunc ) {
361
+ func (e * Echo ) Match (methods []string , path string , handler interface {} ) {
358
362
for _ , m := range methods {
359
- e .add (m , path , h )
363
+ e .add (m , path , handler )
360
364
}
361
365
}
362
366
363
367
// NOTE: v2
364
- func (e * Echo ) add (method , path string , h HandlerFunc ) {
368
+ func (e * Echo ) add (method , path string , h interface {} ) {
365
369
path = e .prefix + path
366
- e .router .Add (method , path , h , e )
370
+ e .router .Add (method , path , wrapHandler ( h ) , e )
367
371
r := Route {
368
372
Method : method ,
369
373
Path : path ,
@@ -511,8 +515,7 @@ func (e *Echo) Routes() []Route {
511
515
return e .router .routes
512
516
}
513
517
514
- // ServeHTTP serves HTTP requests.
515
- func (e * Echo ) ServeHTTP (req engine.Request , res engine.Response ) {
518
+ func (e * Echo ) handle (req engine.Request , res engine.Response ) {
516
519
if e .hook != nil {
517
520
e .hook (req , res )
518
521
}
@@ -566,33 +569,11 @@ func (e *Echo) RunTLS(addr, certfile, keyfile string) {
566
569
567
570
// RunConfig runs a server with engine configuration.
568
571
func (e * Echo ) RunConfig (config * engine.Config ) {
569
- handler := func (req engine.Request , res engine.Response ) {
570
- if e .hook != nil {
571
- e .hook (req , res )
572
- }
573
-
574
- c := e .pool .Get ().(* context )
575
- h , e := e .router .Find (req .Method (), req .URL ().Path (), c )
576
- c .reset (req , res , e )
577
-
578
- // Chain middleware with handler in the end
579
- for i := len (e .middleware ) - 1 ; i >= 0 ; i -- {
580
- h = e.middleware [i ](h )
581
- }
582
-
583
- // Execute chain
584
- if err := h (c ); err != nil {
585
- e .httpErrorHandler (err , c )
586
- }
587
-
588
- e .pool .Put (c )
589
- }
590
-
591
572
switch e .engineType {
592
573
case engine .FastHTTP :
593
- e .engine = fasthttp .NewServer (config , handler , e .logger )
574
+ e .engine = fasthttp .NewServer (config , e . handle , e .logger )
594
575
default :
595
- e .engine = standard .NewServer (config , handler , e .logger )
576
+ e .engine = standard .NewServer (config , e . handle , e .logger )
596
577
}
597
578
e .engine .Start ()
598
579
}
@@ -635,3 +616,29 @@ func (binder) Bind(r engine.Request, i interface{}) (err error) {
635
616
}
636
617
return
637
618
}
619
+
620
+ func wrapMiddleware (m interface {}) MiddlewareFunc {
621
+ switch m := m .(type ) {
622
+ case Middleware :
623
+ return m .Process
624
+ case MiddlewareFunc :
625
+ return m
626
+ case func (HandlerFunc ) HandlerFunc :
627
+ return m
628
+ default :
629
+ panic ("invalid middleware" )
630
+ }
631
+ }
632
+
633
+ func wrapHandler (h interface {}) HandlerFunc {
634
+ switch h := h .(type ) {
635
+ case Handler :
636
+ return h .Handle
637
+ case HandlerFunc :
638
+ return h
639
+ case func (Context ) error :
640
+ return h
641
+ default :
642
+ panic ("invalid handler" )
643
+ }
644
+ }
0 commit comments