Use http.FileServer to serve files

This commit is contained in:
Las Zenow 2012-08-15 11:31:05 +02:00
parent afacc1349e
commit 3be0a28eb8

View file

@ -42,9 +42,9 @@ func bookHandler(coll *mgo.Collection, w http.ResponseWriter, r *http.Request) {
} }
func sendFile(w http.ResponseWriter, r *http.Request) { func fileHandler(path string) {
path := r.URL.Path[1:] h := http.FileServer(http.Dir(path[1:]))
http.ServeFile(w, r, path) http.Handle(path, http.StripPrefix(path, h))
} }
func main() { func main() {
@ -58,10 +58,10 @@ func main() {
http.HandleFunc("/book/", func(w http.ResponseWriter, r *http.Request) { bookHandler(coll, w, r) }) 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) }) http.HandleFunc("/search/", func(w http.ResponseWriter, r *http.Request) { searchHandler(coll, w, r) })
// FIXME: shows content http.HandleFunc("/upload/", func(w http.ResponseWriter, r *http.Request) { uploadHandler(coll, w, r) })
http.HandleFunc("/img/", sendFile) fileHandler("/img/")
http.HandleFunc("/cover/", sendFile) fileHandler("/cover/")
http.HandleFunc("/books/", sendFile) fileHandler("/books/")
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { loadTemplate(w, "front", num) }) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { loadTemplate(w, "front", num) })
http.ListenAndServe(":8080", nil) http.ListenAndServe(":8080", nil)
} }