From 4c57f549320e9b5ccb6850612b85ad31a7c7e65c Mon Sep 17 00:00:00 2001 From: Vincent Composieux Date: Thu, 9 Mar 2017 21:10:44 +0100 Subject: [PATCH] Added HTTP/2 support --- README.md | 13 ++++++++++++- gofast.go | 22 ++++++++++++++++++++-- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 49388e2..95bc1f4 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ func main() { app.Post("add", "/add/([a-zA-Z]+)$", func(context gofast.Context) { request := context.GetRequest() - pattern := app.GetRoute().GetPattern() + pattern := context.GetRoute().GetPattern() url := request.GetHttpRequest().URL.Path request.AddParameter("name", pattern.FindStringSubmatch(url)[1]) @@ -155,3 +155,14 @@ app.Get("retrieve-data", "/retrieve$", func(context gofast.Context) { fmt.Fprint(response, "{result: 200}") }) ``` + +HTTP/2 Support +-------------- + +You can use HTTP/2 support by using the following line instead of app.Listen(): + +``` +app.ListenHttp2("./fullchain.pem", "./privkey.pem") +``` + +Of course, you will have to precize SSL certificate and private key. diff --git a/gofast.go b/gofast.go index f6e76cc..da7435a 100644 --- a/gofast.go +++ b/gofast.go @@ -11,6 +11,8 @@ import ( "os" "sort" "time" + + "golang.org/x/net/http2" ) const ( @@ -35,8 +37,8 @@ func Bootstrap() *Gofast { return &Gofast{&router, &templating, &middleware} } -// Listens and handles HTTP requests -func (g *Gofast) Listen() { +// Prepares a HTTP server +func (g *Gofast) PrepareHttpServer() string { sort.Sort(RouteLen(g.GetRoutes())) http.Handle("/", g) @@ -52,9 +54,25 @@ func (g *Gofast) Listen() { port = fmt.Sprintf(":%s", p) } + return port +} + +// Listens and handles HTTP requests +func (g *Gofast) Listen() { + port := g.PrepareHttpServer() + http.ListenAndServe(port, nil) } +// Listens and handles HTTP/2 requests +func (g *Gofast) ListenHttp2(certificate string, key string) { + port := g.PrepareHttpServer() + server := &http.Server{Addr: port, Handler: nil} + + http2.ConfigureServer(server, nil) + log.Fatal(server.ListenAndServeTLS(certificate, key)) +} + // Serves HTTP request by matching the correct route func (g *Gofast) ServeHTTP(res http.ResponseWriter, req *http.Request) { matchedRoute := g.GetFallback()