From 2fc0f71e6ecd72ab7a981f1bc5a2a8ce2ca53519 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, 34 insertions(+), 1 deletion(-) 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..0152d4a 100644 --- a/gofast.go +++ b/gofast.go @@ -6,6 +6,7 @@ package gofast import ( "fmt" + "golang.org/x/net/http2" "log" "net/http" "os" @@ -55,6 +56,27 @@ func (g *Gofast) Listen() { http.ListenAndServe(port, nil) } +func (g *Gofast) ListenHttp2(certificate string, key string) { + sort.Sort(RouteLen(g.GetRoutes())) + + assetsDirectory := g.GetAssetsDirectory() + assetsUrl := fmt.Sprintf("/%s/", assetsDirectory) + assetsPrefix := fmt.Sprintf("/%s", assetsDirectory) + + http.Handle(assetsUrl, http.StripPrefix(assetsPrefix, http.FileServer(http.Dir(assetsDirectory)))) + + port := PORT + + if p := os.Getenv("PORT"); p != "" { + port = fmt.Sprintf(":%s", p) + } + + server := &http.Server{Addr: port, Handler: g} + + http2.ConfigureServer(server, &http2.Server{}) + 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()