-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhandlers_page.go
227 lines (195 loc) · 6.14 KB
/
handlers_page.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
package main
import (
"fmt"
"log"
"net/http"
"strings"
)
type PageProvider interface {
GetPage(title string) (*Page, error)
GetPageAt(title, revision string) (*Page, error)
}
type PageExists interface {
PageExists(name string) bool
}
type ContentRenderer interface {
Render([]byte) (string, error)
}
func ViewPageHandler(t *Templates, renderer ContentRenderer, pp PageProvider) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
pageTitle := strings.TrimPrefix(r.URL.Path, "/view/")
revision := r.FormValue("rev")
var page *Page
var err error
if revision == "" {
page, err = pp.GetPage(pageTitle)
} else {
page, err = pp.GetPageAt(pageTitle, revision)
}
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
content, err := renderer.Render(page.Content)
if err != nil {
log.Printf("Failed to render markdown: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
t.RenderPage(w, r, pageTitle, content, &LastModifiedDetails{
User: page.LastModified.User,
Time: page.LastModified.Time,
})
}
}
func EditPageHandler(t *Templates, pp PageProvider) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
pageTitle := strings.TrimPrefix(r.URL.Path, "/edit/")
var content string
if page, err := pp.GetPage(pageTitle); err == nil {
content = string(page.Content)
}
t.RenderEditPage(w, r, pageTitle, content)
}
}
type PageEditor interface {
PutPage(title string, content []byte, user string, message string) error
}
func SubmitPageHandler(pe PageEditor) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
pageTitle := strings.TrimPrefix(request.URL.Path, "/edit/")
content := request.FormValue("content")
message := request.FormValue("message")
username := "Anonymoose"
if user := getUserForRequest(request); user != nil {
username = user.Name
}
if err := pe.PutPage(pageTitle, []byte(content), username, message); err != nil {
// TODO: We should probably send an error to the client
log.Printf("Error saving page: %v\n", err)
} else {
writer.Header().Add("Location", fmt.Sprintf("/view/%s", pageTitle))
writer.WriteHeader(http.StatusSeeOther)
}
}
}
type DeletePageProvider interface {
DeletePage(name string, message string, user string) error
}
func DeletePageConfirmHandler(t *Templates) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
name := strings.TrimPrefix(r.URL.Path, "/delete/")
t.RenderDeletePage(w, r, name)
}
}
func DeletePageHandler(provider DeletePageProvider) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
name := strings.TrimPrefix(request.URL.Path, "/delete/")
confirm := request.FormValue("confirm")
if confirm == "" {
http.Redirect(writer, request, "/delete/"+name, http.StatusSeeOther)
return
}
message := request.FormValue("message")
username := "Anonymoose"
if user := getUserForRequest(request); user != nil {
username = user.Name
}
err := provider.DeletePage(name, message, username)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
return
}
putSessionKey(writer, request, sessionNoticeKey, fmt.Sprintf("Deleted page %s", name))
http.Redirect(writer, request, "/", http.StatusSeeOther)
}
}
type RenamePageProvider interface {
RenamePage(name string, newName string, message string, user string) error
}
func RenamePageConfirmHandler(backend PageExists, t *Templates) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
name := strings.TrimPrefix(r.URL.Path, "/rename/")
if !backend.PageExists(name) {
w.WriteHeader(http.StatusBadRequest)
return
}
t.RenderRenamePage(w, r, name)
}
}
func RenamePageHandler(provider RenamePageProvider) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
name := strings.TrimPrefix(request.URL.Path, "/rename/")
newName := request.FormValue("newName")
if newName == "" {
http.Redirect(writer, request, "/rename/"+name, http.StatusSeeOther)
return
}
message := request.FormValue("message")
username := "Anonymoose"
if user := getUserForRequest(request); user != nil {
username = user.Name
}
err := provider.RenamePage(name, newName, message, username)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
return
}
http.Redirect(writer, request, "/view/"+newName, http.StatusSeeOther)
}
}
type RevertPageProvider interface {
RevertPage(name, revision, user, message string) error
}
func RevertPageConfirmHandler(t *Templates) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
name := strings.TrimPrefix(r.URL.Path, "/revert/")
revision := r.FormValue("rev")
if revision == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
t.RenderRevertPage(w, r, name, revision)
}
}
func RevertPageHandler(provider RevertPageProvider) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
name := strings.TrimPrefix(request.URL.Path, "/revert/")
confirm := request.FormValue("confirm")
if confirm == "" {
http.Redirect(writer, request, "/revert/"+name, http.StatusSeeOther)
return
}
revision := request.FormValue("rev")
if revision == "" {
writer.WriteHeader(http.StatusBadRequest)
return
}
message := request.FormValue("message")
username := "Anonymoose"
if user := getUserForRequest(request); user != nil {
username = user.Name
}
err := provider.RevertPage(name, revision, username, message)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
return
}
putSessionKey(writer, request, sessionNoticeKey, fmt.Sprintf("Reverted page %s", name))
http.Redirect(writer, request, fmt.Sprintf("/view/%s", name), http.StatusSeeOther)
}
}
type PageLister interface {
ListPages() ([]string, error)
}
func ListPagesHandler(t *Templates, pl PageLister) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
pages, err := pl.ListPages()
if err != nil {
log.Printf("Failed to list pages: %v\n", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
t.RenderPageList(w, r, pages)
}
}