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 10, 2017
1 parent cef5909 commit 8c144e5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
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.
21 changes: 19 additions & 2 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 @@ -35,8 +36,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)

Expand All @@ -52,9 +53,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, &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 8c144e5

Please sign in to comment.