1
0
mirror of https://github.com/osmarks/mycorrhiza.git synced 2024-12-13 05:50:27 +00:00
mycorrhiza/main.go
2020-07-04 00:20:56 +05:00

109 lines
2.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"time"
"github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/fs"
"github.com/bouncepaw/mycorrhiza/mycelium"
"github.com/gorilla/mux"
)
// 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".
func RevInMap(m map[string]string) string {
if id, ok := m["rev"]; ok {
return id
}
return "0"
}
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)
}
func main() {
if len(os.Args) == 1 {
panic("Expected a root wiki pages directory")
}
wikiDir, err := filepath.Abs(os.Args[1])
if err != nil {
panic(err)
}
log.Println("Welcome to MycorrhizaWiki α")
cfg.InitConfig(wikiDir)
log.Println("Indexing hyphae...")
mycelium.Init()
fs.InitStorage()
// Start server code. See handlers.go for handlers' implementations.
r := mux.NewRouter()
r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, rq *http.Request) {
http.ServeFile(w, rq, filepath.Join(filepath.Dir(cfg.WikiDir), "favicon.ico"))
})
IdempotentRouterBoiler(r, "binary", HandlerBinary)
IdempotentRouterBoiler(r, "raw", HandlerRaw)
IdempotentRouterBoiler(r, "zen", HandlerZen)
IdempotentRouterBoiler(r, "view", HandlerView)
r.Queries("action", "edit").Path(cfg.MyceliumUrl + cfg.HyphaUrl).
HandlerFunc(HandlerEdit)
r.Queries("action", "edit").Path(cfg.HyphaUrl).
HandlerFunc(HandlerEdit)
r.Queries("action", "update").Path(cfg.MyceliumUrl + cfg.HyphaUrl).
Methods("POST").HandlerFunc(HandlerUpdate)
r.Queries("action", "update").Path(cfg.HyphaUrl).
Methods("POST").HandlerFunc(HandlerUpdate)
r.HandleFunc(cfg.MyceliumUrl+cfg.HyphaUrl, HandlerView)
r.HandleFunc(cfg.HyphaUrl, HandlerView)
// 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) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, `
<p>Check out <a href="/Fruit">Fruit</a> maybe.</p>
<p><strong>TODO:</strong> make this page usable</p>
`)
})
http.Handle("/", r)
srv := &http.Server{
Handler: r,
Addr: cfg.Address,
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Fatal(srv.ListenAndServe())
}