Refactored the route declarations.
This commit is contained in:
parent
2049948b0f
commit
4040417320
|
@ -9,9 +9,6 @@ import (
|
||||||
"text/template"
|
"text/template"
|
||||||
)
|
)
|
||||||
|
|
||||||
var routeList []*RouteImpl
|
|
||||||
var routeGroups []*RouteGroup
|
|
||||||
|
|
||||||
type TmplVars struct {
|
type TmplVars struct {
|
||||||
RouteList []*RouteImpl
|
RouteList []*RouteImpl
|
||||||
RouteGroups []*RouteGroup
|
RouteGroups []*RouteGroup
|
||||||
|
@ -27,11 +24,12 @@ func main() {
|
||||||
log.Println("Generating the router...")
|
log.Println("Generating the router...")
|
||||||
|
|
||||||
// Load all the routes...
|
// Load all the routes...
|
||||||
routes()
|
r := &Router{}
|
||||||
|
routes(r)
|
||||||
|
|
||||||
var tmplVars = TmplVars{
|
var tmplVars = TmplVars{
|
||||||
RouteList: routeList,
|
RouteList: r.routeList,
|
||||||
RouteGroups: routeGroups,
|
RouteGroups: r.routeGroups,
|
||||||
}
|
}
|
||||||
var allRouteNames []string
|
var allRouteNames []string
|
||||||
var allRouteMap = make(map[string]int)
|
var allRouteMap = make(map[string]int)
|
||||||
|
@ -64,7 +62,7 @@ func main() {
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, route := range routeList {
|
for _, route := range r.routeList {
|
||||||
mapIt(route.Name)
|
mapIt(route.Name)
|
||||||
var end = len(route.Path) - 1
|
var end = len(route.Path) - 1
|
||||||
out += "\n\t\tcase \"" + route.Path[0:end] + "\":"
|
out += "\n\t\tcase \"" + route.Path[0:end] + "\":"
|
||||||
|
@ -84,7 +82,7 @@ func main() {
|
||||||
out += `)`
|
out += `)`
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, group := range routeGroups {
|
for _, group := range r.routeGroups {
|
||||||
var end = len(group.Path) - 1
|
var end = len(group.Path) - 1
|
||||||
out += "\n\t\tcase \"" + group.Path[0:end] + "\":"
|
out += "\n\t\tcase \"" + group.Path[0:end] + "\":"
|
||||||
out += runBefore(group.RunBefore, 3)
|
out += runBefore(group.RunBefore, 3)
|
||||||
|
|
|
@ -18,10 +18,6 @@ type Runnable struct {
|
||||||
Literal bool
|
Literal bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func addRoute(route *RouteImpl) {
|
|
||||||
routeList = append(routeList, route)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (route *RouteImpl) Before(items ...string) *RouteImpl {
|
func (route *RouteImpl) Before(items ...string) *RouteImpl {
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
route.RunBefore = append(route.RunBefore, Runnable{item, false})
|
route.RunBefore = append(route.RunBefore, Runnable{item, false})
|
||||||
|
@ -77,10 +73,6 @@ func (route *RouteImpl) NoHeader() *RouteImpl {
|
||||||
return route
|
return route
|
||||||
}
|
}
|
||||||
|
|
||||||
func addRouteGroup(routeGroup *RouteGroup) {
|
|
||||||
routeGroups = append(routeGroups, routeGroup)
|
|
||||||
}
|
|
||||||
|
|
||||||
func blankRoute() *RouteImpl {
|
func blankRoute() *RouteImpl {
|
||||||
return &RouteImpl{"", "", false, false, []string{}, []Runnable{}, nil}
|
return &RouteImpl{"", "", false, false, []string{}, []Runnable{}, nil}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type Router struct {
|
||||||
|
routeList []*RouteImpl
|
||||||
|
routeGroups []*RouteGroup
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Router) Add(route ...*RouteImpl) {
|
||||||
|
r.routeList = append(r.routeList, route...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *Router) AddGroup(routeGroup ...*RouteGroup) {
|
||||||
|
r.routeGroups = append(r.routeGroups, routeGroup...)
|
||||||
|
}
|
|
@ -1,13 +1,13 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
// TODO: How should we handle *HeaderLite and *Header?
|
// TODO: How should we handle *HeaderLite and *Header?
|
||||||
func routes() {
|
func routes(r *Router) {
|
||||||
addRoute(View("routes.Overview", "/overview/"))
|
r.Add(View("routes.Overview", "/overview/"))
|
||||||
addRoute(View("routes.CustomPage", "/pages/", "extraData"))
|
r.Add(View("routes.CustomPage", "/pages/", "extraData"))
|
||||||
addRoute(View("routes.ForumList", "/forums/" /*,"&forums"*/))
|
r.Add(View("routes.ForumList", "/forums/" /*,"&forums"*/))
|
||||||
addRoute(View("routes.ViewForum", "/forum/", "extraData"))
|
r.Add(View("routes.ViewForum", "/forum/", "extraData"))
|
||||||
addRoute(AnonAction("routes.ChangeTheme", "/theme/"))
|
r.Add(AnonAction("routes.ChangeTheme", "/theme/"))
|
||||||
addRoute(
|
r.Add(
|
||||||
View("routes.ShowAttachment", "/attachs/", "extraData").Before("ParseForm").NoGzip().NoHeader(),
|
View("routes.ShowAttachment", "/attachs/", "extraData").Before("ParseForm").NoGzip().NoHeader(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,36 +17,36 @@ func routes() {
|
||||||
View("routes.APIMe", "/api/me/"),
|
View("routes.APIMe", "/api/me/"),
|
||||||
View("routeJSAntispam", "/api/watches/"),
|
View("routeJSAntispam", "/api/watches/"),
|
||||||
).NoHeader()
|
).NoHeader()
|
||||||
addRouteGroup(apiGroup)
|
r.AddGroup(apiGroup)
|
||||||
|
|
||||||
// TODO: Reduce the number of Befores. With a new method, perhaps?
|
// TODO: Reduce the number of Befores. With a new method, perhaps?
|
||||||
reportGroup := newRouteGroup("/report/",
|
reportGroup := newRouteGroup("/report/",
|
||||||
Action("routes.ReportSubmit", "/report/submit/", "extraData"),
|
Action("routes.ReportSubmit", "/report/submit/", "extraData"),
|
||||||
).Before("NoBanned")
|
).Before("NoBanned")
|
||||||
addRouteGroup(reportGroup)
|
r.AddGroup(reportGroup)
|
||||||
|
|
||||||
topicGroup := newRouteGroup("/topics/",
|
topicGroup := newRouteGroup("/topics/",
|
||||||
View("routes.TopicList", "/topics/"),
|
View("routes.TopicList", "/topics/"),
|
||||||
View("routes.TopicListMostViewed", "/topics/most-viewed/"),
|
View("routes.TopicListMostViewed", "/topics/most-viewed/"),
|
||||||
MemberView("routes.CreateTopic", "/topics/create/", "extraData"),
|
MemberView("routes.CreateTopic", "/topics/create/", "extraData"),
|
||||||
)
|
)
|
||||||
addRouteGroup(topicGroup)
|
r.AddGroup(topicGroup)
|
||||||
|
|
||||||
buildPanelRoutes()
|
r.AddGroup(panelRoutes())
|
||||||
buildUserRoutes()
|
r.AddGroup(userRoutes())
|
||||||
buildTopicRoutes()
|
r.AddGroup(usersRoutes())
|
||||||
buildReplyRoutes()
|
r.AddGroup(topicRoutes())
|
||||||
buildProfileReplyRoutes()
|
r.AddGroup(replyRoutes())
|
||||||
buildPollRoutes()
|
r.AddGroup(profileReplyRoutes())
|
||||||
buildAccountRoutes()
|
r.AddGroup(pollRoutes())
|
||||||
|
r.AddGroup(accountRoutes())
|
||||||
|
|
||||||
addRoute(Special("common.RouteWebsockets", "/ws/"))
|
r.Add(Special("common.RouteWebsockets", "/ws/"))
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Test the email token route
|
// TODO: Test the email token route
|
||||||
func buildUserRoutes() {
|
func userRoutes() *RouteGroup {
|
||||||
userGroup := newRouteGroup("/user/")
|
return newRouteGroup("/user/").Routes(
|
||||||
userGroup.Routes(
|
|
||||||
View("routes.ViewProfile", "/user/").LitBefore("req.URL.Path += extraData"),
|
View("routes.ViewProfile", "/user/").LitBefore("req.URL.Path += extraData"),
|
||||||
|
|
||||||
MemberView("routes.AccountEdit", "/user/edit/"),
|
MemberView("routes.AccountEdit", "/user/edit/"),
|
||||||
|
@ -63,22 +63,20 @@ func buildUserRoutes() {
|
||||||
|
|
||||||
MemberView("routes.LevelList", "/user/levels/"),
|
MemberView("routes.LevelList", "/user/levels/"),
|
||||||
)
|
)
|
||||||
addRouteGroup(userGroup)
|
}
|
||||||
|
|
||||||
|
func usersRoutes() *RouteGroup {
|
||||||
// TODO: Auto test and manual test these routes
|
// TODO: Auto test and manual test these routes
|
||||||
userGroup = newRouteGroup("/users/")
|
return newRouteGroup("/users/").Routes(
|
||||||
userGroup.Routes(
|
|
||||||
Action("routes.BanUserSubmit", "/users/ban/submit/", "extraData"),
|
Action("routes.BanUserSubmit", "/users/ban/submit/", "extraData"),
|
||||||
Action("routes.UnbanUser", "/users/unban/", "extraData"),
|
Action("routes.UnbanUser", "/users/unban/", "extraData"),
|
||||||
Action("routes.ActivateUser", "/users/activate/", "extraData"),
|
Action("routes.ActivateUser", "/users/activate/", "extraData"),
|
||||||
MemberView("routes.IPSearch", "/users/ips/"), // TODO: .Perms("ViewIPs")?
|
MemberView("routes.IPSearch", "/users/ips/"), // TODO: .Perms("ViewIPs")?
|
||||||
)
|
)
|
||||||
addRouteGroup(userGroup)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildTopicRoutes() {
|
func topicRoutes() *RouteGroup {
|
||||||
topicGroup := newRouteGroup("/topic/")
|
return newRouteGroup("/topic/").Routes(
|
||||||
topicGroup.Routes(
|
|
||||||
View("routes.ViewTopic", "/topic/", "extraData"),
|
View("routes.ViewTopic", "/topic/", "extraData"),
|
||||||
UploadAction("routes.CreateTopicSubmit", "/topic/create/submit/").MaxSizeVar("int(common.Config.MaxRequestSize)"),
|
UploadAction("routes.CreateTopicSubmit", "/topic/create/submit/").MaxSizeVar("int(common.Config.MaxRequestSize)"),
|
||||||
Action("routes.EditTopicSubmit", "/topic/edit/submit/", "extraData"),
|
Action("routes.EditTopicSubmit", "/topic/edit/submit/", "extraData"),
|
||||||
|
@ -90,12 +88,10 @@ func buildTopicRoutes() {
|
||||||
Action("routes.MoveTopicSubmit", "/topic/move/submit/", "extraData"),
|
Action("routes.MoveTopicSubmit", "/topic/move/submit/", "extraData"),
|
||||||
Action("routes.LikeTopicSubmit", "/topic/like/submit/", "extraData").Before("ParseForm"),
|
Action("routes.LikeTopicSubmit", "/topic/like/submit/", "extraData").Before("ParseForm"),
|
||||||
)
|
)
|
||||||
addRouteGroup(topicGroup)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildReplyRoutes() {
|
func replyRoutes() *RouteGroup {
|
||||||
replyGroup := newRouteGroup("/reply/")
|
return newRouteGroup("/reply/").Routes(
|
||||||
replyGroup.Routes(
|
|
||||||
// TODO: Reduce this to 1MB for attachments for each file?
|
// TODO: Reduce this to 1MB for attachments for each file?
|
||||||
UploadAction("routes.CreateReplySubmit", "/reply/create/").MaxSizeVar("int(common.Config.MaxRequestSize)"), // TODO: Rename the route so it's /reply/create/submit/
|
UploadAction("routes.CreateReplySubmit", "/reply/create/").MaxSizeVar("int(common.Config.MaxRequestSize)"), // TODO: Rename the route so it's /reply/create/submit/
|
||||||
Action("routes.ReplyEditSubmit", "/reply/edit/submit/", "extraData"),
|
Action("routes.ReplyEditSubmit", "/reply/edit/submit/", "extraData"),
|
||||||
|
@ -104,33 +100,27 @@ func buildReplyRoutes() {
|
||||||
//MemberView("routes.ReplyEdit","/reply/edit/","extraData"), // No js fallback
|
//MemberView("routes.ReplyEdit","/reply/edit/","extraData"), // No js fallback
|
||||||
//MemberView("routes.ReplyDelete","/reply/delete/","extraData"), // No js confirmation page? We could have a confirmation modal for the JS case
|
//MemberView("routes.ReplyDelete","/reply/delete/","extraData"), // No js confirmation page? We could have a confirmation modal for the JS case
|
||||||
)
|
)
|
||||||
addRouteGroup(replyGroup)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Move these into /user/?
|
// TODO: Move these into /user/?
|
||||||
func buildProfileReplyRoutes() {
|
func profileReplyRoutes() *RouteGroup {
|
||||||
pReplyGroup := newRouteGroup("/profile/")
|
return newRouteGroup("/profile/").Routes(
|
||||||
pReplyGroup.Routes(
|
|
||||||
Action("routes.ProfileReplyCreateSubmit", "/profile/reply/create/"), // TODO: Add /submit/ to the end
|
Action("routes.ProfileReplyCreateSubmit", "/profile/reply/create/"), // TODO: Add /submit/ to the end
|
||||||
Action("routes.ProfileReplyEditSubmit", "/profile/reply/edit/submit/", "extraData"),
|
Action("routes.ProfileReplyEditSubmit", "/profile/reply/edit/submit/", "extraData"),
|
||||||
Action("routes.ProfileReplyDeleteSubmit", "/profile/reply/delete/submit/", "extraData"),
|
Action("routes.ProfileReplyDeleteSubmit", "/profile/reply/delete/submit/", "extraData"),
|
||||||
)
|
)
|
||||||
addRouteGroup(pReplyGroup)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildPollRoutes() {
|
func pollRoutes() *RouteGroup {
|
||||||
pollGroup := newRouteGroup("/poll/")
|
return newRouteGroup("/poll/").Routes(
|
||||||
pollGroup.Routes(
|
|
||||||
Action("routes.PollVote", "/poll/vote/", "extraData"),
|
Action("routes.PollVote", "/poll/vote/", "extraData"),
|
||||||
View("routes.PollResults", "/poll/results/", "extraData").NoHeader(),
|
View("routes.PollResults", "/poll/results/", "extraData").NoHeader(),
|
||||||
)
|
)
|
||||||
addRouteGroup(pollGroup)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildAccountRoutes() {
|
func accountRoutes() *RouteGroup {
|
||||||
//router.HandleFunc("/accounts/list/", routeLogin) // Redirect /accounts/ and /user/ to here.. // Get a list of all of the accounts on the forum
|
//router.HandleFunc("/accounts/list/", routeLogin) // Redirect /accounts/ and /user/ to here.. // Get a list of all of the accounts on the forum
|
||||||
accReplyGroup := newRouteGroup("/accounts/")
|
return newRouteGroup("/accounts/").Routes(
|
||||||
accReplyGroup.Routes(
|
|
||||||
View("routes.AccountLogin", "/accounts/login/"),
|
View("routes.AccountLogin", "/accounts/login/"),
|
||||||
View("routes.AccountRegister", "/accounts/create/"),
|
View("routes.AccountRegister", "/accounts/create/"),
|
||||||
Action("routes.AccountLogout", "/accounts/logout/"),
|
Action("routes.AccountLogout", "/accounts/logout/"),
|
||||||
|
@ -139,12 +129,10 @@ func buildAccountRoutes() {
|
||||||
AnonAction("routes.AccountLoginMFAVerifySubmit", "/accounts/mfa_verify/submit/"), // We have logic in here which filters out regular guests
|
AnonAction("routes.AccountLoginMFAVerifySubmit", "/accounts/mfa_verify/submit/"), // We have logic in here which filters out regular guests
|
||||||
AnonAction("routes.AccountRegisterSubmit", "/accounts/create/submit/"),
|
AnonAction("routes.AccountRegisterSubmit", "/accounts/create/submit/"),
|
||||||
)
|
)
|
||||||
addRouteGroup(accReplyGroup)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildPanelRoutes() {
|
func panelRoutes() *RouteGroup {
|
||||||
panelGroup := newRouteGroup("/panel/").Before("SuperModOnly").NoHeader()
|
return newRouteGroup("/panel/").Before("SuperModOnly").NoHeader().Routes(
|
||||||
panelGroup.Routes(
|
|
||||||
View("panel.Dashboard", "/panel/"),
|
View("panel.Dashboard", "/panel/"),
|
||||||
View("panel.Forums", "/panel/forums/"),
|
View("panel.Forums", "/panel/forums/"),
|
||||||
Action("panel.ForumsCreateSubmit", "/panel/forums/create/"),
|
Action("panel.ForumsCreateSubmit", "/panel/forums/create/"),
|
||||||
|
@ -219,5 +207,4 @@ func buildPanelRoutes() {
|
||||||
View("panel.LogsMod", "/panel/logs/mod/"),
|
View("panel.LogsMod", "/panel/logs/mod/"),
|
||||||
View("panel.Debug", "/panel/debug/").Before("AdminOnly"),
|
View("panel.Debug", "/panel/debug/").Before("AdminOnly"),
|
||||||
)
|
)
|
||||||
addRouteGroup(panelGroup)
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue