diff options
author | siddharth <s@ricketyspace.net> | 2022-05-22 18:19:50 -0400 |
---|---|---|
committer | siddharth <s@ricketyspace.net> | 2022-05-22 18:19:50 -0400 |
commit | 6f7883f96cd80da47f38c00b3470e0e00adae4a5 (patch) | |
tree | 8507182c48b5f2ede6b0c5f920ee59bb66f1ea7a | |
parent | 12a49ac3a4236fe61ebe3651625c273f48fed81b (diff) |
main: allow listening on custom port
-rw-r--r-- | main.go | 17 |
1 files changed, 16 insertions, 1 deletions
@@ -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) { |