@@ -15,13 +15,15 @@ import (
15
15
16
16
"github.com/mattn/go-colorable"
17
17
"golang.org/x/net/websocket"
18
+ "github.com/bradfitz/http2"
18
19
)
19
20
20
21
type (
21
22
Echo struct {
22
23
Router * router
23
24
prefix string
24
25
middleware []MiddlewareFunc
26
+ http2 bool
25
27
maxParam byte
26
28
notFoundHandler HandlerFunc
27
29
httpErrorHandler HTTPErrorHandler
@@ -146,6 +148,7 @@ func New() (e *Echo) {
146
148
// Defaults
147
149
//----------
148
150
151
+ e .HTTP2 (true )
149
152
e .SetMaxParam (5 )
150
153
e .notFoundHandler = func (c * Context ) error {
151
154
return NewHTTPError (http .StatusNotFound )
@@ -187,6 +190,11 @@ func (e *Echo) Group(pfx string, m ...Middleware) *Echo {
187
190
return & g
188
191
}
189
192
193
+ // HTTP2 enables HTTP2 support.
194
+ func (e * Echo ) HTTP2 (on bool ) {
195
+ e .http2 = on
196
+ }
197
+
190
198
// SetMaxParam sets the maximum number of path parameters allowed for the application.
191
199
// Default value is 5, good enough for many use cases.
192
200
func (e * Echo ) SetMaxParam (n uint8 ) {
@@ -371,24 +379,38 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
371
379
372
380
// Run runs a server.
373
381
func (e * Echo ) Run (addr string ) {
374
- log .Fatal (http .ListenAndServe (addr , e ))
382
+ s := & http.Server {Addr : addr }
383
+ e .run (s )
375
384
}
376
385
377
386
// RunTLS runs a server with TLS configuration.
378
387
func (e * Echo ) RunTLS (addr , certFile , keyFile string ) {
379
- log .Fatal (http .ListenAndServeTLS (addr , certFile , keyFile , e ))
388
+ s := & http.Server {Addr : addr }
389
+ e .run (s , certFile , keyFile )
380
390
}
381
391
382
392
// RunServer runs a custom server.
383
- func (e * Echo ) RunServer (server * http.Server ) {
384
- server .Handler = e
385
- log .Fatal (server .ListenAndServe ())
393
+ func (e * Echo ) RunServer (srv * http.Server ) {
394
+ e .run (srv )
386
395
}
387
396
388
397
// RunTLSServer runs a custom server with TLS configuration.
389
- func (e * Echo ) RunTLSServer (server * http.Server , certFile , keyFile string ) {
390
- server .Handler = e
391
- log .Fatal (server .ListenAndServeTLS (certFile , keyFile ))
398
+ func (e * Echo ) RunTLSServer (srv * http.Server , certFile , keyFile string ) {
399
+ e .run (srv , certFile , keyFile )
400
+ }
401
+
402
+ func (e * Echo ) run (s * http.Server , f ... string ) {
403
+ s .Handler = e
404
+ if e .http2 {
405
+ http2 .ConfigureServer (s , nil )
406
+ }
407
+ if len (f ) == 0 {
408
+ log .Fatal (s .ListenAndServe ())
409
+ } else if len (f ) == 2 {
410
+ log .Fatal (s .ListenAndServeTLS (f [0 ], f [1 ]))
411
+ } else {
412
+ log .Fatal ("echo: invalid TLS configuration" )
413
+ }
392
414
}
393
415
394
416
// wraps middleware
0 commit comments