2021-05-09 10:42:12 +00:00
|
|
|
package web
|
2021-05-09 09:18:21 +00:00
|
|
|
|
2021-05-11 08:33:00 +00:00
|
|
|
// stuff.go is used for meta stuff about the wiki or all hyphae at once.
|
2021-05-09 09:18:21 +00:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
|
2021-06-19 04:51:10 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/files"
|
|
|
|
|
2021-05-09 09:18:21 +00:00
|
|
|
"github.com/bouncepaw/mycorrhiza/hyphae"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/shroom"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/user"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
|
|
|
"github.com/bouncepaw/mycorrhiza/views"
|
|
|
|
)
|
|
|
|
|
2021-05-09 11:09:27 +00:00
|
|
|
func initStuff() {
|
2021-05-09 09:18:21 +00:00
|
|
|
http.HandleFunc("/list/", handlerList)
|
|
|
|
http.HandleFunc("/reindex/", handlerReindex)
|
|
|
|
http.HandleFunc("/update-header-links/", handlerUpdateHeaderLinks)
|
|
|
|
http.HandleFunc("/random/", handlerRandom)
|
|
|
|
http.HandleFunc("/about/", handlerAbout)
|
2021-06-15 20:35:24 +00:00
|
|
|
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, rq *http.Request) {
|
|
|
|
http.Redirect(w, rq, "/static/favicon.ico", http.StatusSeeOther)
|
|
|
|
})
|
2021-05-09 09:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handlerList shows a list of all hyphae in the wiki in random order.
|
|
|
|
func handlerList(w http.ResponseWriter, rq *http.Request) {
|
2021-05-09 10:42:12 +00:00
|
|
|
util.PrepareRq(rq)
|
|
|
|
util.HTTP200Page(w, views.BaseHTML("List of pages", views.HyphaListHTML(), user.FromRequest(rq)))
|
2021-05-09 09:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handlerReindex reindexes all hyphae by checking the wiki storage directory anew.
|
|
|
|
func handlerReindex(w http.ResponseWriter, rq *http.Request) {
|
2021-05-09 10:42:12 +00:00
|
|
|
util.PrepareRq(rq)
|
2021-05-09 09:18:21 +00:00
|
|
|
if ok := user.CanProceed(rq, "reindex"); !ok {
|
2021-05-09 11:09:27 +00:00
|
|
|
httpErr(w, http.StatusForbidden, cfg.HomeHypha, "Not enough rights", "You must be an admin to reindex hyphae.")
|
2021-05-09 09:18:21 +00:00
|
|
|
log.Println("Rejected", rq.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
hyphae.ResetCount()
|
2021-06-19 04:51:10 +00:00
|
|
|
log.Println("Reindexing hyphae in", files.HyphaeDir())
|
|
|
|
hyphae.Index(files.HyphaeDir())
|
2021-05-09 09:18:21 +00:00
|
|
|
log.Println("Indexed", hyphae.Count(), "hyphae")
|
|
|
|
http.Redirect(w, rq, "/", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlerUpdateHeaderLinks updates header links by reading the configured hypha, if there is any, or resorting to default values.
|
|
|
|
//
|
2021-07-05 04:22:17 +00:00
|
|
|
// See https://mycorrhiza.wiki/hypha/configuration/header
|
2021-05-09 09:18:21 +00:00
|
|
|
func handlerUpdateHeaderLinks(w http.ResponseWriter, rq *http.Request) {
|
2021-05-09 10:42:12 +00:00
|
|
|
util.PrepareRq(rq)
|
2021-05-09 09:18:21 +00:00
|
|
|
if ok := user.CanProceed(rq, "update-header-links"); !ok {
|
2021-05-09 11:09:27 +00:00
|
|
|
httpErr(w, http.StatusForbidden, cfg.HomeHypha, "Not enough rights", "You must be a moderator to update header links.")
|
2021-05-09 09:18:21 +00:00
|
|
|
log.Println("Rejected", rq.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
shroom.SetHeaderLinks()
|
|
|
|
http.Redirect(w, rq, "/", http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlerRandom redirects to a random hypha.
|
|
|
|
func handlerRandom(w http.ResponseWriter, rq *http.Request) {
|
2021-05-09 10:42:12 +00:00
|
|
|
util.PrepareRq(rq)
|
2021-05-09 09:18:21 +00:00
|
|
|
var (
|
|
|
|
randomHyphaName string
|
|
|
|
amountOfHyphae = hyphae.Count()
|
|
|
|
)
|
|
|
|
if amountOfHyphae == 0 {
|
2021-05-09 11:09:27 +00:00
|
|
|
httpErr(w, http.StatusNotFound, cfg.HomeHypha, "There are no hyphae",
|
2021-05-09 09:18:21 +00:00
|
|
|
"It is impossible to display a random hypha because the wiki does not contain any hyphae")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
i := rand.Intn(amountOfHyphae)
|
|
|
|
for h := range hyphae.YieldExistingHyphae() {
|
|
|
|
if i == 0 {
|
|
|
|
randomHyphaName = h.Name
|
|
|
|
}
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
http.Redirect(w, rq, "/hypha/"+randomHyphaName, http.StatusSeeOther)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlerAbout shows a summary of wiki's software.
|
|
|
|
func handlerAbout(w http.ResponseWriter, rq *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/html;charset=utf-8")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2021-05-09 10:42:12 +00:00
|
|
|
_, err := io.WriteString(w, views.BaseHTML("About "+cfg.WikiName, views.AboutHTML(), user.FromRequest(rq)))
|
2021-05-09 09:18:21 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
}
|