Removed reinvented wheel from Static.Handle

This commit is contained in:
2017-08-07 07:28:41 +02:00
parent 7e7c641949
commit d10b7f9ef9
+3 -22
View File
@@ -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
}