From 3be0a28eb8d7d150d8d0fdd14d20bb633a95d1a9 Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Wed, 15 Aug 2012 11:31:05 +0200 Subject: [PATCH] Use http.FileServer to serve files --- trantor.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/trantor.go b/trantor.go index 3a41fa3..c5f97e9 100644 --- a/trantor.go +++ b/trantor.go @@ -42,9 +42,9 @@ func bookHandler(coll *mgo.Collection, w http.ResponseWriter, r *http.Request) { } -func sendFile(w http.ResponseWriter, r *http.Request) { - path := r.URL.Path[1:] - http.ServeFile(w, r, path) +func fileHandler(path string) { + h := http.FileServer(http.Dir(path[1:])) + http.Handle(path, http.StripPrefix(path, h)) } func main() { @@ -58,10 +58,10 @@ func main() { http.HandleFunc("/book/", func(w http.ResponseWriter, r *http.Request) { bookHandler(coll, w, r) }) http.HandleFunc("/search/", func(w http.ResponseWriter, r *http.Request) { searchHandler(coll, w, r) }) - // FIXME: shows content - http.HandleFunc("/img/", sendFile) - http.HandleFunc("/cover/", sendFile) - http.HandleFunc("/books/", sendFile) + http.HandleFunc("/upload/", func(w http.ResponseWriter, r *http.Request) { uploadHandler(coll, w, r) }) + fileHandler("/img/") + fileHandler("/cover/") + fileHandler("/books/") http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { loadTemplate(w, "front", num) }) http.ListenAndServe(":8080", nil) }