From 46d1ce6419dd6f05c55cf5f7dc554b4b3534ae9d Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Mon, 4 May 2015 21:16:15 -0700 Subject: [PATCH 1/3] Added HTTP2 support Signed-off-by: Vishal Rana --- echo.go | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/echo.go b/echo.go index 1c671d61c..5d50bbc7a 100644 --- a/echo.go +++ b/echo.go @@ -13,6 +13,7 @@ import ( "strings" "sync" + "github.com/bradfitz/http2" "github.com/labstack/gommon/color" "github.com/mattn/go-colorable" ) @@ -22,6 +23,7 @@ type ( Router *router prefix string middleware []MiddlewareFunc + http2 bool maxParam byte notFoundHandler HandlerFunc httpErrorHandler HTTPErrorHandler @@ -156,6 +158,10 @@ func (e *Echo) Group(pfx string, m ...Middleware) *Echo { return &g } +func (e *Echo) HTTP2(h2 bool) { + e.http2 = h2 +} + // MaxParam sets the maximum number of path parameters allowd for the application. // Default value is 5, good enough for many use cases. func (e *Echo) MaxParam(n uint8) { @@ -317,24 +323,36 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Run runs a server. func (e *Echo) Run(addr string) { - log.Fatal(http.ListenAndServe(addr, e)) + s := &http.Server{Addr: addr} + e.run(s) } // RunTLS runs a server with TLS configuration. func (e *Echo) RunTLS(addr, certFile, keyFile string) { - log.Fatal(http.ListenAndServeTLS(addr, certFile, keyFile, e)) + s := &http.Server{Addr: addr} + e.run(s, certFile, keyFile) } // RunServer runs a custom server. -func (e *Echo) RunServer(server *http.Server) { - server.Handler = e - log.Fatal(server.ListenAndServe()) +func (e *Echo) RunServer(srv *http.Server) { + e.run(srv) } // RunTLSServer runs a custom server with TLS configuration. -func (e *Echo) RunTLSServer(server *http.Server, certFile, keyFile string) { - server.Handler = e - log.Fatal(server.ListenAndServeTLS(certFile, keyFile)) +func (e *Echo) RunTLSServer(srv *http.Server, certFile, keyFile string) { + e.run(srv, certFile, keyFile) +} + +func (e *Echo) run(s *http.Server, f ...string) { + s.Handler = e + if e.http2 { + http2.ConfigureServer(s, &http2.Server{}) + } + if len(f) == 0 { + log.Fatal(s.ListenAndServe()) + } else if len(f) == 2 { + log.Fatal(s.ListenAndServeTLS(f[0], f[1])) + } } // wraps Middleware From 20eab3f39c3b3cf0aae545abc2755e503e423e5c Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Mon, 4 May 2015 21:16:15 -0700 Subject: [PATCH 2/3] Added HTTP2 support Signed-off-by: Vishal Rana --- echo.go | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/echo.go b/echo.go index ddab9ac0b..2e9faf0ed 100644 --- a/echo.go +++ b/echo.go @@ -13,6 +13,7 @@ import ( "strings" "sync" + "github.com/bradfitz/http2" "github.com/mattn/go-colorable" ) @@ -21,6 +22,7 @@ type ( Router *router prefix string middleware []MiddlewareFunc + http2 bool maxParam byte notFoundHandler HandlerFunc httpErrorHandler HTTPErrorHandler @@ -166,7 +168,11 @@ func (e *Echo) Group(pfx string, m ...Middleware) *Echo { return &g } -// MaxParam sets the maximum number of path parameters allowed for the application. +func (e *Echo) HTTP2(h2 bool) { + e.http2 = h2 +} + +// MaxParam sets the maximum number of path parameters allowd for the application. // Default value is 5, good enough for many use cases. func (e *Echo) MaxParam(n uint8) { e.maxParam = n @@ -327,24 +333,36 @@ func (e *Echo) ServeHTTP(w http.ResponseWriter, r *http.Request) { // Run runs a server. func (e *Echo) Run(addr string) { - log.Fatal(http.ListenAndServe(addr, e)) + s := &http.Server{Addr: addr} + e.run(s) } // RunTLS runs a server with TLS configuration. func (e *Echo) RunTLS(addr, certFile, keyFile string) { - log.Fatal(http.ListenAndServeTLS(addr, certFile, keyFile, e)) + s := &http.Server{Addr: addr} + e.run(s, certFile, keyFile) } // RunServer runs a custom server. -func (e *Echo) RunServer(server *http.Server) { - server.Handler = e - log.Fatal(server.ListenAndServe()) +func (e *Echo) RunServer(srv *http.Server) { + e.run(srv) } // RunTLSServer runs a custom server with TLS configuration. -func (e *Echo) RunTLSServer(server *http.Server, certFile, keyFile string) { - server.Handler = e - log.Fatal(server.ListenAndServeTLS(certFile, keyFile)) +func (e *Echo) RunTLSServer(srv *http.Server, certFile, keyFile string) { + e.run(srv, certFile, keyFile) +} + +func (e *Echo) run(s *http.Server, f ...string) { + s.Handler = e + if e.http2 { + http2.ConfigureServer(s, &http2.Server{}) + } + if len(f) == 0 { + log.Fatal(s.ListenAndServe()) + } else if len(f) == 2 { + log.Fatal(s.ListenAndServeTLS(f[0], f[1])) + } } // wraps Middleware From d449d0e68ff5952cb8a1b25dce47afc5e427611d Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Tue, 5 May 2015 08:55:41 -0700 Subject: [PATCH 3/3] Added invalid TLS configuration error Signed-off-by: Vishal Rana --- echo.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/echo.go b/echo.go index 2e9faf0ed..001c37e8d 100644 --- a/echo.go +++ b/echo.go @@ -362,6 +362,8 @@ func (e *Echo) run(s *http.Server, f ...string) { log.Fatal(s.ListenAndServe()) } else if len(f) == 2 { log.Fatal(s.ListenAndServeTLS(f[0], f[1])) + } else { + log.Fatal("echo: invalid TLS configuration") } }