forked from bouncepaw/mycorrhiza
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
133 lines (110 loc) · 3.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
"os"
"path/filepath"
// "strconv"
"time"
)
func GetRevision(hyphae map[string]*Hypha, hyphaName string, rev string, w http.ResponseWriter) (Revision, bool) {
log.Println("Getting hypha", hyphaName, rev)
for name, hypha := range hyphae {
if name == hyphaName {
if rev == "0" {
rev = hypha.NewestRevision()
}
for id, r := range hypha.Revisions {
if rev == id {
return *r, true
}
}
}
}
return Revision{}, false
}
func RevInMap(m map[string]string) string {
if val, ok := m["rev"]; ok {
return val
}
return "0"
}
var rootWikiDir string
var hyphae map[string]*Hypha
func hyphaeAsMap(hyphae []*Hypha) map[string]*Hypha {
mh := make(map[string]*Hypha)
for _, h := range hyphae {
mh[h.Name()] = h
}
return mh
}
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)
}
log.Println("Welcome to MycorrhizaWiki α")
log.Println("Indexing hyphae...")
hyphae = recurFindHyphae(rootWikiDir)
log.Println("Indexed", len(hyphae), "hyphae. Ready to accept requests.")
// setRelations(hyphae)
// Start server code
r := mux.NewRouter()
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)
r.Queries("action", "update").Path(hyphaUrl).
HandlerFunc(HandlerUpdate)
r.HandleFunc(hyphaUrl, HandlerView)
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
for _, v := range hyphae {
log.Println("Rendering latest revision of hypha", v.Name())
html, err := v.AsHtml(hyphae, "0")
if err != nil {
fmt.Fprintln(w, err)
}
fmt.Fprintln(w, html)
}
})
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())
}