diff --git a/TODO b/TODO index ebdc0cb..60ab545 100644 --- a/TODO +++ b/TODO @@ -12,7 +12,7 @@ [ ] *.. Add robots.txt. [x] *.. Compile static files into the binary. [ ] *** Read route preferences from env/config. -[ ] *.. Make the port configurable via CLI option. +[x] *.. Make the port configurable via CLI option. [ ] *.. Make the stop name link to Breng's page for that stop. [ ] *.. Make the route name link to Breng's page for that route. diff --git a/main.go b/main.go index fa2fd7b..a53f672 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "flag" "fmt" "net/http" "net/url" @@ -37,6 +38,11 @@ var BusClient *http.Client var Quays []Quay = []Quay{} func main() { + var port int + + flag.IntVar(&port, "p", 4444, "The port to start the server on") + flag.Parse() + BusClient = &http.Client{} - serveHttp() + serveHttp(port) } diff --git a/server.go b/server.go index 3157a96..7482f10 100644 --- a/server.go +++ b/server.go @@ -2,9 +2,11 @@ package main import ( "embed" + "fmt" "html/template" "log" "net/http" + "strconv" "time" ) @@ -30,13 +32,13 @@ func formatTime(t time.Time) string { var tplFunctions = template.FuncMap{"formatTime": formatTime} -func serveHttp() { +func serveHttp(port int) { http.HandleFunc("/", handleIndex) http.Handle("/static/", http.FileServer(http.FS(staticDir))) - log.Println("Serving on port 4444") + log.Printf("Listening on port %s\n", strconv.Itoa(port)) - err := http.ListenAndServe(":4444", nil) + err := http.ListenAndServe(fmt.Sprintf(":%s", strconv.Itoa(port)), nil) if err != nil { log.Fatal(err) }