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
.htpasswd
dist/
.idea

36
main.go
View File

@ -179,6 +179,22 @@ func prompt(prompt string, secure bool) (string, error) {
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() {
if version {
fmt.Println(build)
@ -259,24 +275,10 @@ func main() {
if auth == "basic" || auth == "header" {
for u := range users {
uPath := path.Join(davDir, u)
handlers.list = append(handlers.list, userHandler{
name: u,
dav: &webdav.Handler{
LockSystem: webdav.NewMemLS(),
FileSystem: webdav.Dir(uPath),
},
fs: http.FileServer(http.Dir(uPath)),
})
addHandler(u, uPath)
}
} else {
handlers.list = append(handlers.list, userHandler{
name: "",
dav: &webdav.Handler{
LockSystem: webdav.NewMemLS(),
FileSystem: webdav.Dir(davDir),
},
fs: http.FileServer(http.Dir(davDir)),
})
addHandler("", davDir)
}
mux := http.NewServeMux()
@ -413,4 +415,4 @@ func main() {
log.Printf("Listening for HTTP on 'http://%s'", listen)
log.Fatalln(s.Serve(lis))
}
}
}