logging, correct file serving, comments
HEAD master1 files changed, 19 insertions, 2 deletions
diff --git a/main.go b/main.go
index de5d97f..e53c959 100644
--- a/main.go
+++ b/main.go
@@ -17,12 +17,29 @@ func main() {
dir := flag.String("dir", "./", "path to host")
flag.Parse()
- // define the http server
+ // define the router
r := mux.NewRouter()
- r.Handle("/", http.FileServer(http.Dir(*dir)))
+ r.Use(loggingMiddleware)
+
+ // define the subrouter hosting the previously defined directory
+ files := r.PathPrefix("/").Subrouter()
+ fs := http.FileServer(http.Dir(*dir))
+ files.PathPrefix("/").Handler(http.StripPrefix("/", fs))
+
+ // define the http server using the values defined using flags
httpServer := http.Server{
Addr: fmt.Sprintf("%s:%s", *ip, *port),
Handler: r,
}
+
+ // start the http server
+ log.Printf("http server listening on %s:%s hosting %s", *ip, *port, *dir)
log.Fatal(httpServer.ListenAndServe())
}
+
+func loggingMiddleware(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ log.Println(r.RequestURI)
+ next.ServeHTTP(w, r)
+ })
+}
|