1
0
mirror of https://github.com/labstack/echo.git synced 2024-12-24 20:14:31 +02:00

Initiated HTTP2 support

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-05-21 23:11:27 -07:00
parent 6f728d428d
commit df924ffc11

38
echo.go
View File

@ -15,6 +15,7 @@ import (
"github.com/mattn/go-colorable"
"golang.org/x/net/websocket"
"github.com/bradfitz/http2"
)
type (
@ -22,6 +23,7 @@ type (
Router *router
prefix string
middleware []MiddlewareFunc
http2 bool
maxParam byte
notFoundHandler HandlerFunc
httpErrorHandler HTTPErrorHandler
@ -146,6 +148,7 @@ func New() (e *Echo) {
// Defaults
//----------
e.HTTP2(true)
e.SetMaxParam(5)
e.notFoundHandler = func(c *Context) error {
return NewHTTPError(http.StatusNotFound)
@ -187,6 +190,11 @@ func (e *Echo) Group(pfx string, m ...Middleware) *Echo {
return &g
}
// HTTP2 enables HTTP2 support.
func (e *Echo) HTTP2(on bool) {
e.http2 = on
}
// SetMaxParam sets the maximum number of path parameters allowed for the application.
// Default value is 5, good enough for many use cases.
func (e *Echo) SetMaxParam(n uint8) {
@ -371,24 +379,38 @@ 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, nil)
}
if len(f) == 0 {
log.Fatal(s.ListenAndServe())
} else if len(f) == 2 {
log.Fatal(s.ListenAndServeTLS(f[0], f[1]))
} else {
log.Fatal("echo: invalid TLS configuration")
}
}
// wraps middleware