filebrowser/http/data.go
Henrique Dias 866375141f fix: strip baseurl (#626)
Former-commit-id: 5daa76063fbd8766115a722336d544d2f6156c74 [formerly 3d644f06b38b6c20f5549b3f2f780556881f216e] [formerly beb819187116079ce83329e85bb1b1d5e6b908ea [formerly 73e0acaa71]]
Former-commit-id: 01dfc4bb6016d6c3de2bb932695e7ca6b9185ff6 [formerly cdee480885c5050d9311f41f39ef7b4feefcc602]
Former-commit-id: 82bbfed106ddae78f22d01cabc11f314c19a1796
2019-01-09 08:13:57 +00:00

69 lines
1.5 KiB
Go

package http
import (
"log"
"net/http"
"strconv"
"github.com/filebrowser/filebrowser/v2/runner"
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/filebrowser/filebrowser/v2/users"
)
type handleFunc func(w http.ResponseWriter, r *http.Request, d *data) (int, error)
type data struct {
*runner.Runner
settings *settings.Settings
server *settings.Server
store *storage.Storage
user *users.User
raw interface{}
}
// Check implements rules.Checker.
func (d *data) Check(path string) bool {
for _, rule := range d.user.Rules {
if rule.Matches(path) {
return rule.Allow
}
}
for _, rule := range d.settings.Rules {
if rule.Matches(path) {
return rule.Allow
}
}
return true
}
func handle(fn handleFunc, prefix string, storage *storage.Storage, server *settings.Server) http.Handler {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
settings, err := storage.Settings.Get()
if err != nil {
log.Fatalln("ERROR: couldn't get settings")
return
}
status, err := fn(w, r, &data{
Runner: &runner.Runner{Settings: settings},
store: storage,
settings: settings,
server: server,
})
if status != 0 {
txt := http.StatusText(status)
http.Error(w, strconv.Itoa(status)+" "+txt, status)
}
if status >= 400 || err != nil {
log.Printf("%s: %v %s %v", r.URL.Path, status, r.RemoteAddr, err)
}
})
return http.StripPrefix(prefix, handler)
}