added ability to specify port via env

This commit is contained in:
Stephen McQuay 2015-11-03 14:31:23 -08:00
parent 08cd989b00
commit 346b7ecd32

11
main.go
View File

@ -4,10 +4,10 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
"strconv"
) )
const usage = "servetls <cert file> <key file>" const usage = "servetls <cert file> <key file>"
const port = 8443
func main() { func main() {
if len(os.Args) != 3 { if len(os.Args) != 3 {
@ -18,6 +18,15 @@ func main() {
http.HandleFunc("/", handler) http.HandleFunc("/", handler)
port := 8443
if os.Getenv("PORT") != "" {
p, err := strconv.Atoi(os.Getenv("PORT"))
if err != nil {
fmt.Fprintf(os.Stderr, "could not parse PORT variable: %s\n", os.Getenv("PORT"))
os.Exit(1)
}
port = p
}
addr := fmt.Sprintf(":%d", port) addr := fmt.Sprintf(":%d", port)
fmt.Printf("serving on %s", addr) fmt.Printf("serving on %s", addr)
err := http.ListenAndServeTLS(addr, cert, key, nil) err := http.ListenAndServeTLS(addr, cert, key, nil)