/
wiki.go
274 lines (219 loc) · 5.2 KB
/
wiki.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package main
import "os"
import "strings"
import "io/ioutil"
import "http"
import "template"
import "regexp"
import "gob"
import "fmt"
import "bytes"
type Page struct{
Title string
Body []byte
}
type taxonomy map[string]string
// modify as per needs
const (
extenstion = ".txt"
permissions = 0600
port = ":8119"
templateEdit = "edit.html"
templateView = "view.html"
templateTag = "tag.html"
tagsFile = "tags.bin"
errorInvalidPageTitle = "Invalid page title"
filePath = "./data/"
templatePath = "./tmpl/"
tagsPath = "./tags"
homePage = "homePage"
//function handlers
view = "/view/"
edit = "/edit/"
save = "/save/"
tag = "/tag/"
lenViewPath = len(view)
lenEditPath = len(edit)
lenSavePath = len(save)
lenTagPath = len(tag)
)
//modify as per needs
var titleValidator = regexp.MustCompile("^[a-zA-Z0-9]+$")
func Store (t taxonomy, fname string) os.Error {
b := new(bytes.Buffer)
enc := gob.NewEncoder(b)
err := enc.Encode(t)
if err != nil {
return err
}
fh, eopen := os.OpenFile(fname, os.O_CREATE|os.O_WRONLY, 0666)
defer fh.Close()
if eopen != nil {
return eopen
}
_,e := fh.Write(b.Bytes())
if e != nil {
return e
}
return nil
}
func Load (fname string) ( taxonomy, os.Error) {
fh, err := os.Open(fname)
if err != nil {
return nil, err
}
t := make(map[string]string)
dec := gob.NewDecoder(fh)
err = dec.Decode(&t)
if err != nil {
return nil, err
}
return t, nil
}
func Add (m taxonomy,str1 string, str2 string) {
for i := 0; i < len(m); i++ {
_, ok := m[str1]
switch {
case ok:
m[str1] = m[str1] + "," + str2
return
default:
m[str1] = str2
return
}
}
}
func (p *Page) save() os.Error {
filename := filePath + p.Title + extenstion
return ioutil.WriteFile(filename,p.Body,permissions)
}
func loadPage(Title string) (*Page, os.Error){
Body,err := ioutil.ReadFile(filePath + Title+extenstion)
if err != nil{
return nil,err
}
return &Page{Title : Title,Body : Body}, nil
}
func getTitle(w http.ResponseWriter, r *http.Request) (title string, err os.Error) {
switch {
case strings.Contains(r.URL.Path,view):
title = r.URL.Path[lenViewPath:]
case strings.Contains(r.URL.Path,edit):
title = r.URL.Path[lenEditPath:]
case strings.Contains(r.URL.Path,save):
title = r.URL.Path[lenSavePath:]
case strings.Contains(r.URL.Path,tag):
title = "dontcare"
}
if !titleValidator.MatchString(title) {
http.NotFound(w, r)
err = os.NewError(errorInvalidPageTitle)
}
return
}
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
t, err := template.ParseFile(templatePath + tmpl, nil)
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
err = t.Execute(w, p)
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
}
}
func viewHandler(w http.ResponseWriter, r *http.Request) {
title, err := getTitle(w, r)
if err != nil {
return
}
p, err := loadPage(title)
if err != nil {
http.Redirect(w, r, edit+title, http.StatusFound)
return
}
renderTemplate(w, templateView, p)
}
func getMatches(m taxonomy,key string) string {
for k, _ := range m {
if k == key{
return m[k]
}
}
return ""
}
func tagHandler(w http.ResponseWriter, r *http.Request) {
tags := r.FormValue("tags")
fmt.Println(tags)
_,err2 := Load(tagsPath+tagsFile)
if err2!=nil{
return
}
//fmt.Fprintf(w, "<h1>Matched</h1><div>%s</div>", getMatches(m,tags))
t, err := template.ParseFile(templatePath + templateTag, nil)
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
err = t.Execute(w, nil)
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
}
}
func editHandler(w http.ResponseWriter, r *http.Request) {
title, err := getTitle(w, r)
if err != nil {
return
}
p, err := loadPage(title)
if err != nil {
p = &Page{Title: title}
}
renderTemplate(w, templateEdit, p)
}
func saveHandler(w http.ResponseWriter, r *http.Request) {
title, err := getTitle(w, r)
if err != nil {
return
}
body := r.FormValue("body")
tags := r.FormValue("tags")
m,err2 := Load(tagsPath+tagsFile)
if err2!=nil{
return
}
splitTokens := strings.Split(tags," ",-1)
for i:=0;i<len(splitTokens);i++ {
Add(m,splitTokens[i],title)
}
err2 = Store(m,tagsPath+tagsFile)
if err2!=nil{
return
}
p := &Page{Title: title, Body: []byte(body)}
err = p.save()
if err != nil {
http.Error(w, err.String(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, view+title, http.StatusFound)
}
func baseHandler(w http.ResponseWriter, r *http.Request) {
t := homePage
p, err := loadPage(t)
if err != nil {
http.Redirect(w, r, edit+t, http.StatusFound)
return
}
renderTemplate(w, templateView, p)
}
func main() {
http.HandleFunc("/", baseHandler)
http.HandleFunc(view, viewHandler)
http.HandleFunc(save, saveHandler)
http.HandleFunc(edit, editHandler)
http.HandleFunc(tag, tagHandler)
http.ListenAndServe(port, nil)
//get map loaded
Load(tagsPath+tagsFile)
}