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 # Running
``` ```
mkdir wiki
cd wiki
# OpenBSD:
htpasswd .htpasswd youruser
# or on Linux/macOS
# htpasswd -c -B youruser
widdler widdler
``` ```
@ -26,3 +32,6 @@ create the wiki file based off the current `empty.html` TiddlyWiki version.
# Saving changes # Saving changes
Simply hit the save button! Simply hit the save button!
# TODO
- [ ] Multi-user support

33
main.go
View File

@ -12,6 +12,7 @@ import (
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
"regexp"
"strings" "strings"
"time" "time"
@ -121,6 +122,7 @@ func main() {
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", logger(func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/", logger(func(w http.ResponseWriter, r *http.Request) {
if strings.Contains(r.URL.Path, ".htpasswd") { if strings.Contains(r.URL.Path, ".htpasswd") {
http.Error(w, "Unauthorized", http.StatusUnauthorized) http.Error(w, "Unauthorized", http.StatusUnauthorized)
return return
@ -133,20 +135,41 @@ func main() {
return return
} }
//wdav.Prefix = user
fp := path.Join(davDir, r.URL.Path) fp := path.Join(davDir, r.URL.Path)
/*
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 {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if isHTML {
// HTML files will be created or sent back
err := createEmpty(fp) err := createEmpty(fp)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
fmt.Fprintf(w, err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
wdav.ServeHTTP(w, r)
if r.URL.Path == "/" { } else {
// Everything else is browsable
idxHandler.ServeHTTP(w, r) idxHandler.ServeHTTP(w, r)
return return
} }
wdav.ServeHTTP(w, r)
})) }))
s := http.Server{ s := http.Server{