Last active
September 26, 2015 03:28
-
-
Save IndianGuru/885b53be30c2c7873a8f to your computer and use it in GitHub Desktop.
webapphr.go
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" | |
| "log" | |
| "net/http" | |
| "os" | |
| ) | |
| func main() { | |
| http.HandleFunc("/", handler) | |
| fmt.Println("listening...") | |
| err := http.ListenAndServe(GetPort(), nil) | |
| if err != nil { | |
| log.Fatal("ListenAndServe: ", err) | |
| } | |
| } | |
| func handler(w http.ResponseWriter, r *http.Request) { | |
| fmt.Fprintf(w, "Hello. This is our first Go web app on Heroku!") | |
| } | |
| // Get the Port from the environment so we can run on Heroku | |
| func GetPort() string { | |
| var port = os.Getenv("PORT") | |
| // Set a default port if there is nothing in the environment | |
| if port == "" { | |
| port = "4747" | |
| fmt.Println("INFO: No PORT environment variable detected, defaulting to " + port) | |
| } | |
| return ":" + port | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment