From 3a2468dc839f004d72a03dbe0c0f1844451a9441 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois-Xavier=20Aguessy?= Date: Mon, 5 Mar 2018 08:05:48 +0100 Subject: [PATCH] Add flag to specify listening host:port --- README.md | 10 ++++++++-- main.go | 9 ++++++--- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0c6da18..32a6b5d 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,13 @@ If you installed fizzbuzz-lbc using `go get` or with `go install`, you can run i $ $GOPATH/bin/fizzbuzz-lbc -It will launch the fizz-buzz endpoint listenning on `*:8080`. +or you can run it in the source folder by running + + $ go run main.go + +`fizzbuzz-lbc` will listen by default on `*:8080`, but you can specify the host and port by using the `-hostport flag`, for example: + + $ ./fizzbuzz-lbc -hostport 127.0.0.1:8000 Then, you can query the server via http, providing the required parameters: @@ -34,4 +40,4 @@ fizzbuzz-lbc expects 5 parameters: - `string2`: the second string - `int1`: the first integer - `int2`: the second integer - - `limit`: the limit + - `limit`: the limit diff --git a/main.go b/main.go index 8bab1c2..3a1807a 100644 --- a/main.go +++ b/main.go @@ -1,15 +1,18 @@ package main import ( + "flag" "log" "net/http" "dev.fxaguessy.fr/fx/fizzbuzz-lbc/fizzbuzz" ) +var hostPort = flag.String("hostport", ":8080", "server listening on host:port") + func main() { - hostPort := ":8080" + flag.Parse() server := &fizzbuzz.Server{} - log.Printf("Starting fizzbuzz server on %s", hostPort) - log.Fatal(http.ListenAndServe(hostPort, server.Routes())) + log.Printf("Starting fizzbuzz server on %s", *hostPort) + log.Fatal(http.ListenAndServe(*hostPort, server.Routes())) }