From a926bdc955ade173db95d64ce47ddd2024c48722 Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Fri, 12 Feb 2016 21:38:47 -0800 Subject: [PATCH] allow hosting on alternate port. --- cmd/smweb/main.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/cmd/smweb/main.go b/cmd/smweb/main.go index 22a6af3..e97095b 100644 --- a/cmd/smweb/main.go +++ b/cmd/smweb/main.go @@ -1,8 +1,10 @@ package main import ( + "fmt" "net/http" "os" + "strconv" "github.com/elazarl/go-bindata-assetfs" "mcquay.me/web" @@ -20,13 +22,23 @@ func main() { } else { fs = http.Dir(os.Getenv("SM_STATIC")) } + port := 8000 + if os.Getenv("PORT") != "" { + p, err := strconv.Atoi(os.Getenv("PORT")) + if err != nil { + fmt.Fprintf(os.Stderr, "couldn't parse port: %v\n", err) + os.Exit(1) + } + port = p + } // XXX: beware: I've copy/pasted this twice now and been confused because // I ought to have been using my own servemux http.Handle( "/", http.FileServer(fs), ) - if err := http.ListenAndServe(":8000", nil); err != nil { - panic(err) + if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil { + fmt.Fprintf(os.Stderr, "problem serving: %v\n", err) + os.Exit(1) } }