From db852c81422a01c9ee09ae549aa5701ccd92165a Mon Sep 17 00:00:00 2001 From: Emile Date: Fri, 30 Aug 2019 00:17:40 +0200 Subject: added a command line flag for specifing on which port the server should listen --- server.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/server.go b/server.go index 5524b0a..626a542 100644 --- a/server.go +++ b/server.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "flag" "fmt" "io/ioutil" "log" @@ -16,16 +17,23 @@ import ( var ( categories []string + port int ) func main() { + initFlags() r := mux.NewRouter() r.HandleFunc("/", indexHandler) r.HandleFunc("/dumpall", dumpallHandler) r.HandleFunc("/{station}/{name}", getHandler) - log.Fatal(http.ListenAndServe(":8080", r)) + log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), r)) +} + +func initFlags() { + flag.IntVar(&port, "p", 8080, "the port the server should listen on") + flag.Parse() } func indexHandler(w http.ResponseWriter, r *http.Request) { -- cgit 1.4.1