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
|
|
|
|
|
2020-06-23 17:53:05 +00:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/cfg"
|
2020-06-24 14:01:51 +00:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/fs"
|
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
|
|
|
|
// 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-12 16:22:02 +00:00
|
|
|
|
func main() {
|
|
|
|
|
if len(os.Args) == 1 {
|
|
|
|
|
panic("Expected a root wiki pages directory")
|
|
|
|
|
}
|
2020-06-23 17:53:05 +00:00
|
|
|
|
wikiDir, err := filepath.Abs(os.Args[1])
|
2020-06-12 16:22:02 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 09:40:51 +00:00
|
|
|
|
log.Println("Welcome to MycorrhizaWiki α")
|
2020-06-23 17:53:05 +00:00
|
|
|
|
cfg.InitConfig(wikiDir)
|
2020-06-17 09:40:51 +00:00
|
|
|
|
log.Println("Indexing hyphae...")
|
2020-06-25 17:18:50 +00:00
|
|
|
|
fs.InitStorage()
|
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
|
|
|
|
|
2020-06-25 17:18:50 +00:00
|
|
|
|
r.HandleFunc("/favicon.ico", func(w http.ResponseWriter, rq *http.Request) {
|
|
|
|
|
http.ServeFile(w, rq, filepath.Join(filepath.Dir(cfg.WikiDir), "favicon.ico"))
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
r.Queries("action", "binary", "rev", cfg.RevQuery).Path(cfg.HyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerBinary)
|
|
|
|
|
r.Queries("action", "binary").Path(cfg.HyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerBinary)
|
2020-06-14 08:12:22 +00:00
|
|
|
|
|
2020-06-24 14:01:51 +00:00
|
|
|
|
r.Queries("action", "raw", "rev", cfg.RevQuery).Path(cfg.HyphaUrl).
|
2020-06-14 08:12:22 +00:00
|
|
|
|
HandlerFunc(HandlerRaw)
|
2020-06-24 14:01:51 +00:00
|
|
|
|
r.Queries("action", "raw").Path(cfg.HyphaUrl).
|
2020-06-14 08:12:22 +00:00
|
|
|
|
HandlerFunc(HandlerRaw)
|
|
|
|
|
|
2020-06-24 16:19:44 +00:00
|
|
|
|
r.Queries("action", "zen", "rev", cfg.RevQuery).Path(cfg.HyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerZen)
|
|
|
|
|
r.Queries("action", "zen").Path(cfg.HyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerZen)
|
|
|
|
|
|
2020-06-25 17:18:50 +00:00
|
|
|
|
r.Queries("action", "view", "rev", cfg.RevQuery).Path(cfg.HyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerView)
|
|
|
|
|
r.Queries("action", "view").Path(cfg.HyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerView)
|
2020-06-14 08:12:22 +00:00
|
|
|
|
|
2020-06-25 17:18:50 +00:00
|
|
|
|
/*
|
2020-06-24 16:19:44 +00:00
|
|
|
|
r.Queries("action", "edit").Path(hyphaUrl).
|
|
|
|
|
HandlerFunc(HandlerEdit)
|
2020-06-14 08:12:22 +00:00
|
|
|
|
|
2020-06-24 14:01:51 +00:00
|
|
|
|
r.Queries("action", "update").Path(hyphaUrl).Methods("POST").
|
|
|
|
|
HandlerFunc(HandlerUpdate)
|
|
|
|
|
*/
|
2020-06-14 08:12:22 +00:00
|
|
|
|
|
2020-06-25 17:18:50 +00:00
|
|
|
|
r.HandleFunc(cfg.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-24 14:01:51 +00:00
|
|
|
|
fmt.Fprintln(w, `
|
|
|
|
|
<p>Check out <a href="/Fruit">Fruit</a> maybe.</p>
|
|
|
|
|
<p><strong>TODO:</strong> make this page usable</p>
|
|
|
|
|
`)
|
2020-06-13 11:18:11 +00:00
|
|
|
|
})
|
2020-06-14 08:12:22 +00:00
|
|
|
|
|
2020-06-13 11:18:11 +00:00
|
|
|
|
http.Handle("/", r)
|
|
|
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
|
Handler: r,
|
2020-06-23 17:53:05 +00:00
|
|
|
|
Addr: cfg.Address,
|
2020-06-13 11:18:11 +00:00
|
|
|
|
// 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
|
|
|
|
}
|