-
Notifications
You must be signed in to change notification settings - Fork 0
/
ajax.go
163 lines (143 loc) · 4.79 KB
/
ajax.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
"strings"
)
// AjaxRouteAuthOptional represents an AJAX handler where authentication is optional,
// that returns a response object to be sent as JSON, and a status code.
type AjaxRouteAuthOptional func(
db *sql.DB,
userID *uint,
w http.ResponseWriter,
r *http.Request,
) (interface{}, int)
// AjaxRouteAuthRequired represents an AJAX handler where authenticaition is mandatory,
// that returns a response object to be sent as JSON, and a status code.
type AjaxRouteAuthRequired func(
db *sql.DB,
userID uint,
w http.ResponseWriter,
r *http.Request,
) (interface{}, int)
var ajaxHandlersAuthOptional = map[string]map[string]AjaxRouteAuthOptional{
http.MethodGet: {
"/ajax/home": ajaxHome,
"/ajax/spec": ajaxSpec,
"/ajax/spec/subspecs": ajaxSubspecs,
"/ajax/spec/subspec": ajaxSubspec,
"/ajax/spec/urls": ajaxSpecURLs,
"/ajax/spec/community": ajaxSpecLoadCommunity,
"/ajax/spec/community/page": ajaxSpecCommunityLoadCommentsPage,
"/ajax/test": ajaxTest,
},
http.MethodPost: {},
}
var ajaxHandlersAuthRequired = map[string]map[string]AjaxRouteAuthRequired{
http.MethodGet: {
"/ajax/auth": ajaxLoadAuthenticatedUser,
"/ajax/settings": ajaxUserSettings,
"/ajax/logout": ajaxLogoutHandler,
"/ajax/spec/load-edit-block": ajaxLoadBlockForEditing,
"/ajax/community-review": ajaxLoadCommuntyReviewPage,
// admin
"/ajax/admin/signup-requests": ajaxAdminLoadSignupRequests,
"/ajax/admin/users": ajaxAdminLoadUsers,
},
http.MethodPost: {
"/ajax/user/change-password": ajaxUserChangePassword,
"/ajax/user/save-settings": ajaxUserSaveSettings,
// specs
"/ajax/spec/create-spec": ajaxCreateSpec,
"/ajax/spec/save-spec": ajaxSaveSpec,
"/ajax/spec/delete-spec": ajaxDeleteSpec,
"/ajax/spec/create-block": ajaxSpecCreateBlock,
"/ajax/spec/save-block": ajaxSpecSaveBlock,
"/ajax/spec/move-blocks": ajaxSpecMoveBlocks,
"/ajax/spec/delete-block": ajaxSpecDeleteBlock,
"/ajax/spec/create-subspec": ajaxSpecCreateSubspec,
"/ajax/spec/save-subspec": ajaxSpecSaveSubspec,
"/ajax/spec/delete-subspec": ajaxSpecDeleteSubspec,
"/ajax/spec/create-url": ajaxSpecCreateURL,
"/ajax/spec/refresh-url": ajaxSpecRefreshURL,
"/ajax/spec/delete-url": ajaxSpecDeleteURL,
"/ajax/spec/community/mark-read": ajaxSpecCommunityMarkRead,
"/ajax/spec/community/add-comment": ajaxSpecCommunityAddComment,
"/ajax/spec/community/update-comment": ajaxSpecCommunityUpdateComment,
"/ajax/spec/community/delete-comment": ajaxSpecCommunityDeleteComment,
// util
"/ajax/render-markdown": ajaxRenderMarkdown,
// admin
"/ajax/admin/review-signup": ajaxAdminSubmitSignupRequestReview,
"/ajax/admin/send-email": ajaxAdminSendEmail,
},
}
func ajaxHandler(db *sql.DB, userID *uint, w http.ResponseWriter, r *http.Request) {
// var rt = NewResponseTracker(w)
var handle = func(handler func() (interface{}, int)) {
// Verify access to admin routes
if strings.HasPrefix(r.URL.Path, "/ajax/admin") && (userID == nil || *userID != adminUserID) {
logError(r, userID, fmt.Errorf("forbidden admin access"))
w.WriteHeader(http.StatusForbidden)
return
}
response, statusCode := handler()
if statusCode >= 400 {
w.WriteHeader(statusCode)
// Send current version stamp
w.Write([]byte("VersionStamp: " + cacheControlVersionStamp))
return
}
if response != nil {
js, err := json.Marshal(response)
if err != nil {
logError(r, userID, fmt.Errorf("marshalling response: %w", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode) // WriteHeader is called after setting headers
w.Write(js)
} else {
w.WriteHeader(statusCode)
}
}
handlersAuthOptional, foundMethod := ajaxHandlersAuthOptional[r.Method]
if foundMethod {
handler, fouundPath := handlersAuthOptional[r.URL.Path]
if fouundPath {
handle(func() (interface{}, int) {
return handler(db, userID, w, r)
})
return
}
}
handlersAuthRequired, foundMethod := ajaxHandlersAuthRequired[r.Method]
if foundMethod {
handler, fouundPath := handlersAuthRequired[r.URL.Path]
if fouundPath {
if userID == nil {
w.WriteHeader(http.StatusForbidden)
} else {
handle(func() (interface{}, int) {
return handler(db, *userID, w, r)
})
}
return
}
}
w.WriteHeader(http.StatusNotFound)
}
func ajaxTest(db *sql.DB, userID *uint, w http.ResponseWriter, r *http.Request) (interface{}, int) {
var message string
if userID == nil {
message = "Message retrieved using AJAX"
} else {
message = "Message retrieved using AJAX by authenticated user " + UintToA(*userID)
}
return struct {
Message string `json:"message"`
}{message}, http.StatusOK
}