Last active
March 5, 2019 11:58
-
-
Save ldclakmal/864493b6d4335c9297719dbf323a7b4e to your computer and use it in GitHub Desktop.
Go HTTP/2 Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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