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
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
}
Loading…
Cancel
Save