Skip to content

Commit 76ec74b

Browse files
committed
reduce duplicated code a bit
1 parent e3b3dad commit 76ec74b

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
widdler
22
.htpasswd
33
dist/
4+
.idea

main.go

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,22 @@ func prompt(prompt string, secure bool) (string, error) {
179179
return input, nil
180180
}
181181

182+
func addHandler(u, uPath string) {
183+
handlers.list = append(handlers.list, userHandler{
184+
name: u,
185+
dav: &webdav.Handler{
186+
LockSystem: webdav.NewMemLS(),
187+
FileSystem: webdav.Dir(uPath),
188+
Logger: func(r *http.Request, err error) {
189+
if err != nil {
190+
log.Print(err)
191+
}
192+
},
193+
},
194+
fs: http.FileServer(http.Dir(uPath)),
195+
})
196+
}
197+
182198
func main() {
183199
if version {
184200
fmt.Println(build)
@@ -259,24 +275,10 @@ func main() {
259275
if auth == "basic" || auth == "header" {
260276
for u := range users {
261277
uPath := path.Join(davDir, u)
262-
handlers.list = append(handlers.list, userHandler{
263-
name: u,
264-
dav: &webdav.Handler{
265-
LockSystem: webdav.NewMemLS(),
266-
FileSystem: webdav.Dir(uPath),
267-
},
268-
fs: http.FileServer(http.Dir(uPath)),
269-
})
278+
addHandler(u, uPath)
270279
}
271280
} else {
272-
handlers.list = append(handlers.list, userHandler{
273-
name: "",
274-
dav: &webdav.Handler{
275-
LockSystem: webdav.NewMemLS(),
276-
FileSystem: webdav.Dir(davDir),
277-
},
278-
fs: http.FileServer(http.Dir(davDir)),
279-
})
281+
addHandler("", davDir)
280282
}
281283

282284
mux := http.NewServeMux()
@@ -413,4 +415,4 @@ func main() {
413415
log.Printf("Listening for HTTP on 'http://%s'", listen)
414416
log.Fatalln(s.Serve(lis))
415417
}
416-
}
418+
}

0 commit comments

Comments
 (0)