Skip to content

Instantly share code, notes, and snippets.

@IndianGuru
Last active September 26, 2015 03:28
Show Gist options
  • Select an option

  • Save IndianGuru/885b53be30c2c7873a8f to your computer and use it in GitHub Desktop.

Select an option

Save IndianGuru/885b53be30c2c7873a8f to your computer and use it in GitHub Desktop.
webapphr.go
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