Last active
October 15, 2018 04:32
-
-
Save ardeshir/135c06732fe6c886d7a0 to your computer and use it in GitHub Desktop.
Simple HTTP Server in Golang
This file contains 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( | |
"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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great for testing other stuff, like firewalls, ssl proxying. Thanks.