2020-06-12 21:22:02 +05:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2020-06-13 16:18:11 +05:00
|
|
|
|
"log"
|
|
|
|
|
|
"net/http"
|
2020-06-12 21:22:02 +05:00
|
|
|
|
"os"
|
|
|
|
|
|
"path/filepath"
|
2020-06-13 16:18:11 +05:00
|
|
|
|
"time"
|
2020-06-19 19:30:19 +05:00
|
|
|
|
|
2020-06-23 22:53:05 +05:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
2020-06-24 19:01:51 +05:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/fs"
|
2020-07-04 00:20:56 +05:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/mycelium"
|
2020-06-19 19:30:19 +05:00
|
|
|
|
"github.com/gorilla/mux"
|
2020-06-12 21:22:02 +05:00
|
|
|
|
)
|
|
|
|
|
|
|
2020-06-19 23:11:47 +05: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 13:12:22 +05:00
|
|
|
|
func RevInMap(m map[string]string) string {
|
2020-06-19 23:11:47 +05:00
|
|
|
|
if id, ok := m["rev"]; ok {
|
|
|
|
|
|
return id
|
2020-06-14 13:12:22 +05:00
|
|
|
|
}
|
|
|
|
|
|
return "0"
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-03 00:03:30 +05:00
|
|
|
|
func IdempotentRouterBoiler(router *mux.Router, action string, handler func(w http.ResponseWriter, rq *http.Request)) {
|
|
|
|
|
|
router.
|
|
|
|
|
|
Queries("action", action, "rev", cfg.RevQuery).
|
|
|
|
|
|
Path(cfg.MyceliumUrl + cfg.HyphaUrl).
|
|
|
|
|
|
HandlerFunc(handler)
|
|
|
|
|
|
router.
|
|
|
|
|
|
Queries("action", action).
|
|
|
|
|
|
Path(cfg.MyceliumUrl + cfg.HyphaUrl).
|
|
|
|
|
|
HandlerFunc(handler)
|
|
|
|
|
|
router.
|
|
|
|
|
|
Queries("action", action, "rev", cfg.RevQuery).
|
|
|
|
|
|
Path(cfg.HyphaUrl).
|
|
|
|
|
|
HandlerFunc(handler)
|
|
|
|
|
|
router.
|
|
|
|
|
|
Queries("action", action).
|
|
|
|
|
|
Path(cfg.HyphaUrl).
|
|
|
|
|
|
HandlerFunc(handler)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-12 21:22:02 +05:00
|
|
|
|
func main() {
|
|
|
|
|
|
if len(os.Args) == 1 {
|
|
|
|
|
|
panic("Expected a root wiki pages directory")
|
|
|
|
|
|
}
|
2020-06-23 22:53:05 +05:00
|
|
|
|
wikiDir, err := filepath.Abs(os.Args[1])
|
2020-06-12 21:22:02 +05:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
panic(err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-17 14:40:51 +05:00
|
|
|
|
log.Println("Welcome to MycorrhizaWiki α")
|
2020-06-23 22:53:05 +05:00
|
|
|
|
cfg.InitConfig(wikiDir)
|
2020-06-17 14:40:51 +05:00
|
|
|
|
log.Println("Indexing hyphae...")
|
2020-07-04 00:20:56 +05:00
|
|
|
|
mycelium.Init()
|
2020-06-25 22:18:50 +05:00
|
|
|
|
fs.InitStorage()
|
2020-06-12 21:22:02 +05:00
|
|
|
|
|
2020-06-19 23:11:47 +05:00
|
|
|
|
// Start server code. See handlers.go for handlers' implementations.
|
2020-06-13 16:18:11 +05:00
|
|
|
|
r := mux.NewRouter()
|
2020-06-14 13:12:22 +05:00
|
|
|
|
|
2020-06-25 22:18:50 +05:00
|
|
|
|
r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, rq *http.Request) {
|
2020-07-18 22:23:56 +05:00
|
|
|
|
http.ServeFile(w, rq, filepath.Join(cfg.WikiDir, "favicon.ico"))
|
2020-06-25 22:18:50 +05:00
|
|
|
|
})
|
|
|
|
|
|
|
2020-07-03 00:03:30 +05:00
|
|
|
|
IdempotentRouterBoiler(r, "binary", HandlerBinary)
|
|
|
|
|
|
IdempotentRouterBoiler(r, "raw", HandlerRaw)
|
|
|
|
|
|
IdempotentRouterBoiler(r, "zen", HandlerZen)
|
|
|
|
|
|
IdempotentRouterBoiler(r, "view", HandlerView)
|
2020-06-14 13:12:22 +05:00
|
|
|
|
|
2020-07-03 00:03:30 +05:00
|
|
|
|
r.Queries("action", "edit").Path(cfg.MyceliumUrl + cfg.HyphaUrl).
|
|
|
|
|
|
HandlerFunc(HandlerEdit)
|
2020-06-26 01:31:58 +05:00
|
|
|
|
r.Queries("action", "edit").Path(cfg.HyphaUrl).
|
|
|
|
|
|
HandlerFunc(HandlerEdit)
|
2020-06-14 13:12:22 +05:00
|
|
|
|
|
2020-07-03 00:03:30 +05:00
|
|
|
|
r.Queries("action", "update").Path(cfg.MyceliumUrl + cfg.HyphaUrl).
|
|
|
|
|
|
Methods("POST").HandlerFunc(HandlerUpdate)
|
|
|
|
|
|
r.Queries("action", "update").Path(cfg.HyphaUrl).
|
|
|
|
|
|
Methods("POST").HandlerFunc(HandlerUpdate)
|
2020-06-14 13:12:22 +05:00
|
|
|
|
|
2020-07-04 00:20:56 +05:00
|
|
|
|
r.HandleFunc(cfg.MyceliumUrl+cfg.HyphaUrl, HandlerView)
|
2020-06-25 22:18:50 +05:00
|
|
|
|
r.HandleFunc(cfg.HyphaUrl, HandlerView)
|
2020-06-13 21:42:43 +05:00
|
|
|
|
|
2020-06-19 23:11:47 +05:00
|
|
|
|
r.HandleFunc("/", func(w http.ResponseWriter, rq *http.Request) {
|
2020-07-17 20:18:03 +05:00
|
|
|
|
http.Redirect(w, rq, cfg.HomePage, http.StatusSeeOther)
|
2020-06-13 16:18:11 +05:00
|
|
|
|
})
|
2020-06-14 13:12:22 +05:00
|
|
|
|
|
2020-06-13 16:18:11 +05:00
|
|
|
|
http.Handle("/", r)
|
|
|
|
|
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
|
|
Handler: r,
|
2020-06-23 22:53:05 +05:00
|
|
|
|
Addr: cfg.Address,
|
2020-06-13 16:18:11 +05:00
|
|
|
|
// Good practice: enforce timeouts for servers you create!
|
|
|
|
|
|
WriteTimeout: 15 * time.Second,
|
|
|
|
|
|
ReadTimeout: 15 * time.Second,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Fatal(srv.ListenAndServe())
|
2020-06-12 21:22:02 +05:00
|
|
|
|
}
|