/
server.go
203 lines (162 loc) · 5.36 KB
/
server.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
package server
import (
"log"
"net/http"
"net/url"
"strconv"
"github.com/echlebek/erickson/assets"
"github.com/echlebek/erickson/db"
"github.com/echlebek/erickson/mail"
"github.com/gorilla/mux"
"github.com/gorilla/sessions"
)
const SessionName = "erickson"
type RootHandler struct {
*mux.Router
db.Database
URL *string
FSRoot string
}
type context struct {
router *mux.Router
db db.Database
review int
revision int
url *string
fsRoot string
store *sessions.CookieStore
mailer mail.Mailer
// The request lead to auth failure. This is here because
// I want to call getLogin after postLogin fails, but I don't
// have any way for getLogin to know about the failure.
// badAuth is passed to the template executor, so that the
// page can show that the login failed.
badAuth bool
}
type contextHandlerFunc func(context, http.ResponseWriter, *http.Request)
// authHandler checks the user's session before proceeding with the request.
func (c context) authHandler(f contextHandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
if session, err := c.store.Get(req, SessionName); err != nil || session.IsNew {
http.Redirect(w, req, "/login", http.StatusSeeOther)
return
}
vars := mux.Vars(req)
c.review, _ = strconv.Atoi(vars["id"])
c.revision, _ = strconv.Atoi(vars["revision"])
f(c, w, req)
}
}
// handler adds context to erickson's handlers.
func (c context) handler(f contextHandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
f(c, w, req)
}
}
func (c context) reviewURL() (str string) {
var (
url *url.URL
err error
)
if c.revision > 0 {
url, err = c.router.Get("review").URL("id", strconv.Itoa(c.review), "rev", strconv.Itoa(c.revision))
} else {
url, err = c.router.Get("review").URL("id", strconv.Itoa(c.review))
}
if err != nil {
log.Println(err)
}
str = *c.url + url.String()
return
}
func (c context) revisionURL(id, revision int) (str string) {
sid := strconv.Itoa(id)
srev := strconv.Itoa(revision)
url, err := c.router.Get("revision").URL("id", sid, "revision", srev)
if err != nil {
log.Println(err)
}
str = *c.url + url.String()
return
}
// NewRootHandler creates a RootHandler with the following routes defined:
//
// / GET
// /reviews GET, POST
// /reviews/{id} GET, PATCH, DELETE
// /reviews/{id}/rev POST
// /reviews/{id}/rev/{revision} GET, PATCH
// /reviews/{id}/annotations POST
// /reviews/{id}/rev/{revision}/annotations POST
// /reviews/{id}/rev/{revision}/annotations/publish POST
//
// /login GET, POST
// /logout POST
// /signup GET, POST
// /assets/{name} GET
func NewRootHandler(d db.Database, fsRoot string, sessionKey []byte, mailer mail.Mailer, urlRoot string) *RootHandler {
r := mux.NewRouter()
handler := &RootHandler{Database: d, Router: r, URL: &urlRoot, FSRoot: fsRoot}
store := sessions.NewCookieStore(sessionKey)
ctx := context{
router: r,
db: d,
url: handler.URL,
fsRoot: fsRoot,
store: store,
mailer: mailer,
}
for name, handler := range assets.StylesheetHandlers {
r.Handle("/assets/"+name, handler).Methods("GET")
}
for name, handler := range assets.ScriptHandlers {
r.Handle("/assets/"+name, handler).Methods("GET")
}
r.HandleFunc("/", ctx.authHandler(home)).
Methods("GET")
r.HandleFunc("/", getCsrfToken).Methods("HEAD")
r.HandleFunc("/reviews", ctx.authHandler(home)).
Methods("GET")
r.HandleFunc("/reviews/{id}", ctx.authHandler(getReview)).
Name("review").Methods("GET")
r.HandleFunc("/reviews/{id}", headReview).
Name("review").Methods("HEAD")
r.HandleFunc("/reviews/{id}", ctx.authHandler(deleteReview)).
Methods("DELETE")
r.HandleFunc("/reviews/{id}/status", ctx.authHandler(postStatus)).
Methods("POST").
Headers("Content-Type", "application/x-www-form-urlencoded")
r.HandleFunc("/reviews/{id}/annotations", ctx.authHandler(postAnnotation)).
Methods("POST").
Headers("Content-Type", "application/x-www-form-urlencoded")
r.HandleFunc("/reviews/{id}", ctx.authHandler(patchReview)).
Methods("PATCH")
r.HandleFunc("/reviews/{id}/rev/{revision}", ctx.authHandler(getReview)).
Name("revision").
Methods("GET")
r.HandleFunc("/reviews/{id}/rev/{revision}/annotations", ctx.authHandler(postAnnotation)).
Methods("POST").
Headers("Content-Type", "application/x-www-form-urlencoded")
r.HandleFunc("/reviews/{id}/rev/{revision}/annotations/publish", ctx.authHandler(publishAnnotations)).
Methods("POST")
r.HandleFunc("/reviews", ctx.authHandler(postJSONReview)).
Methods("POST").
Headers("Content-Type", "application/json")
r.HandleFunc("/reviews", ctx.authHandler(postFormReview)).
Methods("POST").
Headers("Content-Type", "application/x-www-form-urlencoded")
r.HandleFunc("/reviews/{id}/rev", ctx.authHandler(postRevision)).
Methods("POST").
Headers("Content-Type", "application/json")
r.HandleFunc("/reviews/{id}/rev/{revision}", ctx.authHandler(patchRevision)).
Methods("PATCH")
r.HandleFunc("/signup", ctx.handler(getSignup)).Methods("GET")
r.HandleFunc("/signup", ctx.handler(postSignup)).Methods("POST").
Headers("Content-Type", "application/x-www-form-urlencoded")
r.HandleFunc("/login", ctx.handler(getLogin)).
Methods("GET")
r.HandleFunc("/login", ctx.handler(postLogin)).Methods("POST").
Headers("Content-Type", "application/x-www-form-urlencoded")
r.HandleFunc("/logout", ctx.authHandler(postLogout)).Methods("POST")
return handler
}