diff --git a/main.go b/main.go index f2ec209..fec1900 100644 --- a/main.go +++ b/main.go @@ -1,18 +1,19 @@ package main import ( - "net/http" "flag" "fmt" + "log" + "net/http" "path/filepath" "git.milar.in/milarin/adverr" ) var ( - root = flag.String("r", ".", "root directory") + root = flag.String("r", ".", "root directory") iface = flag.String("i", "localhost", "network interface") - port = flag.Uint64("p", 8080, "port") + port = flag.Uint64("p", 8080, "port") ) func main() { @@ -22,11 +23,21 @@ func main() { if err != nil { adverr.Fatalln(err, 1) } + *root = abspath + + fmt.Printf("Root Directory: %s\nURL: http://%s:%d/\n", *root, *iface, *port) - fmt.Printf("Root Directory: %s\nURL: http://%s:%d/\n", abspath, *iface, *port) connStr := fmt.Sprintf("%s:%d", *iface, *port) - handler := http.FileServer(http.Dir(*root)) + handler := LogHandler(http.FileServer(http.Dir(*root))) + if err := http.ListenAndServe(connStr, handler); err != nil { adverr.Fatalln(err, 1) } } + +func LogHandler(handler http.Handler) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + log.Printf("%s %s", r.Method, r.URL) + handler.ServeHTTP(w, r) + } +}