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..9a688d9 100644 --- a/gofast.go +++ b/gofast.go @@ -6,6 +6,7 @@ package gofast import ( "fmt" + "golang.org/x/net/http2" "log" "net/http" "os" @@ -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) @@ -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()