about summary refs log tree commit diff
path: root/main.go
diff options
context:
space:
mode:
authorEmile <hanemile@protonmail.com>2020-05-02 16:08:19 +0200
committerEmile <hanemile@protonmail.com>2020-05-02 16:08:19 +0200
commit92819d371ba3909e4f00628282c827fc585c8cfe (patch)
tree0eba4635140fe7550e072550e4f3c42f8aae0d75 /main.go
parent32a4b73cd07ebc918ead33f1de4297a6b6be1720 (diff)
logging, correct file serving, comments HEAD master
Diffstat (limited to 'main.go')
-rw-r--r--main.go21
1 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)
+	})
+}