filebrowser/http/data.go
Henrique Dias 01929c72ea feat: make server options a struct (#615)
Former-commit-id: a54bc7c8a0fb700d4f90f78c54d6cb028e8acea7 [formerly a0946a1d5b82ce5681684bc0f871bd892c4d6c3f] [formerly 6b23b0abbe5cde6a6348f982975d15a1161359de [formerly 0e7abaa7fb]]
Former-commit-id: cf9ea1110bcb8b5eb30ecd25db1928659ecea922 [formerly 38cebeff1f9e6b03760f7b277a3941f7bd733fb5]
Former-commit-id: db44c1d5ff8fed361849b5bf1fe48e04c75d7ce7
2019-01-08 10:29:09 +00:00

73 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)
}
})
if prefix == "" {
return handler
}
return http.StripPrefix(prefix, handler)
}