From 9d89923ee519f1a4edc3d6c40a906b57f4e0b5be Mon Sep 17 00:00:00 2001 From: bouncepaw Date: Thu, 18 Feb 2021 20:16:15 +0500 Subject: [PATCH] Implement the admin panel and fix a bug with /reindex --- http_admin.go | 33 +++++++++++++++++ hyphae/files.go | 3 ++ main.go | 13 ++----- templates/admin.qtpl | 30 ++++++++++++++++ templates/admin.qtpl.go | 80 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 149 insertions(+), 10 deletions(-) create mode 100644 http_admin.go create mode 100644 templates/admin.qtpl create mode 100644 templates/admin.qtpl.go 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() %} +
+
+

Admininstrative functions

+
+

Safe things

+ +
+
+

Dangerous things

+
+
+ Shutdown wiki + +
+
+
+
+ Reindex hyphae + +
+
+
+
+
+{% endfunc %} diff --git a/templates/admin.qtpl.go b/templates/admin.qtpl.go new file mode 100644 index 0000000..c017c8d --- /dev/null +++ b/templates/admin.qtpl.go @@ -0,0 +1,80 @@ +// Code generated by qtc from "admin.qtpl". DO NOT EDIT. +// See https://github.com/valyala/quicktemplate for details. + +//line templates/admin.qtpl:1 +package templates + +//line templates/admin.qtpl:1 +import ( + qtio422016 "io" + + qt422016 "github.com/valyala/quicktemplate" +) + +//line templates/admin.qtpl:1 +var ( + _ = qtio422016.Copy + _ = qt422016.AcquireByteBuffer +) + +//line templates/admin.qtpl:1 +func StreamAdminPanelHTML(qw422016 *qt422016.Writer) { +//line templates/admin.qtpl:1 + qw422016.N().S(` +
+
+

Admininstrative functions

+
+

Safe things

+ +
+
+

Dangerous things

+
+
+ Shutdown wiki + +
+
+
+
+ Reindex hyphae + +
+
+
+
+
+`) +//line templates/admin.qtpl:30 +} + +//line templates/admin.qtpl:30 +func WriteAdminPanelHTML(qq422016 qtio422016.Writer) { +//line templates/admin.qtpl:30 + qw422016 := qt422016.AcquireWriter(qq422016) +//line templates/admin.qtpl:30 + StreamAdminPanelHTML(qw422016) +//line templates/admin.qtpl:30 + qt422016.ReleaseWriter(qw422016) +//line templates/admin.qtpl:30 +} + +//line templates/admin.qtpl:30 +func AdminPanelHTML() string { +//line templates/admin.qtpl:30 + qb422016 := qt422016.AcquireByteBuffer() +//line templates/admin.qtpl:30 + WriteAdminPanelHTML(qb422016) +//line templates/admin.qtpl:30 + qs422016 := string(qb422016.B) +//line templates/admin.qtpl:30 + qt422016.ReleaseByteBuffer(qb422016) +//line templates/admin.qtpl:30 + return qs422016 +//line templates/admin.qtpl:30 +}