Last active
March 5, 2019 11:58
-
-
Save ldclakmal/170b0668681e187f5263629a3b57385d to your computer and use it in GitHub Desktop.
Go HTTP 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" | |
"io/ioutil" | |
"log" | |
"net/http" | |
) | |
func main() { | |
http.HandleFunc("/hello/sayHello", echoPayload) | |
log.Printf("Go Backend: { HTTPVersion = 1 }; serving on https://localhost:9191/hello/sayHello") | |
log.Fatal(http.ListenAndServeTLS(":9191", "./cert/server.crt", "./cert/server.key", nil)) | |
} | |
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