Have service listen and server over TLS

Pass in command line arguments to the path of the certificate and key
files.
master
Buddy Sandidge 8 years ago
parent d1b889eac2
commit 35474b635a

@ -14,7 +14,7 @@ import (
func main() { func main() {
app := cli.NewApp() app := cli.NewApp()
app.Name = "what-is-my-ip" app.Name = "what-is-my-ip"
app.Version = "0.1.0" app.Version = "0.2.0"
app.Usage = "Webapp that gives the IP address for incoming requests" app.Usage = "Webapp that gives the IP address for incoming requests"
app.Action = action app.Action = action
@ -25,6 +25,20 @@ func main() {
EnvVar: "WHAT_IS_MY_IP_ADDRESS", EnvVar: "WHAT_IS_MY_IP_ADDRESS",
Usage: "Address and port to bind to", Usage: "Address and port to bind to",
}, },
cli.StringFlag{
Name: "certificate, c",
Value: "cert.pem",
EnvVar: "WHAT_IS_MY_IP_CERTIFICATE",
Usage: "path to certificate file for https server",
},
cli.StringFlag{
Name: "key, k",
Value: "key.pem",
EnvVar: "WHAT_IS_MY_IP_KEY",
Usage: "path to key file for https server",
},
} }
app.Run(os.Args) app.Run(os.Args)
@ -33,8 +47,10 @@ func main() {
func action(c *cli.Context) error { func action(c *cli.Context) error {
http.HandleFunc("/", getIP) http.HandleFunc("/", getIP)
address := c.String("address") address := c.String("address")
certificate := c.String("certificate")
key := c.String("key")
fmt.Printf("listening on %s\n", address) fmt.Printf("listening on %s\n", address)
return http.ListenAndServe(address, nil) return http.ListenAndServeTLS(address, certificate, key, nil)
} }
func getIP(w http.ResponseWriter, req *http.Request) { func getIP(w http.ResponseWriter, req *http.Request) {

Loading…
Cancel
Save