This repository was archived by the owner on May 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.go
62 lines (52 loc) · 1.85 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
package main
import (
"net/http"
)
type route struct {
pattern string
method string
handler appHandler
public bool
}
var routes = []route{
{"/login", "POST", webapp.Login, true},
{"/user", "POST", webapp.CreateUser, true},
{"/user", "GET", webapp.RetrieveUsers, false},
{"/user/{id:[0-9]+}", "GET", webapp.RetrieveUser, false},
{"/user/{email}", "GET", webapp.RetrieveUserByEmail, false},
{"/user/{id:[0-9]+}", "DELETE", webapp.DeleteUser, false},
{"/user/{id:[0-9]+}", "PUT", webapp.UpdateUser, false},
{"/question", "POST", webapp.CreateQuestion, false},
{"/question", "GET", webapp.RetrieveQuestions, false},
{"/question/{id:[0-9]+}", "GET", webapp.RetrieveQuestion, false},
{"/question/{id:[0-9]+}", "PUT", webapp.UpdateQuestion, false},
{"/question/{id:[0-9]+}/vote", "PUT", webapp.UpVoteQuestion, false},
{"/question/{id:[0-9]+}/vote", "DELETE", webapp.DownVoteQuestion, false},
{"/question/{id:[0-9]+}/comments", "POST", webapp.CreateQuestionComments,
false},
{"/question/{id:[0-9]+}/comments", "GET", webapp.RetrieveQuestionComments,
false},
{"/question/{id:[0-9]+}/comments/{cid:[0-9]+}", "GET",
webapp.RetrieveQuestionComment, false},
{"/question/{id:[0-9]+}/comments/{cid:[0-9]+}", "PUT",
webapp.UpdateQuestionComment, false},
{"/question/{id:[0-9]+}/comments/{cid:[0-9]+}/vote", "PUT",
webapp.UpVoteQuestionComment, false},
{"/question/{id:[0-9]+}/comments/{cid:[0-9]+}/vote", "DELETE",
webapp.DownVoteQuestionComment, false},
}
func (app *app) registerRoutes(logger func(appHandler) http.Handler) {
for _, route := range app.routes {
app.router.Handle(route.pattern, logger(route.handler)).
Methods(route.method)
}
}
func (app *app) isPublic(r *http.Request) bool {
for _, route := range app.routes {
if route.pattern == r.URL.Path && r.Method == route.method &&
route.public {
return true
}
}
return false
}