Browse Source

Removed reinvented wheel from Static.Handle

master
Gisle Aune 7 years ago
parent
commit
d10b7f9ef9
  1. 25
      static.go

25
static.go

@ -1,8 +1,6 @@
package wrouter package wrouter
import ( import (
"io"
"mime"
"net/http" "net/http"
"os" "os"
"path" "path"
@ -34,32 +32,15 @@ func (static *Static) Handle(urlPath string, w http.ResponseWriter, req *http.Re
return true return true
} }
// Try loading the file
// Look for the file
filepath := path.Join(static.path, subpath) filepath := path.Join(static.path, subpath)
info, err := os.Stat(filepath) info, err := os.Stat(filepath)
if err != nil || info.IsDir() { if err != nil || info.IsDir() {
return false 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 return true
} }
Loading…
Cancel
Save