allow hosting on alternate port.

This commit is contained in:
Stephen McQuay 2016-02-12 21:38:47 -08:00
parent d76a9bff9f
commit a926bdc955
1 changed files with 14 additions and 2 deletions

View File

@ -1,8 +1,10 @@
package main package main
import ( import (
"fmt"
"net/http" "net/http"
"os" "os"
"strconv"
"github.com/elazarl/go-bindata-assetfs" "github.com/elazarl/go-bindata-assetfs"
"mcquay.me/web" "mcquay.me/web"
@ -20,13 +22,23 @@ func main() {
} else { } else {
fs = http.Dir(os.Getenv("SM_STATIC")) 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 // XXX: beware: I've copy/pasted this twice now and been confused because
// I ought to have been using my own servemux // I ought to have been using my own servemux
http.Handle( http.Handle(
"/", "/",
http.FileServer(fs), http.FileServer(fs),
) )
if err := http.ListenAndServe(":8000", nil); err != nil { if err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil); err != nil {
panic(err) fmt.Fprintf(os.Stderr, "problem serving: %v\n", err)
os.Exit(1)
} }
} }