Let user specify port

This commit is contained in:
Stephen McQuay 2013-10-21 19:21:59 -07:00
parent 10110d8ae1
commit 59b496b66e

View File

@ -1,10 +1,14 @@
package main package main
import ( import (
"flag"
"fmt"
"log" "log"
"net/http" "net/http"
) )
var port = flag.Int("port", 8000, "port from which to serve")
func logger(h http.Handler) http.Handler { func logger(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Printf("%s: %s\n", r.RemoteAddr, r.URL) log.Printf("%s: %s\n", r.RemoteAddr, r.URL)
@ -13,6 +17,11 @@ func logger(h http.Handler) http.Handler {
} }
func main() { func main() {
flag.Parse()
addr := fmt.Sprintf(":%d", *port)
fh := http.FileServer(http.Dir("./")) fh := http.FileServer(http.Dir("./"))
http.ListenAndServe(":8080", logger(fh)) http.ListenAndServe(":8080", logger(fh))
if err := http.ListenAndServe(addr, logger(fh)); err != nil {
log.Fatal(err)
}
} }