Initial commit

This commit is contained in:
2018-12-06 15:18:05 +01:00
commit 2cb1bf1881
23 changed files with 827 additions and 0 deletions

22
static/static.go Normal file
View File

@ -0,0 +1,22 @@
package static
import (
"net/http"
"os"
)
// Dir serves the files in the given directory or
// uses the given handler to handles missing files
func Dir(dirPath string, stripPrefix string, notFoundHandler http.Handler) http.Handler {
root := http.Dir(dirPath)
fs := http.FileServer(root)
fn := func(w http.ResponseWriter, r *http.Request) {
if _, err := os.Stat(dirPath + r.RequestURI); os.IsNotExist(err) {
notFoundHandler.ServeHTTP(w, r)
} else {
fs.ServeHTTP(w, r)
}
}
handler := http.StripPrefix(stripPrefix, http.HandlerFunc(fn))
return handler
}