From 6d7156c30fbe581963b49cd5f5fe6eb8e3b5ee41 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Sat, 26 Sep 2015 22:08:12 +0100 Subject: [PATCH] serveAssets function --- hugo.go | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/hugo.go b/hugo.go index e67a343d..28a86e25 100644 --- a/hugo.go +++ b/hugo.go @@ -65,26 +65,6 @@ func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error return 0, nil } - // Serve the static assets - if page == "assets" { - filename := strings.Replace(r.URL.Path, "/admin/", "", 1) - file, err := assets.Asset(filename) - - if err != nil { - return 404, nil - } - - // Get the file extension ant its mime type - extension := filepath.Ext(filename) - mime := mime.TypeByExtension(extension) - - // Write the header with the Content-Type and write the file - // content to the buffer - w.Header().Set("Content-Type", mime) - w.Write(file) - return 200, nil - } - // If the url matches exactly with /admin/settings/ serve that page // page variable isn't used here to avoid people using URLs like // "/admin/settings/something". @@ -107,6 +87,11 @@ func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error return 0, nil } + // Serve the static assets + if page == "assets" { + return serveAssets(w, r) + } + // Browse page if page == "browse" { code, err = browse.ServeHTTP(w, r, h.Config) @@ -128,3 +113,22 @@ func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error return h.Next.ServeHTTP(w, r) } + +func serveAssets(w http.ResponseWriter, r *http.Request) (int, error) { + filename := strings.Replace(r.URL.Path, "/admin/", "", 1) + file, err := assets.Asset(filename) + + if err != nil { + return 404, nil + } + + // Get the file extension ant its mime type + extension := filepath.Ext(filename) + mime := mime.TypeByExtension(extension) + + // Write the header with the Content-Type and write the file + // content to the buffer + w.Header().Set("Content-Type", mime) + w.Write(file) + return 200, nil +}