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"
|
2020-06-13 11:18:11 +00:00
|
|
|
|
"time"
|
2020-06-19 14:30:19 +00:00
|
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2020-06-12 16:22:02 +00:00
|
|
|
|
)
|
|
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
|
// GetRevision finds revision with id `id` of `hyphaName` in `hyphae`.
|
|
|
|
|
// If `id` is `"0"`, it means the last revision.
|
|
|
|
|
// If no such revision is found, last return value is false.
|
|
|
|
|
func GetRevision(hyphaName string, id string) (Revision, bool) {
|
|
|
|
|
log.Println("Getting hypha", hyphaName, id)
|
|
|
|
|
if hypha, ok := hyphae[hyphaName]; ok {
|
|
|
|
|
if id == "0" {
|
|
|
|
|
id = hypha.NewestRevision()
|
|
|
|
|
}
|
|
|
|
|
if rev, ok := hypha.Revisions[id]; ok {
|
|
|
|
|
return *rev, true
|
2020-06-16 18:35:52 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Revision{}, false
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
|
// RevInMap finds value of `rev` (the one from URL queries like) in the passed map that is usually got from `mux.Vars(*http.Request)`.
|
|
|
|
|
// If there is no `rev`, return "0".
|
2020-06-14 08:12:22 +00:00
|
|
|
|
func RevInMap(m map[string]string) string {
|
2020-06-19 18:11:47 +00:00
|
|
|
|
if id, ok := m["rev"]; ok {
|
|
|
|
|
return id
|
2020-06-14 08:12:22 +00:00
|
|
|
|
}
|
|
|
|
|
return "0"
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
|
// `rootWikiDir` is a directory where all wiki files reside.
|
2020-06-12 16:22:02 +00:00
|
|
|
|
var rootWikiDir string
|
|
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
|
// `hyphae` is a map with all hyphae. Many functions use it.
|
|
|
|
|
var hyphae map[string]*Hypha
|
2020-06-12 16:22:02 +00:00
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
if len(os.Args) == 1 {
|
|
|
|
|
panic("Expected a root wiki pages directory")
|
|
|
|
|
}
|
|
|
|
|
// Required so the rootWikiDir hereinbefore does not get redefined.
|
|
|
|
|
var err error
|
|
|
|
|
rootWikiDir, err = filepath.Abs(os.Args[1])
|
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 09:40:51 +00:00
|
|
|
|
log.Println("Welcome to MycorrhizaWiki α")
|
|
|
|
|
log.Println("Indexing hyphae...")
|
2020-06-16 18:35:52 +00:00
|
|
|
|
hyphae = recurFindHyphae(rootWikiDir)
|
2020-06-17 09:40:51 +00:00
|
|
|
|
log.Println("Indexed", len(hyphae), "hyphae. Ready to accept requests.")
|
2020-06-12 16:22:02 +00:00
|
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
|
// Start server code. See handlers.go for handlers' implementations.
|
2020-06-13 11:18:11 +00:00
|
|
|
|
r := mux.NewRouter()
|
2020-06-14 08:12:22 +00:00
|
|
|
|
|
|
|
|
|
r.Queries("action", "getBinary", "rev", revQuery).Path(hyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerGetBinary)
|
|
|
|
|
r.Queries("action", "getBinary").Path(hyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerGetBinary)
|
|
|
|
|
|
|
|
|
|
r.Queries("action", "raw", "rev", revQuery).Path(hyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerRaw)
|
|
|
|
|
r.Queries("action", "raw").Path(hyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerRaw)
|
|
|
|
|
|
|
|
|
|
r.Queries("action", "zen", "rev", revQuery).Path(hyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerZen)
|
|
|
|
|
r.Queries("action", "zen").Path(hyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerZen)
|
|
|
|
|
|
|
|
|
|
r.Queries("action", "view", "rev", revQuery).Path(hyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerView)
|
|
|
|
|
r.Queries("action", "view").Path(hyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerView)
|
|
|
|
|
|
|
|
|
|
r.Queries("action", "history").Path(hyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerHistory)
|
|
|
|
|
|
|
|
|
|
r.Queries("action", "edit").Path(hyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerEdit)
|
|
|
|
|
|
|
|
|
|
r.Queries("action", "rewind", "rev", revQuery).Path(hyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerRewind)
|
|
|
|
|
|
|
|
|
|
r.Queries("action", "delete").Path(hyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerDelete)
|
|
|
|
|
|
|
|
|
|
r.Queries("action", "rename", "to", hyphaPattern).Path(hyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerRename)
|
|
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
|
r.Queries("action", "update").Path(hyphaUrl).Methods("POST").
|
2020-06-14 08:12:22 +00:00
|
|
|
|
HandlerFunc(HandlerUpdate)
|
|
|
|
|
|
|
|
|
|
r.HandleFunc(hyphaUrl, HandlerView)
|
2020-06-13 16:42:43 +00:00
|
|
|
|
|
2020-06-19 18:11:47 +00:00
|
|
|
|
// Debug page that renders all hyphae.
|
|
|
|
|
// TODO: make it redirect to home page.
|
|
|
|
|
// TODO: make a home page.
|
|
|
|
|
r.HandleFunc("/", func(w http.ResponseWriter, rq *http.Request) {
|
2020-06-13 11:18:11 +00:00
|
|
|
|
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2020-06-19 18:11:47 +00:00
|
|
|
|
for _, h := range hyphae {
|
|
|
|
|
log.Println("Rendering latest revision of hypha", h.FullName)
|
|
|
|
|
html, err := h.AsHtml("0", w)
|
2020-06-13 11:18:11 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintln(w, err)
|
|
|
|
|
}
|
|
|
|
|
fmt.Fprintln(w, html)
|
|
|
|
|
}
|
|
|
|
|
})
|
2020-06-14 08:12:22 +00:00
|
|
|
|
|
2020-06-13 11:18:11 +00:00
|
|
|
|
http.Handle("/", r)
|
|
|
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
|
Handler: r,
|
|
|
|
|
Addr: "127.0.0.1:8000",
|
|
|
|
|
// Good practice: enforce timeouts for servers you create!
|
|
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Fatal(srv.ListenAndServe())
|
2020-06-12 16:22:02 +00:00
|
|
|
|
}
|