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
1 changed files with 10 additions and 1 deletions

11
main.go
View File

@ -4,10 +4,10 @@ import (
"fmt"
"net/http"
"os"
"strconv"
)
const usage = "servetls <cert file> <key file>"
const port = 8443
func main() {
if len(os.Args) != 3 {
@ -18,6 +18,15 @@ func main() {
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)
fmt.Printf("serving on %s", addr)
err := http.ListenAndServeTLS(addr, cert, key, nil)