Add a landing page with instructions on how to create a new wiki

This commit is contained in:
Aaron Bieber 2021-05-19 21:14:22 -06:00
parent 490a1a757a
commit e38b3cbc23

67
main.go
View File

@ -14,6 +14,7 @@ import (
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
"text/template"
"time" "time"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
@ -21,10 +22,33 @@ import (
"suah.dev/protect" "suah.dev/protect"
) )
var twFile = "empty-5.1.23.html" // Landing will be used to fill our landing template
type Landing struct {
User string
URL string
}
//go:embed empty-5.1.23.html const landingPage = `
var tiddly embed.FS <h1>Hello{{if .User}} {{.User}}{{end}}! Welcome to widdler!</h1>
<p>To create a new TiddlyWiki html file, simply append an html file name to the URL in the address bar!</p>
<h3>For example:</h3>
<a href="{{.URL}}">{{.URL}}</a>
<p>This will create a new wiki called "<b>wiki.html</b>"</p>
<p>After creating a wiki, this message will be replaced by a list of your wiki files.</p>
`
var (
twFile = "empty-5.1.23.html"
//go:embed empty-5.1.23.html
tiddly embed.FS
templ *template.Template
)
type userHandlers struct { type userHandlers struct {
dav *webdav.Handler dav *webdav.Handler
@ -49,7 +73,7 @@ func init() {
} }
flag.StringVar(&davDir, "wikis", dir, "Directory of TiddlyWikis to serve over WebDAV.") flag.StringVar(&davDir, "wikis", dir, "Directory of TiddlyWikis to serve over WebDAV.")
flag.StringVar(&listen, "http", "127.0.0.1:8080", "Listen on") flag.StringVar(&listen, "http", "localhost:8080", "Listen on")
flag.StringVar(&passPath, "htpass", fmt.Sprintf("%s/.htpasswd", dir), "Path to .htpasswd file..") flag.StringVar(&passPath, "htpass", fmt.Sprintf("%s/.htpasswd", dir), "Path to .htpasswd file..")
flag.BoolVar(&auth, "auth", true, "Enable HTTP Basic Authentication.") flag.BoolVar(&auth, "auth", true, "Enable HTTP Basic Authentication.")
flag.Parse() flag.Parse()
@ -61,6 +85,11 @@ func init() {
_ = protect.Unveil("/etc/resolv.conf", "r") _ = protect.Unveil("/etc/resolv.conf", "r")
_ = protect.Pledge("stdio wpath rpath cpath inet dns") _ = protect.Pledge("stdio wpath rpath cpath inet dns")
templ, err = template.New("landing").Parse(landingPage)
if err != nil {
log.Fatalln(err)
}
_, fErr := os.Stat(passPath) _, fErr := os.Stat(passPath)
if os.IsNotExist(fErr) { if os.IsNotExist(fErr) {
if auth { if auth {
@ -177,12 +206,12 @@ func main() {
} }
handler := handlers[user] handler := handlers[user]
up := path.Join(davDir, user) userPath := path.Join(davDir, user)
fp := path.Join(davDir, user, r.URL.Path) fullPath := path.Join(davDir, user, r.URL.Path)
_, dErr := os.Stat(up) _, dErr := os.Stat(userPath)
if os.IsNotExist(dErr) { if os.IsNotExist(dErr) {
mErr := os.Mkdir(up, 0700) mErr := os.Mkdir(userPath, 0700)
if mErr != nil { if mErr != nil {
http.Error(w, mErr.Error(), http.StatusInternalServerError) http.Error(w, mErr.Error(), http.StatusInternalServerError)
return return
@ -197,7 +226,7 @@ func main() {
if isHTML { if isHTML {
// HTML files will be created or sent back // HTML files will be created or sent back
err := createEmpty(fp) err := createEmpty(fullPath)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
@ -206,8 +235,24 @@ func main() {
handler.dav.ServeHTTP(w, r) handler.dav.ServeHTTP(w, r)
} else { } else {
// Everything else is browsable // Everything else is browsable
handler.fs.ServeHTTP(w, r) entries, err := os.ReadDir(userPath)
return if err != nil {
log.Println(err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if len(entries) > 0 {
handler.fs.ServeHTTP(w, r)
} else {
l := Landing{
URL: fmt.Sprintf("http://%s/wiki.html", listen),
}
if user != "" {
l.User = user
}
err = templ.ExecuteTemplate(w, "landing", l)
}
} }
})) }))