Use CLI package to make address an argument

master
Buddy Sandidge 8 years ago
parent e33df20ee1
commit d1b889eac2

@ -6,12 +6,35 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"os"
"github.com/urfave/cli"
) )
func main() { 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) http.HandleFunc("/", getIP)
fmt.Println("listening on port 3000") address := c.String("address")
http.ListenAndServe(":3000", nil) fmt.Printf("listening on %s\n", address)
return http.ListenAndServe(address, nil)
} }
func getIP(w http.ResponseWriter, req *http.Request) { func getIP(w http.ResponseWriter, req *http.Request) {

Loading…
Cancel
Save