Skip to content

Instantly share code, notes, and snippets.

@ldclakmal
Last active March 5, 2019 11:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ldclakmal/864493b6d4335c9297719dbf323a7b4e to your computer and use it in GitHub Desktop.
Save ldclakmal/864493b6d4335c9297719dbf323a7b4e to your computer and use it in GitHub Desktop.
Go HTTP/2 Server
package main
import (
"fmt"
"golang.org/x/net/http2"
"io/ioutil"
"log"
"net/http"
)
func main() {
var httpServer = http.Server{
Addr: ":9191",
}
var http2Server = http2.Server{}
_ = http2.ConfigureServer(&httpServer, &http2Server)
http.HandleFunc("/hello/sayHello", echoPayload)
log.Printf("Go Backend: { HTTPVersion = 2 }; serving on https://localhost:9191/hello/sayHello")
log.Fatal(httpServer.ListenAndServeTLS("./cert/server.crt", "./cert/server.key"))
}
func echoPayload(w http.ResponseWriter, req *http.Request) {
log.Printf("Request connection: %s, path: %s", req.Proto, req.URL.Path[1:])
defer req.Body.Close()
contents, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Fatalf("Oops! Failed reading body of the request.\n %s", err)
http.Error(w, err.Error(), 500)
}
fmt.Fprintf(w, "%s\n", string(contents))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment