Skip to content

Commit df924ff

Browse files
committed
Initiated HTTP2 support
Signed-off-by: Vishal Rana <[email protected]>
1 parent 6f728d4 commit df924ff

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

echo.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ import (
1515

1616
"github.com/mattn/go-colorable"
1717
"golang.org/x/net/websocket"
18+
"github.com/bradfitz/http2"
1819
)
1920

2021
type (
2122
Echo struct {
2223
Router *router
2324
prefix string
2425
middleware []MiddlewareFunc
26+
http2 bool
2527
maxParam byte
2628
notFoundHandler HandlerFunc
2729
httpErrorHandler HTTPErrorHandler
@@ -146,6 +148,7 @@ func New() (e *Echo) {
146148
// Defaults
147149
//----------
148150

151+
e.HTTP2(true)
149152
e.SetMaxParam(5)
150153
e.notFoundHandler = func(c *Context) error {
151154
return NewHTTPError(http.StatusNotFound)
@@ -187,6 +190,11 @@ func (e *Echo) Group(pfx string, m ...Middleware) *Echo {
187190
return &g
188191
}
189192

193+
// HTTP2 enables HTTP2 support.
194+
func (e *Echo) HTTP2(on bool) {
195+
e.http2 = on
196+
}
197+
190198
// SetMaxParam sets the maximum number of path parameters allowed for the application.
191199
// Default value is 5, good enough for many use cases.
192200
func (e *Echo) SetMaxParam(n uint8) {
@@ -371,24 +379,38 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
371379

372380
// Run runs a server.
373381
func (e *Echo) Run(addr string) {
374-
log.Fatal(http.ListenAndServe(addr, e))
382+
s := &http.Server{Addr: addr}
383+
e.run(s)
375384
}
376385

377386
// RunTLS runs a server with TLS configuration.
378387
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)
380390
}
381391

382392
// 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)
386395
}
387396

388397
// 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+
}
392414
}
393415

394416
// wraps middleware

0 commit comments

Comments
 (0)