package main import ( "fmt" "log" "math/rand" "net/http" "os" "strconv" "time" "github.com/dustinkirkland/golang-petname" ) func main() { // Seed the random number generator rand.Seed(time.Now().UnixNano()) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // Default to 2 words, can be overridden by query param "words" words := 2 if wParam := r.URL.Query().Get("words"); wParam != "" { if parsed, err := strconv.Atoi(wParam); err == nil && parsed > 0 { words = parsed } } // Default separator is hyphen, can be overridden by query param "separator" separator := "-" if sParam := r.URL.Query().Get("separator"); sParam != "" { separator = sParam } // Generate petname name := petname.Generate(words, separator) w.Header().Set("Content-Type", "text/plain") fmt.Fprint(w, name) }) port := os.Getenv("PORT") if port == "" { port = "8080" } log.Printf("Listening on port %s", port) if err := http.ListenAndServe(":"+port, nil); err != nil { log.Fatal(err) } }