Refactored the route declarations.

This commit is contained in:
Azareal 2018-11-13 14:43:05 +10:00
parent 2049948b0f
commit 4040417320
4 changed files with 56 additions and 65 deletions

View File

@ -9,9 +9,6 @@ import (
"text/template"
)
var routeList []*RouteImpl
var routeGroups []*RouteGroup
type TmplVars struct {
RouteList []*RouteImpl
RouteGroups []*RouteGroup
@ -27,11 +24,12 @@ func main() {
log.Println("Generating the router...")
// Load all the routes...
routes()
r := &Router{}
routes(r)
var tmplVars = TmplVars{
RouteList: routeList,
RouteGroups: routeGroups,
RouteList: r.routeList,
RouteGroups: r.routeGroups,
}
var allRouteNames []string
var allRouteMap = make(map[string]int)
@ -64,7 +62,7 @@ func main() {
return out
}
for _, route := range routeList {
for _, route := range r.routeList {
mapIt(route.Name)
var end = len(route.Path) - 1
out += "\n\t\tcase \"" + route.Path[0:end] + "\":"
@ -84,7 +82,7 @@ func main() {
out += `)`
}
for _, group := range routeGroups {
for _, group := range r.routeGroups {
var end = len(group.Path) - 1
out += "\n\t\tcase \"" + group.Path[0:end] + "\":"
out += runBefore(group.RunBefore, 3)

View File

@ -18,10 +18,6 @@ type Runnable struct {
Literal bool
}
func addRoute(route *RouteImpl) {
routeList = append(routeList, route)
}
func (route *RouteImpl) Before(items ...string) *RouteImpl {
for _, item := range items {
route.RunBefore = append(route.RunBefore, Runnable{item, false})
@ -77,10 +73,6 @@ func (route *RouteImpl) NoHeader() *RouteImpl {
return route
}
func addRouteGroup(routeGroup *RouteGroup) {
routeGroups = append(routeGroups, routeGroup)
}
func blankRoute() *RouteImpl {
return &RouteImpl{"", "", false, false, []string{}, []Runnable{}, nil}
}

14
router_gen/router.go Normal file
View File

@ -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...)
}

View File

@ -1,13 +1,13 @@
package main
// TODO: How should we handle *HeaderLite and *Header?
func routes() {
addRoute(View("routes.Overview", "/overview/"))
addRoute(View("routes.CustomPage", "/pages/", "extraData"))
addRoute(View("routes.ForumList", "/forums/" /*,"&forums"*/))
addRoute(View("routes.ViewForum", "/forum/", "extraData"))
addRoute(AnonAction("routes.ChangeTheme", "/theme/"))
addRoute(
func routes(r *Router) {
r.Add(View("routes.Overview", "/overview/"))
r.Add(View("routes.CustomPage", "/pages/", "extraData"))
r.Add(View("routes.ForumList", "/forums/" /*,"&forums"*/))
r.Add(View("routes.ViewForum", "/forum/", "extraData"))
r.Add(AnonAction("routes.ChangeTheme", "/theme/"))
r.Add(
View("routes.ShowAttachment", "/attachs/", "extraData").Before("ParseForm").NoGzip().NoHeader(),
)
@ -17,36 +17,36 @@ func routes() {
View("routes.APIMe", "/api/me/"),
View("routeJSAntispam", "/api/watches/"),
).NoHeader()
addRouteGroup(apiGroup)
r.AddGroup(apiGroup)
// TODO: Reduce the number of Befores. With a new method, perhaps?
reportGroup := newRouteGroup("/report/",
Action("routes.ReportSubmit", "/report/submit/", "extraData"),
).Before("NoBanned")
addRouteGroup(reportGroup)
r.AddGroup(reportGroup)
topicGroup := newRouteGroup("/topics/",
View("routes.TopicList", "/topics/"),
View("routes.TopicListMostViewed", "/topics/most-viewed/"),
MemberView("routes.CreateTopic", "/topics/create/", "extraData"),
)
addRouteGroup(topicGroup)
r.AddGroup(topicGroup)
buildPanelRoutes()
buildUserRoutes()
buildTopicRoutes()
buildReplyRoutes()
buildProfileReplyRoutes()
buildPollRoutes()
buildAccountRoutes()
r.AddGroup(panelRoutes())
r.AddGroup(userRoutes())
r.AddGroup(usersRoutes())
r.AddGroup(topicRoutes())
r.AddGroup(replyRoutes())
r.AddGroup(profileReplyRoutes())
r.AddGroup(pollRoutes())
r.AddGroup(accountRoutes())
addRoute(Special("common.RouteWebsockets", "/ws/"))
r.Add(Special("common.RouteWebsockets", "/ws/"))
}
// TODO: Test the email token route
func buildUserRoutes() {
userGroup := newRouteGroup("/user/")
userGroup.Routes(
func userRoutes() *RouteGroup {
return newRouteGroup("/user/").Routes(
View("routes.ViewProfile", "/user/").LitBefore("req.URL.Path += extraData"),
MemberView("routes.AccountEdit", "/user/edit/"),
@ -63,22 +63,20 @@ func buildUserRoutes() {
MemberView("routes.LevelList", "/user/levels/"),
)
addRouteGroup(userGroup)
}
func usersRoutes() *RouteGroup {
// TODO: Auto test and manual test these routes
userGroup = newRouteGroup("/users/")
userGroup.Routes(
return newRouteGroup("/users/").Routes(
Action("routes.BanUserSubmit", "/users/ban/submit/", "extraData"),
Action("routes.UnbanUser", "/users/unban/", "extraData"),
Action("routes.ActivateUser", "/users/activate/", "extraData"),
MemberView("routes.IPSearch", "/users/ips/"), // TODO: .Perms("ViewIPs")?
)
addRouteGroup(userGroup)
}
func buildTopicRoutes() {
topicGroup := newRouteGroup("/topic/")
topicGroup.Routes(
func topicRoutes() *RouteGroup {
return newRouteGroup("/topic/").Routes(
View("routes.ViewTopic", "/topic/", "extraData"),
UploadAction("routes.CreateTopicSubmit", "/topic/create/submit/").MaxSizeVar("int(common.Config.MaxRequestSize)"),
Action("routes.EditTopicSubmit", "/topic/edit/submit/", "extraData"),
@ -90,12 +88,10 @@ func buildTopicRoutes() {
Action("routes.MoveTopicSubmit", "/topic/move/submit/", "extraData"),
Action("routes.LikeTopicSubmit", "/topic/like/submit/", "extraData").Before("ParseForm"),
)
addRouteGroup(topicGroup)
}
func buildReplyRoutes() {
replyGroup := newRouteGroup("/reply/")
replyGroup.Routes(
func replyRoutes() *RouteGroup {
return newRouteGroup("/reply/").Routes(
// 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/
Action("routes.ReplyEditSubmit", "/reply/edit/submit/", "extraData"),
@ -104,33 +100,27 @@ func buildReplyRoutes() {
//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
)
addRouteGroup(replyGroup)
}
// TODO: Move these into /user/?
func buildProfileReplyRoutes() {
pReplyGroup := newRouteGroup("/profile/")
pReplyGroup.Routes(
func profileReplyRoutes() *RouteGroup {
return newRouteGroup("/profile/").Routes(
Action("routes.ProfileReplyCreateSubmit", "/profile/reply/create/"), // TODO: Add /submit/ to the end
Action("routes.ProfileReplyEditSubmit", "/profile/reply/edit/submit/", "extraData"),
Action("routes.ProfileReplyDeleteSubmit", "/profile/reply/delete/submit/", "extraData"),
)
addRouteGroup(pReplyGroup)
}
func buildPollRoutes() {
pollGroup := newRouteGroup("/poll/")
pollGroup.Routes(
func pollRoutes() *RouteGroup {
return newRouteGroup("/poll/").Routes(
Action("routes.PollVote", "/poll/vote/", "extraData"),
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
accReplyGroup := newRouteGroup("/accounts/")
accReplyGroup.Routes(
return newRouteGroup("/accounts/").Routes(
View("routes.AccountLogin", "/accounts/login/"),
View("routes.AccountRegister", "/accounts/create/"),
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.AccountRegisterSubmit", "/accounts/create/submit/"),
)
addRouteGroup(accReplyGroup)
}
func buildPanelRoutes() {
panelGroup := newRouteGroup("/panel/").Before("SuperModOnly").NoHeader()
panelGroup.Routes(
func panelRoutes() *RouteGroup {
return newRouteGroup("/panel/").Before("SuperModOnly").NoHeader().Routes(
View("panel.Dashboard", "/panel/"),
View("panel.Forums", "/panel/forums/"),
Action("panel.ForumsCreateSubmit", "/panel/forums/create/"),
@ -219,5 +207,4 @@ func buildPanelRoutes() {
View("panel.LogsMod", "/panel/logs/mod/"),
View("panel.Debug", "/panel/debug/").Before("AdminOnly"),
)
addRouteGroup(panelGroup)
}