From 6f7883f96cd80da47f38c00b3470e0e00adae4a5 Mon Sep 17 00:00:00 2001 From: siddharth Date: Sun, 22 May 2022 18:19:50 -0400 Subject: main: allow listening on custom port --- main.go | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 2f4448e..e6e455b 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ package main import ( "embed" + "flag" "fmt" "html/template" "log" @@ -14,6 +15,12 @@ import ( "ricketyspace.net/peach/nws" ) +// Peach port. Defaults to 8151 +var peachPort = flag.Int("p", 8151, "Port to run peach on") + +// Peach listen address. Set during init. +var peachAddr = "" + // Holds static content. //go:embed html static var peachFS embed.FS @@ -47,6 +54,14 @@ type WeatherTimeline struct { Periods []WeatherPeriod } +func init() { + flag.Parse() + if *peachPort < 80 { + log.Fatalf("port number is invalid: %v", *peachPort) + } + peachAddr = fmt.Sprintf(":%d", *peachPort) +} + func main() { http.Handle("/static/", http.FileServer(http.FS(peachFS))) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { @@ -56,7 +71,7 @@ func main() { } showWeather(w, 41.115, -83.177) }) - log.Fatal(http.ListenAndServe(":8151", nil)) + log.Fatal(http.ListenAndServe(peachAddr, nil)) } func showWeather(w http.ResponseWriter, lat, lng float32) { -- cgit v1.2.3