-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.go
336 lines (303 loc) · 8.93 KB
/
routes.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package sqlite
import (
"database/sql"
"embed"
"encoding/json"
"fmt"
"net/http"
"sqlite/templates"
"strconv"
"time"
"github.com/julienschmidt/httprouter"
)
//go:embed assets/*
var assetsFS embed.FS
type Handler struct {
AuthService *AuthService
UserService *UserService
DialService *DialService
UseTLS bool
}
func NewHandler(authService *AuthService, userService *UserService, dialService *DialService, useTLS bool) http.Handler {
mux := http.NewServeMux()
h := &Handler{
AuthService: authService,
UserService: userService,
DialService: dialService,
UseTLS: useTLS,
}
router := NewInstrumentedRouter()
// if you are already authenticated, none of these routes make
// sense
router.GET("/login", requireNoAuth(h.handleGetLogin))
router.POST("/login", requireNoAuth(h.handlePostLogin))
router.GET("/signup", requireNoAuth(h.handleGetSignup))
router.POST("/signup", requireNoAuth(h.handlePostSignup))
// these routes are public.
router.GET("/logout", h.handleLogout)
router.GET("/", h.handleIndex)
// these routes required an authenticated user
router.GET("/dials", requireAuth(h.handleDials))
router.GET("/newDial", requireAuth(h.handleGetNewDials))
router.POST("/newDial", requireAuth(h.handlePostNewDials))
router.GET("/dials/:id", requireAuth(h.handleGetDial))
router.GET("/dials/:id/edit", requireAuth(h.handleGetEditDial))
router.POST("/dials/:id/edit", requireAuth(h.handlePostEditDial))
router.PATCH("/dials/:id", requireAuth(h.handlePatchDial))
router.POST("/dials/:id/delete", requireAuth(h.handleDeleteDial))
mux.Handle("/", authService.Middleware(router))
mux.Handle("/assets/", http.FileServer(http.FS(assetsFS)))
router.NotFound = http.HandlerFunc(handleNotFound)
router.PanicHandler = handleError
return instrumentedHandler(mux)
}
func requireNoAuth(handle httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
userId := UserFromFromContext(r.Context()).UserID
if userId != 0 {
http.Redirect(w, r, "/", http.StatusFound)
return
}
handle(w, r, p)
}
}
func requireAuth(handle httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
userId := UserFromFromContext(r.Context()).UserID
if userId == 0 {
http.Redirect(w, r, fmt.Sprintf("/login?next=%s", r.URL.Path), http.StatusSeeOther)
return
}
handle(w, r, p)
}
}
func (h *Handler) handleIndex(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
userId := UserFromFromContext(r.Context()).UserID
if userId == 0 {
templates.IndexNoAuth().Render(r.Context(), w)
return
}
user, err := h.UserService.Get(r.Context())
if err != nil {
handleError(w, r, err)
return
}
templates.Index(user.UserName).Render(r.Context(), w)
}
func (h *Handler) handleGetLogin(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
templates.Login("", "", r.FormValue("next")).Render(r.Context(), w)
}
func (h *Handler) handlePostLogin(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
userName := r.FormValue("userName")
password := r.FormValue("password")
output, err := h.AuthService.Login(r.Context(), AuthInput{
UserName: userName,
Password: password,
})
if err != nil {
handleError(w, r, err)
return
}
if !output.OK {
w.WriteHeader(http.StatusUnauthorized)
templates.Login("Invalid email and/or password", userName, r.FormValue("next")).Render(r.Context(), w)
return
}
http.SetCookie(w, &http.Cookie{
Name: "token",
Value: output.Token,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
Expires: time.Now().AddDate(0, 0, 30),
Secure: h.UseTLS,
})
redirect := r.FormValue("next")
if redirect == "" {
redirect = "/"
}
http.Redirect(w, r, redirect, http.StatusFound)
}
func (h *Handler) handleGetSignup(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
templates.Signup("", "").Render(r.Context(), w)
}
func (h *Handler) handlePostSignup(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
userName := r.FormValue("userName")
password := r.FormValue("password")
if userName == "" || password == "" {
w.WriteHeader(http.StatusBadRequest)
templates.Signup("Missing required values", userName).Render(r.Context(), w)
return
}
output, err := h.AuthService.Signup(r.Context(), AuthInput{
UserName: userName,
Password: password,
})
if err != nil {
handleError(w, r, err)
return
}
if !output.OK {
w.WriteHeader(http.StatusUnauthorized)
templates.Signup("Username already claimed", userName).Render(r.Context(), w)
return
}
http.SetCookie(w, &http.Cookie{
Name: "token",
Value: output.Token,
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
Expires: time.Now().AddDate(0, 0, 30),
Secure: h.UseTLS,
})
redirect := r.FormValue("next")
if redirect == "" {
redirect = "/"
}
http.Redirect(w, r, redirect, http.StatusFound)
}
func (h *Handler) handleLogout(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
cookie, err := r.Cookie("token")
if err == nil {
// clear and expire the cookie
cookie.Value = ""
cookie.Expires = time.Unix(0, 0)
http.SetCookie(w, cookie)
}
http.Redirect(w, r, "/", http.StatusSeeOther)
}
func (h *Handler) handleDials(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
dials, err := h.DialService.List(r.Context())
if err != nil {
handleError(w, r, err)
return
}
templates.Dials(dials).Render(r.Context(), w)
}
func (h *Handler) handleGetNewDials(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
templates.DialForm("").Render(r.Context(), w)
}
func (h *Handler) handlePostNewDials(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
name := r.FormValue("name")
id, err := h.DialService.Create(r.Context(), name)
if err != nil {
handleError(w, r, err)
return
}
http.Redirect(w, r, fmt.Sprintf("/dials/%d", id), http.StatusFound)
}
func (h *Handler) handleGetDial(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id, err := strconv.ParseInt(p.ByName("id"), 10, 64)
if err != nil {
handleError(w, r, err)
return
}
dial, err := h.DialService.Get(r.Context(), id)
if err != nil {
if err == sql.ErrNoRows {
w.WriteHeader(http.StatusNotFound)
templates.NotFound(true).Render(r.Context(), w)
return
}
handleError(w, r, err)
return
}
templates.Dial(dial).Render(r.Context(), w)
}
func (h *Handler) handleGetEditDial(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id, err := strconv.ParseInt(p.ByName("id"), 10, 64)
if err != nil {
handleError(w, r, err)
return
}
dial, err := h.DialService.Get(r.Context(), id)
if err != nil {
if err == sql.ErrNoRows {
w.WriteHeader(http.StatusNotFound)
templates.NotFound(true).Render(r.Context(), w)
}
handleError(w, r, err)
return
}
templates.DialForm(dial.Name).Render(r.Context(), w)
}
func (h *Handler) handlePostEditDial(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id, err := strconv.ParseInt(p.ByName("id"), 10, 64)
if err != nil {
handleError(w, r, err)
return
}
name := r.FormValue("name")
err = h.DialService.Update(r.Context(), UpdateDial{
ID: id,
Name: name,
})
if err != nil {
if err == sql.ErrNoRows {
w.WriteHeader(http.StatusNotFound)
templates.NotFound(true).Render(r.Context(), w)
return
}
handleError(w, r, err)
return
}
http.Redirect(w, r, fmt.Sprintf("/dials/%d", id), http.StatusFound)
}
type PatchDial struct {
Value int64 `json:"value"`
}
func (h *Handler) handlePatchDial(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id, err := strconv.ParseInt(p.ByName("id"), 10, 64)
if err != nil {
handleError(w, r, err)
return
}
var patch PatchDial
decoder := json.NewDecoder(r.Body)
err = decoder.Decode(&patch)
if err != nil {
handleError(w, r, err)
return
}
err = h.DialService.SetValue(r.Context(), SetDialValue{
ID: id,
Value: patch.Value,
})
if err != nil {
if err == sql.ErrNoRows {
w.WriteHeader(http.StatusNotFound)
templates.NotFound(true).Render(r.Context(), w)
return
}
handleError(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (h *Handler) handleDeleteDial(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id, err := strconv.ParseInt(p.ByName("id"), 10, 64)
if err != nil {
handleError(w, r, err)
return
}
err = h.DialService.Delete(r.Context(), id)
if err != nil {
if err == sql.ErrNoRows {
w.WriteHeader(http.StatusNotFound)
templates.NotFound(true).Render(r.Context(), w)
return
}
handleError(w, r, err)
return
}
http.Redirect(w, r, "/dials", http.StatusSeeOther)
}
func handleError(w http.ResponseWriter, r *http.Request, err interface{}) {
ctx := r.Context()
w.WriteHeader(http.StatusInternalServerError)
templates.Error(UserFromFromContext(ctx).UserID != 0).Render(ctx, w)
}
func handleNotFound(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
w.WriteHeader(http.StatusNotFound)
templates.NotFound(UserFromFromContext(ctx).UserID != 0).Render(ctx, w)
}