-
Notifications
You must be signed in to change notification settings - Fork 3
/
wiki.go
199 lines (182 loc) · 6.07 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
package main
import (
"flag"
"fmt"
mmd "github.com/SimonWaldherr/micromarkdownGo"
sqlite "github.com/mxk/go-sqlite/sqlite3"
"io"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"strings"
"text/template"
"time"
)
var (
templates = template.Must(template.ParseFiles("edit.html", "view.html", "user.html", "search.html", "index.html"))
validPath = regexp.MustCompile("^/(edit|save|view|user|search)/([a-zA-Z0-9_()]+)$")
sql, _ = sqlite.Open("./wiki.sqlite3")
)
type Page struct {
Title string
Body string
}
type PagesQuery struct {
Id int
Name string
Content string
Timestamp int
IP string
}
func (p *Page) saveSource(r *http.Request) error {
return sql.Exec("INSERT INTO wiki (name, content, timestamp, ip) VALUES('" + p.Title + "', '" + strings.Replace(p.Body, "'", "''", -1) + "', '" + strconv.FormatInt(time.Now().Unix(), 10) + "', '" + strings.Split(r.RemoteAddr, ":")[0] + "');")
}
func (p *Page) saveCache() error {
filename := "cache/" + p.Title + ".html"
return ioutil.WriteFile(filename, []byte(p.Body), 0600)
}
func loadSource(title string) (*Page, error) {
var qu PagesQuery
q, err := sql.Query("SELECT * FROM wiki WHERE name = '" + title + "' ORDER BY timestamp DESC LIMIT 1;")
if err != nil {
return &Page{Title: title, Body: ""}, nil
} else {
err = q.Scan(&qu.Id, &qu.Name, &qu.Content, &qu.Timestamp)
if err != nil {
fmt.Printf("Error while getting row data: %s\n", err)
}
return &Page{Title: qu.Name, Body: qu.Content}, nil
}
}
func loadCache(title string) (*Page, error) {
filename := "cache/" + title + ".html"
body, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
return &Page{Title: title, Body: string(body)}, nil
}
func renderTemplate(w http.ResponseWriter, tmpl string, p *Page) {
err := templates.ExecuteTemplate(w, tmpl+".html", p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func viewHandler(w http.ResponseWriter, r *http.Request, title string) {
w.Header().Set("Access-Control-Allow-Methods", "GET")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
p, err := loadCache(title)
if err != nil {
http.Redirect(w, r, "/edit/"+title, http.StatusFound)
return
}
renderTemplate(w, "view", p)
}
func userHandler(w http.ResponseWriter, r *http.Request, title string) {
p := &Page{Title: "", Body: ""}
renderTemplate(w, "user", p)
}
func searchHandler(w http.ResponseWriter, r *http.Request, title string) {
var qu PagesQuery
q, _ := sql.Query("SELECT id, name, content, timestamp FROM wiki WHERE name like '%" + title + "%' GROUP BY name ORDER BY timestamp DESC;")
retstr := ""
ioeof := false
for ioeof != true {
q.Scan(&qu.Id, &qu.Name, &qu.Content, &qu.Timestamp)
retstr += "<li><a href=\"/view/" + qu.Name + "\">" + qu.Name + "</a></li>"
if q.Next() == io.EOF {
ioeof = true
}
}
retstr = "<ul>" + retstr + "</ul>"
renderTemplate(w, "search", &Page{Title: title, Body: retstr})
}
func editHandler(w http.ResponseWriter, r *http.Request, title string) {
w.Header().Set("Access-Control-Allow-Methods", "GET")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
p, _ := loadSource(title)
renderTemplate(w, "edit", p)
}
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
w.Header().Set("Access-Control-Allow-Methods", "POST")
w.Header().Set("Content-Type", "text/html; charset=utf-8")
r.ParseForm()
body := r.PostFormValue("body")
body = strings.Replace(body, "\r\n", "\n", -1)
p := &Page{Title: title, Body: body}
err := p.saveSource(r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Printf("1 Error while getting row data: %s\n", err)
return
}
p = &Page{Title: title, Body: mmd.Micromarkdown(body)}
err = p.saveCache()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
fmt.Printf("2 Error while getting row data: %s\n", err)
return
}
http.Redirect(w, r, "/view/"+title, http.StatusFound)
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
var qu PagesQuery
q, _ := sql.Query("SELECT id, name, content, timestamp FROM wiki GROUP BY name ORDER BY timestamp DESC;")
retstr := ""
ioeof := false
for ioeof != true {
q.Scan(&qu.Id, &qu.Name, &qu.Content, &qu.Timestamp)
retstr += "<li><a href=\"./view/" + qu.Name + "\">" + qu.Name + "</a></li>"
if q.Next() == io.EOF {
ioeof = true
}
}
retstr = "<ul>" + retstr + "</ul>"
renderTemplate(w, "index", &Page{Title: qu.Name, Body: retstr})
}
func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
m := validPath.FindStringSubmatch(r.URL.Path)
fmt.Println(strconv.FormatInt(time.Now().Unix(), 10) + ": " + strings.Split(r.RemoteAddr, ":")[0] + " - " + r.URL.Path)
if m == nil && r.URL.Path != "/" {
http.NotFound(w, r)
return
} else if r.URL.Path == "/" {
var qu PagesQuery
q, _ := sql.Query("SELECT id, name, content, timestamp FROM wiki GROUP BY name ORDER BY timestamp DESC;")
q.Scan(&qu.Id, &qu.Name, &qu.Content, &qu.Timestamp)
retstr := ""
for q.Next() != io.EOF {
retstr += "<li><a href=\"./view/" + qu.Name + "\">" + qu.Name + "</a></li>"
q.Scan(&qu.Id, &qu.Name, &qu.Content, &qu.Timestamp)
}
retstr += "<li><a href=\"./view/" + qu.Name + "\">" + qu.Name + "</a></li>"
retstr = "<ul>" + retstr + "</ul>"
renderTemplate(w, "index", &Page{Title: qu.Name, Body: retstr})
return
}
fn(w, r, m[2])
}
}
func main() {
flag.Parse()
port := ":" + flag.Arg(0)
if port == ":" {
port = ":8080"
}
server := &http.Server{
Addr: port,
ReadTimeout: 20 * time.Second,
WriteTimeout: 20 * time.Second,
MaxHeaderBytes: 1 << 20,
}
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
http.HandleFunc("/view/", makeHandler(viewHandler))
http.HandleFunc("/edit/", makeHandler(editHandler))
http.HandleFunc("/save/", makeHandler(saveHandler))
http.HandleFunc("/user/", makeHandler(userHandler))
http.HandleFunc("/search/", makeHandler(searchHandler))
http.HandleFunc("/", makeHandler(viewHandler))
server.ListenAndServe()
}