use RWLock on handlers, fix close bug

This commit is contained in:
Aaron Bieber 2021-12-10 06:27:27 -07:00
parent 8c113d95c1
commit 68c586f805

24
main.go
View File

@ -60,12 +60,15 @@ type userHandler struct {
name string name string
} }
type userHandlers []userHandler type userHandlers struct {
list []userHandler
mu sync.RWMutex
}
func (u userHandlers) find(name string) *userHandler { func (u *userHandlers) find(name string) *userHandler {
for i := range u { for i := range u.list {
if u[i].name == name { if u.list[i].name == name {
return &u[i] return &u.list[i]
} }
} }
return nil return nil
@ -233,12 +236,12 @@ func main() {
ht.Comment = '#' ht.Comment = '#'
ht.TrimLeadingSpace = true ht.TrimLeadingSpace = true
err = p.Close() entries, err := ht.ReadAll()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
entries, err := ht.ReadAll() err = p.Close()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -251,7 +254,7 @@ func main() {
if auth { if auth {
for u := range users { for u := range users {
uPath := path.Join(davDir, u) uPath := path.Join(davDir, u)
handlers = append(handlers, userHandler{ handlers.list = append(handlers.list, userHandler{
name: u, name: u,
dav: &webdav.Handler{ dav: &webdav.Handler{
LockSystem: webdav.NewMemLS(), LockSystem: webdav.NewMemLS(),
@ -261,7 +264,7 @@ func main() {
}) })
} }
} else { } else {
handlers = append(handlers, userHandler{ handlers.list = append(handlers.list, userHandler{
name: "", name: "",
dav: &webdav.Handler{ dav: &webdav.Handler{
LockSystem: webdav.NewMemLS(), LockSystem: webdav.NewMemLS(),
@ -296,7 +299,10 @@ func main() {
} }
} }
handlers.mu.RLock()
handler := handlers.find(user) handler := handlers.find(user)
handlers.mu.RUnlock()
if handler == nil { if handler == nil {
http.NotFound(w, r) http.NotFound(w, r)
return return