Skip to content

Instantly share code, notes, and snippets.

@ardeshir
Last active October 15, 2018 04:32
Show Gist options
  • Save ardeshir/135c06732fe6c886d7a0 to your computer and use it in GitHub Desktop.
Save ardeshir/135c06732fe6c886d7a0 to your computer and use it in GitHub Desktop.
Simple HTTP Server in Golang
package main
import(
"os"
"fmt"
"net/http"
"encoding/json"
)
type msg struct {
Message string `json:"msg"` //= {"msg":"Hello Golang Mpls Conf!"}
}
func main() {
port := os.Getenv("PORT") // port string
if port == "" {
port = "8080"
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if err := json.NewEncoder(w).Encode( &msg{ Message: "Hello Golang MPLS Conf"} ); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
})
fmt.Printf("Running server on port: %s\nType Ctr-c to shutdown server.\n", port)
http.ListenAndServe(":"+port, nil)
}
@soniah
Copy link

soniah commented Oct 15, 2018

Great for testing other stuff, like firewalls, ssl proxying. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment