1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-10-30 11:46:16 +00:00
mycorrhiza/main.go

111 lines
3.3 KiB
Go
Raw Normal View History

2020-08-31 17:52:26 +00:00
//go:generate go get -u github.com/valyala/quicktemplate/qtc
//go:generate qtc -dir=templates
2020-06-12 16:22:02 +00:00
package main
import (
"fmt"
2020-06-13 11:18:11 +00:00
"log"
"net/http"
2020-06-12 16:22:02 +00:00
"os"
"path/filepath"
"regexp"
2020-08-08 20:10:28 +00:00
"github.com/bouncepaw/mycorrhiza/history"
2020-08-31 17:52:26 +00:00
"github.com/bouncepaw/mycorrhiza/templates"
"github.com/bouncepaw/mycorrhiza/util"
2020-06-12 16:22:02 +00:00
)
// WikiDir is a rooted path to the wiki storage directory.
var WikiDir string
// HyphaPattern is a pattern which all hyphae must match. Not used currently.
var HyphaPattern = regexp.MustCompile(`[^?!:#@><*|"\'&%]+`)
// HyphaStorage is a mapping between canonical hypha names and their meta information.
var HyphaStorage = make(map[string]*HyphaData)
2020-08-05 20:19:14 +00:00
// IterateHyphaNamesWith is a closure to be passed to subpackages to let them iterate all hypha names read-only.
func IterateHyphaNamesWith(f func(string)) {
for hyphaName, _ := range HyphaStorage {
f(hyphaName)
}
}
// HttpErr is used by many handlers to signal errors in a compact way.
func HttpErr(w http.ResponseWriter, status int, name, title, errMsg string) {
log.Println(errMsg, "for", name)
w.Header().Set("Content-Type", "text/html;charset=utf-8")
w.WriteHeader(status)
fmt.Fprint(w, base(title, fmt.Sprintf(
`<p>%s. <a href="/page/%s">Go back to the hypha.<a></p>`,
errMsg, name)))
}
// Show all hyphae
func handlerList(w http.ResponseWriter, rq *http.Request) {
log.Println(rq.URL)
2020-08-31 17:52:26 +00:00
var tbody string
// It should be moved to templates too, right?
for name, data := range HyphaStorage {
2020-08-31 17:52:26 +00:00
tbody += fmt.Sprintf(`
<tr>
<td><a href="/page/%s">%s</a></td>
<td>%s</td>
<td>%d</td>
<td>%s</td>
<td>%d</td>
</tr>`,
name, name,
2020-08-31 17:52:26 +00:00
util.ShorterPath(data.textPath), data.textType,
util.ShorterPath(data.binaryPath), data.binaryType,
)
}
2020-08-31 17:52:26 +00:00
util.HTTP200Page(w, base("List of pages", templates.PageListHTML(tbody)))
}
// This part is present in all html documents.
2020-08-31 17:52:26 +00:00
var base = templates.BaseHTML
// Reindex all hyphae by checking the wiki storage directory anew.
func handlerReindex(w http.ResponseWriter, rq *http.Request) {
log.Println(rq.URL)
HyphaStorage = make(map[string]*HyphaData)
log.Println("Wiki storage directory is", WikiDir)
log.Println("Start indexing hyphae...")
Index(WikiDir)
log.Println("Indexed", len(HyphaStorage), "hyphae")
}
func main() {
log.Println("Running MycorrhizaWiki β")
2020-06-13 11:18:11 +00:00
var err error
WikiDir, err = filepath.Abs(os.Args[1])
util.WikiDir = WikiDir
if err != nil {
log.Fatal(err)
2020-06-13 11:18:11 +00:00
}
2020-08-19 18:54:23 +00:00
if err := os.Chdir(WikiDir); err != nil {
log.Fatal(err)
}
log.Println("Wiki storage directory is", WikiDir)
log.Println("Start indexing hyphae...")
Index(WikiDir)
log.Println("Indexed", len(HyphaStorage), "hyphae")
2020-08-08 20:10:28 +00:00
history.Start(WikiDir)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(WikiDir+"/static"))))
2020-08-19 18:54:23 +00:00
// See http_readers.go for /page/, /text/, /binary/, /history/.
// See http_mutators.go for /upload-binary/, /upload-text/, /edit/.
http.HandleFunc("/list", handlerList)
http.HandleFunc("/reindex", handlerReindex)
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, rq *http.Request) {
http.ServeFile(w, rq, WikiDir+"/static/favicon.ico")
})
http.HandleFunc("/", func(w http.ResponseWriter, rq *http.Request) {
http.Redirect(w, rq, "/page/home", http.StatusSeeOther)
})
log.Fatal(http.ListenAndServe("0.0.0.0:1737", nil))
2020-06-12 16:22:02 +00:00
}