show index of files, make example a bit better

This commit is contained in:
Aaron Bieber 2021-05-17 15:59:48 -06:00
parent bc89676155
commit 4c570ed713
2 changed files with 38 additions and 6 deletions

View File

@ -15,6 +15,12 @@ go get -u suah.dev/widdler
# Running
```
mkdir wiki
cd wiki
# OpenBSD:
htpasswd .htpasswd youruser
# or on Linux/macOS
# htpasswd -c -B youruser
widdler
```
@ -26,3 +32,6 @@ create the wiki file based off the current `empty.html` TiddlyWiki version.
# Saving changes
Simply hit the save button!
# TODO
- [ ] Multi-user support

35
main.go
View File

@ -12,6 +12,7 @@ import (
"os"
"path"
"path/filepath"
"regexp"
"strings"
"time"
@ -121,6 +122,7 @@ func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", logger(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, ".htpasswd") {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
@ -133,20 +135,41 @@ func main() {
return
}
//wdav.Prefix = user
fp := path.Join(davDir, r.URL.Path)
err := createEmpty(fp)
/*
fp := path.Join(davDir, user, r.URL.Path)
_, dErr := os.Stat(user)
if os.IsNotExist(dErr) {
mErr := os.Mkdir(path.Join(davDir, user), 0700)
if mErr != nil {
http.Error(w, mErr.Error(), http.StatusInternalServerError)
return
}
}
*/
isHTML, err := regexp.Match(`\.html$`, []byte(r.URL.Path))
if err != nil {
log.Println(err)
fmt.Fprintf(w, err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if r.URL.Path == "/" {
if isHTML {
// HTML files will be created or sent back
err := createEmpty(fp)
if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
wdav.ServeHTTP(w, r)
} else {
// Everything else is browsable
idxHandler.ServeHTTP(w, r)
return
}
wdav.ServeHTTP(w, r)
}))
s := http.Server{