From 92819d371ba3909e4f00628282c827fc585c8cfe Mon Sep 17 00:00:00 2001 From: Emile Date: Sat, 2 May 2020 16:08:19 +0200 Subject: logging, correct file serving, comments --- main.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) (limited to 'main.go') 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) + }) +} -- cgit 1.4.1