-
Notifications
You must be signed in to change notification settings - Fork 2
/
urls.go
93 lines (74 loc) · 3.44 KB
/
urls.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
package heyfyiserver
import (
"strings"
"github.com/gocraft/web"
)
type URL string
const (
HomeUrl URL = "/"
ListFactUrl URL = "/fact"
CreateFactUrl URL = "/fact/create"
ViewFactUrl URL = "/fact/view/:factId"
DeleteFactUrl URL = "/fact/delete/:factId"
VoteOnFactUrl URL = "/api/vote"
ModerateFactUrl URL = "/api/moderate"
SignUpUrl URL = "/signup"
SignInUrl URL = "/signin"
SignOutUrl URL = "/signout"
VerificationUrl URL = "/verify/:accountId/:verificationCode"
RequestPasswordResetUrl URL = "/reset"
ResetPasswordUrl URL = "/reset/:accountId/:resetVerificationCode"
)
func (u URL) String() string {
return string(u)
}
func (u URL) Make(param ...string) string {
if len(param)%2 != 0 {
panic("Make URL " + u.String() + " had non-even number of params")
}
retStr := u.String()
for i := 0; i < len(param); i += 2 {
retStr = strings.Replace(retStr, ":"+param[i], param[i+1], 1)
}
return retStr
}
func initRouter() *web.Router {
rootRouter := web.New(Context{})
rootRouter.Middleware(web.LoggerMiddleware)
rootRouter.Middleware(web.ShowErrorsMiddleware)
rootRouter.Middleware(web.StaticMiddleware("./media/public", web.StaticOption{Prefix: "/public"})) // "public" is a directory to serve files from.)
rootRouter.Middleware((*Context).AssignStorageMiddleware)
rootRouter.Middleware((*Context).AssignTemplatesAndSessionsMiddleware)
rootRouter.Middleware((*Context).LoadUserMiddleware)
rootRouter.Middleware((*Context).GetErrorMessagesMiddleware)
rootRouter.Middleware((*Context).GetNotificationMessagesMiddleware)
//rootRouter web paths
rootRouter.Get(HomeUrl.String(), (*Context).HomeHandler)
//sign up, sign in, etc
rootRouter.Get(SignUpUrl.String(), (*Context).SignUpHandler)
rootRouter.Post(SignUpUrl.String(), (*Context).DoSignUpHandler)
rootRouter.Post(SignInUrl.String(), (*Context).DoSignInRequestHandler)
rootRouter.Get(VerificationUrl.String(), (*Context).DoVerificationRequestHandler)
//password reset handlers
rootRouter.Get(RequestPasswordResetUrl.String(), (*Context).BeginPasswordResetRequestHandler)
rootRouter.Post(RequestPasswordResetUrl.String(), (*Context).DoBeginPasswordResetRequestHandler)
rootRouter.Get(ResetPasswordUrl.String(), (*Context).PasswordResetRequestHandler)
rootRouter.Post(ResetPasswordUrl.String(), (*Context).DoPasswordResetRequestHandler)
//viewing and listing facts handlers
rootRouter.Get(ViewFactUrl.String(), (*Context).ViewFactHandler)
rootRouter.Get(ListFactUrl.String(), (*Context).ListFactsHandler)
//must be logged in for some handlers...
loggedInRouter := rootRouter.Subrouter(LoggedInContext{}, "/")
loggedInRouter.Middleware((*LoggedInContext).RequireAccountMiddleware)
//sign out handler
loggedInRouter.Post(SignOutUrl.String(), (*LoggedInContext).DoSignOutRequestHandler)
//vote, moderate fact handlers
loggedInRouter.Post(VoteOnFactUrl.String(), (*LoggedInContext).VoteOnFactHandler)
loggedInRouter.Post(ModerateFactUrl.String(), (*LoggedInContext).ModerateFactHandler)
//create, delete fact handlers
loggedInRouter.Get(CreateFactUrl.String(), (*LoggedInContext).CreateFactHandler)
loggedInRouter.Post(CreateFactUrl.String(), (*LoggedInContext).DoCreateFactHandler)
loggedInRouter.Get(DeleteFactUrl.String(), (*LoggedInContext).DeleteFactHandler)
loggedInRouter.Post(DeleteFactUrl.String(), (*LoggedInContext).DoDeleteFactHandler)
return rootRouter
}