reduce duplicated code a bit

This commit is contained in:
Aaron Bieber 2021-12-11 16:07:52 -07:00
parent e3b3dad3ee
commit 76ec74b21b
2 changed files with 20 additions and 17 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
widdler widdler
.htpasswd .htpasswd
dist/ dist/
.idea

34
main.go
View File

@ -179,6 +179,22 @@ func prompt(prompt string, secure bool) (string, error) {
return input, nil return input, nil
} }
func addHandler(u, uPath string) {
handlers.list = append(handlers.list, userHandler{
name: u,
dav: &webdav.Handler{
LockSystem: webdav.NewMemLS(),
FileSystem: webdav.Dir(uPath),
Logger: func(r *http.Request, err error) {
if err != nil {
log.Print(err)
}
},
},
fs: http.FileServer(http.Dir(uPath)),
})
}
func main() { func main() {
if version { if version {
fmt.Println(build) fmt.Println(build)
@ -259,24 +275,10 @@ func main() {
if auth == "basic" || auth == "header" { if auth == "basic" || auth == "header" {
for u := range users { for u := range users {
uPath := path.Join(davDir, u) uPath := path.Join(davDir, u)
handlers.list = append(handlers.list, userHandler{ addHandler(u, uPath)
name: u,
dav: &webdav.Handler{
LockSystem: webdav.NewMemLS(),
FileSystem: webdav.Dir(uPath),
},
fs: http.FileServer(http.Dir(uPath)),
})
} }
} else { } else {
handlers.list = append(handlers.list, userHandler{ addHandler("", davDir)
name: "",
dav: &webdav.Handler{
LockSystem: webdav.NewMemLS(),
FileSystem: webdav.Dir(davDir),
},
fs: http.FileServer(http.Dir(davDir)),
})
} }
mux := http.NewServeMux() mux := http.NewServeMux()