1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 03:36:16 +00:00

Implement the admin panel and fix a bug with /reindex

This commit is contained in:
bouncepaw 2021-02-18 20:16:15 +05:00
parent 58ed6ecdfb
commit 9d89923ee5
5 changed files with 149 additions and 10 deletions

33
http_admin.go Normal file
View File

@ -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")
}
}

View File

@ -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) {

13
main.go
View File

@ -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))
}

30
templates/admin.qtpl Normal file
View File

@ -0,0 +1,30 @@
{% func AdminPanelHTML() %}
<div class="layout">
<main class="main-width">
<h1>Admininstrative functions</h1>
<section>
<h2>Safe things</h2>
<ul>
<li><a href="/about">About this wiki<a></li>
<li><a href="/user-list">User list</a></li>
<li><a href="/update-header-links">Update header links</a></li>
</ul>
</section>
<section>
<h2>Dangerous things</h2>
<form action="/admin/shutdown" method="POST" style="float:left">
<fieldset>
<legend>Shutdown wiki</legend>
<input type="submit">
</fieldset>
</form>
<form action="/reindex" method="GET" style="float:left">
<fieldset>
<legend>Reindex hyphae</legend>
<input type="submit">
</fieldset>
</form>
</section>
</main>
</div>
{% endfunc %}

80
templates/admin.qtpl.go Normal file
View File

@ -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(`
<div class="layout">
<main class="main-width">
<h1>Admininstrative functions</h1>
<section>
<h2>Safe things</h2>
<ul>
<li><a href="/about">About this wiki<a></li>
<li><a href="/user-list">User list</a></li>
<li><a href="/update-header-links">Update header links</a></li>
</ul>
</section>
<section>
<h2>Dangerous things</h2>
<form action="/admin/shutdown" method="POST" style="float:left">
<fieldset>
<legend>Shutdown wiki</legend>
<input type="submit">
</fieldset>
</form>
<form action="/reindex" method="GET" style="float:left">
<fieldset>
<legend>Reindex hyphae</legend>
<input type="submit">
</fieldset>
</form>
</section>
</main>
</div>
`)
//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
}