diff --git a/http_admin.go b/http_admin.go new file mode 100644 index 0000000..cd3675d --- /dev/null +++ b/http_admin.go @@ -0,0 +1,33 @@ +package main + +import ( + "log" + "net/http" + + "github.com/bouncepaw/mycorrhiza/templates" + "github.com/bouncepaw/mycorrhiza/user" +) + +// This is not init(), because user.AuthUsed is not set at init-stage. +func initAdmin() { + if user.AuthUsed { + http.HandleFunc("/admin", handlerAdmin) + http.HandleFunc("/admin/shutdown", handlerAdminShutdown) + } +} + +func handlerAdmin(w http.ResponseWriter, rq *http.Request) { + log.Println(rq.URL) + if user.CanProceed(rq, "admin") { + w.Header().Set("Content-Type", "text/html;charset=utf-8") + w.WriteHeader(http.StatusOK) + w.Write([]byte(base("Admin panel", templates.AdminPanelHTML(), user.FromRequest(rq)))) + } +} + +func handlerAdminShutdown(w http.ResponseWriter, rq *http.Request) { + log.Println(rq.URL) + if user.CanProceed(rq, "admin/shutdown") && rq.Method == "POST" { + log.Fatal("An admin commanded the wiki to shutdown") + } +} diff --git a/hyphae/files.go b/hyphae/files.go index 7a55090..56ccda5 100644 --- a/hyphae/files.go +++ b/hyphae/files.go @@ -11,6 +11,9 @@ import ( // Index finds all hypha files in the full `path` and saves them to the hypha storage. func Index(path string) { + byNamesMutex.Lock() + defer byNamesMutex.Unlock() + byNames = make(map[string]*Hypha) ch := make(chan *Hypha, 5) go func(ch chan *Hypha) { diff --git a/main.go b/main.go index a19fd18..e667d8f 100644 --- a/main.go +++ b/main.go @@ -76,12 +76,6 @@ func handlerReindex(w http.ResponseWriter, rq *http.Request) { } // Stop the wiki -func handlerAdminShutdown(w http.ResponseWriter, rq *http.Request) { - log.Println(rq.URL) - if user.CanProceed(rq, "admin/shutdown") { - log.Fatal("An admin commanded the wiki to shutdown") - } -} // Update header links by reading the configured hypha, if there is any, or resorting to default values. func handlerUpdateHeaderLinks(w http.ResponseWriter, rq *http.Request) { @@ -187,6 +181,8 @@ func main() { go handleGemini() + // See http_admin.go for /admin, /admin/* + initAdmin() // See http_readers.go for /page/, /hypha/, /text/, /binary/ // See http_mutators.go for /upload-binary/, /upload-text/, /edit/, /delete-ask/, /delete-confirm/, /rename-ask/, /rename-confirm/, /unattach-ask/, /unattach-confirm/ // See http_auth.go for /login, /login-data, /logout, /logout-confirm @@ -203,12 +199,9 @@ func main() { }) http.HandleFunc("/static/common.css", handlerStyle) http.HandleFunc("/static/icon/", handlerIcon) - if user.AuthUsed { - http.HandleFunc("/admin/shutdown", handlerAdminShutdown) - } + http.HandleFunc("/robots.txt", handlerRobotsTxt) http.HandleFunc("/", func(w http.ResponseWriter, rq *http.Request) { http.Redirect(w, rq, "/hypha/"+util.HomePage, http.StatusSeeOther) }) - http.HandleFunc("/robots.txt", handlerRobotsTxt) log.Fatal(http.ListenAndServe("0.0.0.0:"+util.ServerPort, nil)) } diff --git a/templates/admin.qtpl b/templates/admin.qtpl new file mode 100644 index 0000000..8932da7 --- /dev/null +++ b/templates/admin.qtpl @@ -0,0 +1,30 @@ +{% func AdminPanelHTML() %} +