Skip to content

Commit

Permalink
reduce duplicated code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
qbit committed Dec 11, 2021
1 parent e3b3dad commit 76ec74b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
widdler
.htpasswd
dist/
.idea
36 changes: 19 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -413,4 +415,4 @@ func main() {
log.Printf("Listening for HTTP on 'http://%s'", listen)
log.Fatalln(s.Serve(lis))
}
}
}

0 comments on commit 76ec74b

Please sign in to comment.