From d10b7f9ef92a8945d3ce8f9b6159de619240e6f6 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Mon, 7 Aug 2017 07:28:41 +0200 Subject: [PATCH] Removed reinvented wheel from Static.Handle --- static.go | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/static.go b/static.go index 520ee67..9b3bea7 100644 --- a/static.go +++ b/static.go @@ -1,8 +1,6 @@ package wrouter import ( - "io" - "mime" "net/http" "os" "path" @@ -34,32 +32,15 @@ func (static *Static) Handle(urlPath string, w http.ResponseWriter, req *http.Re return true } - // Try loading the file + // Look for the file filepath := path.Join(static.path, subpath) info, err := os.Stat(filepath) if err != nil || info.IsDir() { return false } - file, err := os.Open(filepath) - if err != nil || file == nil { - return false - } - - // Find and convert extension - ep := strings.LastIndex(filepath, ".") - ext := "" - if ep != -1 { - ext = filepath[ep:] - } - mimeType := mime.TypeByExtension(ext) - if mimeType == "" { - mimeType = "text/plain" - } - w.Header().Set("Content-Type", mimeType) - // Submit - w.WriteHeader(200) - io.Copy(w, file) + // Serve it + http.ServeFile(w, req, filepath) return true }