Initial revision

This commit is contained in:
Jason Swank
2026-05-11 12:30:42 -04:00
commit c92a46c49d
5 changed files with 188 additions and 0 deletions

50
main.go Normal file
View File

@@ -0,0 +1,50 @@
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)
}
}