-
Notifications
You must be signed in to change notification settings - Fork 0
/
router.go
130 lines (112 loc) · 4.76 KB
/
router.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
package main
import (
"github.com/axiomzen/zenauth/config"
"github.com/axiomzen/zenauth/constants"
"github.com/axiomzen/zenauth/context/core"
"github.com/axiomzen/zenauth/context/v1"
"github.com/axiomzen/zenauth/routes"
"github.com/gocraft/web"
)
// InitRouter initializes the router
func InitRouter(c *config.ZENAUTHConfig) *web.Router {
// Setup Base router with middleware
coreRouter := web.New(core.RequestContext{})
// setup a request
coreRouter.Middleware((*core.RequestContext).Setup)
// setup default headers for every request
coreRouter.Middleware((*core.RequestContext).AccessControlAllowHandler)
// handle options (if you want to log OPTIONS requests then put it later)
coreRouter.Middleware((*core.RequestContext).OPTIONSHandler)
// log incoming and outgoing requests
coreRouter.Middleware((*core.RequestContext).Logging)
// compress everything that goes out
coreRouter.Middleware((*core.RequestContext).CompressionHandler)
// check for API token in header
//coreRouter.Middleware((*core.RequestContext).APIAuthRequired)
// custom errors
coreRouter.Error((*core.RequestContext).Error)
// custom 404
coreRouter.NotFound((*core.RequestContext).NotFound)
router := coreRouter.Subrouter(core.RequestContext{}, "")
// new relic plugin
if core.InitNewRelicPlugin(c) {
router.Middleware(core.GoRelicHandler)
}
// new relic agent
if core.InitNewRelicApp(c) {
router.Middleware((*core.RequestContext).NewRelicTransaction)
}
// support ping here (before /v1)
router.Get(routes.ResourcePing, (*core.RequestContext).PingResponse)
// =========
// V1 Routes
// =========
v1APIAuthRouter := router.
Subrouter(v1.APIAuthContext{}, routes.V1).
Middleware((*v1.APIAuthContext).APIAuthRequired).
// Support ping here to test api key
Get(routes.ResourcePing, (*v1.APIAuthContext).PingResponse)
v1APIRouter := router.Subrouter(v1.APIAuthContext{}, routes.V1)
// User routes
// -----------
// No API auth, no user auth
v1APIRouter.Subrouter(v1.UserContext{}, routes.ResourceUsers).
// reset password (POST)
Get(routes.ResourceResetPassword, (*v1.UserContext).ChangePasswordHTML).
Post(routes.ResourceResetPassword, (*v1.UserContext).ResetPassword).
// verify email (PUT) (sent from web browser)
Put(routes.ResourceVerifyEmail, (*v1.UserContext).VerifyEmail).
Get(routes.ResourceForgotPassword, (*v1.UserContext).ForgotPassword).
Get(routes.ResourceMessage, (*v1.UserContext).GeneralMessageHTML)
{
// API auth, but no user auth
v1APIAuthUserRouter := v1APIAuthRouter.
Subrouter(v1.UserContext{}, routes.ResourceUsers).
// user signup
Post(routes.ResourceSignup, (*v1.UserContext).Signup).
// user login
Post(routes.ResourceLogin, (*v1.UserContext).Login).
// Accepts query parameter of: ?email=example@email.ca
Get(routes.ResourceExists, (*v1.UserContext).Exists).
Put(routes.ResourceForgotPassword, (*v1.UserContext).ForgotPassword)
v1APIAuthUserRouter.Subrouter(v1.FacebookContext{}, "").
// Facebook login
Post(routes.ResourceFacebookLogin, (*v1.FacebookContext).Login).
// Facebook signup
Post(routes.ResourceFacebookSignup, (*v1.FacebookContext).Signup).
// Facebook login + signup
Post(routes.ResourceFacebook, (*v1.FacebookContext).Facebook)
{
// API auth and user auth
v1APIAuthUserAuthRouter := v1APIAuthUserRouter.
Subrouter(v1.UserContext{}, "").
Middleware((*v1.UserContext).AuthRequired)
v1APIAuthUserAuthRouter.
Get(routes.ResourceRoot, (*v1.UserContext).GetSelf).
Put(routes.ResourcePassword, (*v1.UserContext).PasswordPut).
Put(routes.ResourceEmail, (*v1.UserContext).EmailPut).
Get("/:id", (*v1.UserContext).Get)
v1APIAuthUserAuthRouter.Subrouter(v1.FacebookContext{}, "").
Post(routes.ResourceFacebookLink, (*v1.FacebookContext).Link)
// Invitations
v1APIAuthUserAuthRouter.
Subrouter(v1.InvitationContext{}, routes.ResourceInvitations).
Post(routes.ResourceEmail, (*v1.InvitationContext).CreateEmailInvitations).
Post(routes.ResourceFacebook, (*v1.InvitationContext).CreateFacebookInvitations)
}
}
// Integration test Routes
if c.Environment == constants.EnvironmentTest {
testRouter := v1APIAuthRouter.Subrouter(v1.TestContext{}, routes.ResourceTest)
// panic route
testRouter.Get(routes.ResourcePanic, (*v1.TestContext).Panic)
testRouter.Subrouter(v1.TestContext{}, routes.ResourceUsers).
Get(routes.ResourcePasswordReset, (*v1.TestContext).UserPasswordResetTokenGet).
// for now using user id, see if we need to delete via token or email
Delete(routes.ResourcePasswordReset+"/:user_id:"+c.UUIDRegex, (*v1.TestContext).UserPasswordResetTokenDelete).
Delete(routes.ResourceInvitations, (*v1.TestContext).InvitationsDelete).
Delete("/:user_id:"+c.UUIDRegex, (*v1.TestContext).UserDelete)
}
// your application routes here
return coreRouter
}