Skip to content

Commit

Permalink
Added HTTP/2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
eko committed Mar 9, 2017
1 parent cef5909 commit 2fc0f71
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
13 changes: 12 additions & 1 deletion README.md
Expand Up @@ -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])
Expand Down Expand Up @@ -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.
22 changes: 22 additions & 0 deletions gofast.go
Expand Up @@ -6,6 +6,7 @@ package gofast

import (
"fmt"
"golang.org/x/net/http2"
"log"
"net/http"
"os"
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 2fc0f71

Please sign in to comment.