From 34b48f411c0f8cb03e6084ba3ee812fe276a48b0 Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Sun, 28 Oct 2012 19:21:42 +0100 Subject: [PATCH] Use config paths to serve files --- config.go | 3 +++ trantor.go | 17 ++++++++--------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/config.go b/config.go index 0a3814e..fdbb611 100644 --- a/config.go +++ b/config.go @@ -14,6 +14,9 @@ const ( BOOKS_PATH = "books/" COVER_PATH = "cover/" NEW_PATH = "new/" + CSS_PATH = "css/" + JS_PATH = "js/" + IMG_PATH = "img/" RESIZE_CMD = "/usr/bin/convert -resize 300 -quality 60 " RESIZE_THUMB_CMD = "/usr/bin/convert -resize 60 -quality 60 " ) diff --git a/trantor.go b/trantor.go index c9540c5..2ab5037 100644 --- a/trantor.go +++ b/trantor.go @@ -66,11 +66,6 @@ func downloadHandler(w http.ResponseWriter, r *http.Request) { http.ServeFile(w, r, file) } -func fileHandler(path string) { - h := http.FileServer(http.Dir(path[1:])) - http.Handle(path, http.StripPrefix(path, h)) -} - type indexData struct { S Status Books []Book @@ -144,10 +139,14 @@ func main() { http.HandleFunc("/about/", aboutHandler) http.HandleFunc("/books/", downloadHandler) http.HandleFunc("/settings/", settingsHandler) - fileHandler("/img/") - fileHandler("/cover/") - fileHandler("/css/") - fileHandler("/js/") + h := http.FileServer(http.Dir(IMG_PATH)) + http.Handle("/img/", http.StripPrefix("/img/", h)) + h = http.FileServer(http.Dir(COVER_PATH)) + http.Handle("/cover/", http.StripPrefix("/cover/", h)) + h = http.FileServer(http.Dir(CSS_PATH)) + http.Handle("/css/", http.StripPrefix("/css/", h)) + h = http.FileServer(http.Dir(JS_PATH)) + http.Handle("/js/", http.StripPrefix("/js/", h)) http.HandleFunc("/", indexHandler) panic(http.ListenAndServe(":"+PORT, nil)) }