Make the port configurable via CLI flag

main
Jasper Bok 2023-08-18 17:18:07 +02:00
parent f52890f58a
commit b294be3335
3 changed files with 13 additions and 5 deletions

2
TODO
View File

@ -12,7 +12,7 @@
[ ] *.. Add robots.txt. [ ] *.. Add robots.txt.
[x] *.. Compile static files into the binary. [x] *.. Compile static files into the binary.
[ ] *** Read route preferences from env/config. [ ] *** 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 stop name link to Breng's page for that stop.
[ ] *.. Make the route name link to Breng's page for that route. [ ] *.. Make the route name link to Breng's page for that route.

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"net/http" "net/http"
"net/url" "net/url"
@ -37,6 +38,11 @@ var BusClient *http.Client
var Quays []Quay = []Quay{} var Quays []Quay = []Quay{}
func main() { func main() {
var port int
flag.IntVar(&port, "p", 4444, "The port to start the server on")
flag.Parse()
BusClient = &http.Client{} BusClient = &http.Client{}
serveHttp() serveHttp(port)
} }

View File

@ -2,9 +2,11 @@ package main
import ( import (
"embed" "embed"
"fmt"
"html/template" "html/template"
"log" "log"
"net/http" "net/http"
"strconv"
"time" "time"
) )
@ -30,13 +32,13 @@ func formatTime(t time.Time) string {
var tplFunctions = template.FuncMap{"formatTime": formatTime} var tplFunctions = template.FuncMap{"formatTime": formatTime}
func serveHttp() { func serveHttp(port int) {
http.HandleFunc("/", handleIndex) http.HandleFunc("/", handleIndex)
http.Handle("/static/", http.FileServer(http.FS(staticDir))) 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }