-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
246 lines (220 loc) · 6 KB
/
handlers.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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/gorilla/mux"
)
func rootHandler(w http.ResponseWriter, r *http.Request) {
if _, err := os.Stat(filepath.Join(cfg.RepositoryRoot, "Home")); os.IsNotExist(err) {
fmt.Println(err)
http.Redirect(w, r, "/create/Home", http.StatusFound)
return
}
http.Redirect(w, r, "/Home", http.StatusFound)
}
func newPageHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
splat := strings.Split(vars["page"], "/")
if stat, _ := os.Stat(filepath.Join(cfg.RepositoryRoot, vars["page"])); stat != nil {
http.Redirect(w, r, fmt.Sprintf("/%s", vars["page"]), http.StatusSeeOther)
return
}
data := struct {
FileName string
Dir string
AbsolutePath string
}{
splat[len(splat)-1],
fmt.Sprintf("/%s", strings.Join(splat[:len(splat)-1], "/")),
vars["page"],
}
renderer.HTML(w, http.StatusOK, "new_page", data)
}
func createPageHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
vars := mux.Vars(r)
p := NewHomePage(vars["page"], r.PostFormValue("content"), r.PostFormValue("comment"))
err = p.Save(cfg)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, fmt.Sprintf("/%s", vars["page"]), http.StatusFound)
}
func detailHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
splat := strings.Split(vars["page"], "/")
commitHash := splat[len(splat)-1]
// check if the commit hash exist against the file name
content, history, err := getContentByHash(strings.Join(splat[:len(splat)-1], "/"), commitHash)
if err != nil {
// if commit doesn't exist, check if the file exist against the path.
b, err := ioutil.ReadFile(filepath.Join(cfg.RepositoryRoot, fmt.Sprintf("%s", titleToFileName(vars["page"])))) // just pass the file name
if err != nil {
// if file doesn't exist, create a file with the given path
http.Redirect(w, r, fmt.Sprintf("/create/%s", vars["page"]), http.StatusFound)
return
}
p := NewHomePage(vars["page"], "", "")
histories, err := getGitHistory(p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := struct {
AbsolutePath string
Body string
LastCommit *History
}{
vars["page"],
string(b),
histories[0],
}
renderer.HTML(w, http.StatusOK, "show_page", data)
return
}
data := struct {
AbsolutePath string
Body string
LastCommit *History
}{
strings.Join(splat[:len(splat)-1], "/"),
content,
history,
}
renderer.HTML(w, http.StatusOK, "show_page", data)
}
func editPageHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
vars := mux.Vars(r)
b, err := ioutil.ReadFile(filepath.Join(cfg.RepositoryRoot, fmt.Sprintf("%s", titleToFileName(vars["page"])))) // just pass the file name
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := struct {
FileName string
AbsolutePath string
Body string
}{
strings.Split(vars["page"], "/")[len(strings.Split(vars["page"], "/"))-1],
vars["page"],
string(b),
}
renderer.HTML(w, http.StatusOK, "edit_page", data)
}
func updatePageHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
vars := mux.Vars(r)
p := NewHomePage(vars["page"], r.PostFormValue("content"), r.PostFormValue("comment"))
err = p.Save(cfg)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, fmt.Sprintf("/%s", vars["page"]), http.StatusFound)
}
func historyPageHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
vars := mux.Vars(r)
p := NewHomePage(vars["page"], "", "")
histories, err := getGitHistory(p)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
data := struct {
FileName string
AbsolutePath string
Histories []*History
}{
strings.Split(vars["page"], "/")[len(strings.Split(vars["page"], "/"))-1],
vars["page"],
histories,
}
renderer.HTML(w, http.StatusOK, "history_page", data)
}
func latestChangesHandler(w http.ResponseWriter, r *http.Request) {
logs, err := getGitLog()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
renderer.HTML(w, http.StatusOK, "lastest_changes", logs)
}
func pagesHandler(w http.ResponseWriter, r *http.Request) {
pages, err := getPages()
if err != nil {
rootHandler(w, r)
return
}
data := struct {
Pages []*GitPage
Path string
}{
pages,
"",
}
renderer.HTML(w, http.StatusOK, "all_pages", data)
}
func pageHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
vars := mux.Vars(r)
pages, err := getPage(vars["page"])
if err != nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
data := struct {
Pages []*GitPage
Path string
}{
pages,
"/" + vars["page"],
}
renderer.HTML(w, http.StatusOK, "all_pages", data)
}
func deleteHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
vars := mux.Vars(r)
// err = os.Remove(filepath.Join(cfg.RepositoryRoot, fmt.Sprintf("%s.md", vars["page"])))
// if err != nil {
// http.Redirect(w, r, "/", http.StatusFound)
// return
// }
p := NewHomePage(vars["page"], r.PostFormValue("content"), fmt.Sprintf("Destroyed %s", strings.Split(vars["page"], "/")[len(strings.Split(vars["page"], "/"))-1]))
err = gitCommit(p, true)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/", http.StatusFound)
}