From d1b889eac2abc7b4649993690ee2b1a0c3ca1ebc Mon Sep 17 00:00:00 2001 From: Buddy Sandidge Date: Fri, 11 Nov 2016 20:00:33 -0800 Subject: [PATCH] Use CLI package to make address an argument --- what-is-my-ip.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/what-is-my-ip.go b/what-is-my-ip.go index b01a788..70f71a8 100644 --- a/what-is-my-ip.go +++ b/what-is-my-ip.go @@ -6,12 +6,35 @@ import ( "log" "net" "net/http" + "os" + + "github.com/urfave/cli" ) func main() { + app := cli.NewApp() + app.Name = "what-is-my-ip" + app.Version = "0.1.0" + app.Usage = "Webapp that gives the IP address for incoming requests" + app.Action = action + + app.Flags = []cli.Flag{ + cli.StringFlag{ + Name: "address, a", + Value: ":3000", + EnvVar: "WHAT_IS_MY_IP_ADDRESS", + Usage: "Address and port to bind to", + }, + } + + app.Run(os.Args) +} + +func action(c *cli.Context) error { http.HandleFunc("/", getIP) - fmt.Println("listening on port 3000") - http.ListenAndServe(":3000", nil) + address := c.String("address") + fmt.Printf("listening on %s\n", address) + return http.ListenAndServe(address, nil) } func getIP(w http.ResponseWriter, req *http.Request) {