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/170b0668681e187f5263629a3b57385d to your computer and use it in GitHub Desktop.
Save ldclakmal/170b0668681e187f5263629a3b57385d to your computer and use it in GitHub Desktop.
Go HTTP Server
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