Try to reduce duplication in the router.
This commit is contained in:
parent
ad4dacb447
commit
483ff06775
@ -621,15 +621,15 @@ func NewWriterIntercept(w http.ResponseWriter) *WriterIntercept {
|
||||
var wiMaxAge = "max-age=" + strconv.Itoa(int(c.Day))
|
||||
func (writ *WriterIntercept) WriteHeader(code int) {
|
||||
if code == 200 {
|
||||
writ.ResponseWriter.Header().Set("Cache-Control", wiMaxAge)
|
||||
writ.ResponseWriter.Header().Set("Vary", "Accept-Encoding")
|
||||
h := writ.ResponseWriter.Header()
|
||||
h.Set("Cache-Control", wiMaxAge)
|
||||
h.Set("Vary", "Accept-Encoding")
|
||||
}
|
||||
writ.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
|
||||
// HTTPSRedirect is a connection handler which redirects all HTTP requests to HTTPS
|
||||
type HTTPSRedirect struct {
|
||||
}
|
||||
type HTTPSRedirect struct {}
|
||||
|
||||
func (red *HTTPSRedirect) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Connection", "close")
|
||||
|
@ -386,15 +386,15 @@ func NewWriterIntercept(w http.ResponseWriter) *WriterIntercept {
|
||||
var wiMaxAge = "max-age=" + strconv.Itoa(int(c.Day))
|
||||
func (writ *WriterIntercept) WriteHeader(code int) {
|
||||
if code == 200 {
|
||||
writ.ResponseWriter.Header().Set("Cache-Control", wiMaxAge)
|
||||
writ.ResponseWriter.Header().Set("Vary", "Accept-Encoding")
|
||||
h := writ.ResponseWriter.Header()
|
||||
h.Set("Cache-Control", wiMaxAge)
|
||||
h.Set("Vary", "Accept-Encoding")
|
||||
}
|
||||
writ.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
|
||||
// HTTPSRedirect is a connection handler which redirects all HTTP requests to HTTPS
|
||||
type HTTPSRedirect struct {
|
||||
}
|
||||
type HTTPSRedirect struct {}
|
||||
|
||||
func (red *HTTPSRedirect) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Connection", "close")
|
||||
|
@ -1,5 +1,7 @@
|
||||
package main
|
||||
|
||||
import "strings"
|
||||
|
||||
type RouteGroup struct {
|
||||
Path string
|
||||
RouteList []*RouteImpl
|
||||
@ -9,17 +11,17 @@ type RouteGroup struct {
|
||||
}
|
||||
|
||||
func newRouteGroup(path string, routes ...*RouteImpl) *RouteGroup {
|
||||
group := &RouteGroup{Path: path}
|
||||
g := &RouteGroup{Path: path}
|
||||
for _, route := range routes {
|
||||
route.Parent = group
|
||||
group.RouteList = append(group.RouteList, route)
|
||||
route.Parent = g
|
||||
g.RouteList = append(g.RouteList, route)
|
||||
}
|
||||
return group
|
||||
return g
|
||||
}
|
||||
|
||||
func (group *RouteGroup) Not(path ...string) *RouteSubset {
|
||||
routes := make([]*RouteImpl, len(group.RouteList))
|
||||
copy(routes, group.RouteList)
|
||||
func (g *RouteGroup) Not(path ...string) *RouteSubset {
|
||||
routes := make([]*RouteImpl, len(g.RouteList))
|
||||
copy(routes, g.RouteList)
|
||||
for i, route := range routes {
|
||||
if inStringList(route.Path, path) {
|
||||
routes = append(routes[:i], routes[i+1:]...)
|
||||
@ -37,29 +39,47 @@ func inStringList(needle string, list []string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (group *RouteGroup) NoHeader() *RouteGroup {
|
||||
group.NoHead = true
|
||||
return group
|
||||
func (g *RouteGroup) NoHeader() *RouteGroup {
|
||||
g.NoHead = true
|
||||
return g
|
||||
}
|
||||
|
||||
func (group *RouteGroup) Before(lines ...string) *RouteGroup {
|
||||
func (g *RouteGroup) Before(lines ...string) *RouteGroup {
|
||||
for _, line := range lines {
|
||||
group.RunBefore = append(group.RunBefore, Runnable{line, false})
|
||||
g.RunBefore = append(g.RunBefore, Runnable{line, false})
|
||||
}
|
||||
return group
|
||||
return g
|
||||
}
|
||||
|
||||
func (group *RouteGroup) LitBefore(lines ...string) *RouteGroup {
|
||||
func (g *RouteGroup) LitBefore(lines ...string) *RouteGroup {
|
||||
for _, line := range lines {
|
||||
group.RunBefore = append(group.RunBefore, Runnable{line, true})
|
||||
g.RunBefore = append(g.RunBefore, Runnable{line, true})
|
||||
}
|
||||
return group
|
||||
return g
|
||||
}
|
||||
|
||||
func (group *RouteGroup) Routes(routes ...*RouteImpl) *RouteGroup {
|
||||
/*func (g *RouteGroup) Routes(routes ...*RouteImpl) *RouteGroup {
|
||||
for _, route := range routes {
|
||||
route.Parent = group
|
||||
group.RouteList = append(group.RouteList, route)
|
||||
route.Parent = g
|
||||
g.RouteList = append(g.RouteList, route)
|
||||
}
|
||||
return group
|
||||
return g
|
||||
}*/
|
||||
|
||||
func (g *RouteGroup) Routes(routes ...interface{}) *RouteGroup {
|
||||
for _, route := range routes {
|
||||
switch r := route.(type) {
|
||||
case *RouteImpl:
|
||||
r.Parent = g
|
||||
g.RouteList = append(g.RouteList, r)
|
||||
case RouteSet:
|
||||
for _, rr := range r.Items {
|
||||
rr.Name = r.Name + rr.Name
|
||||
rr.Path = strings.TrimSuffix(r.Path, "/") + "/" + strings.TrimPrefix(rr.Path, "/")
|
||||
rr.Parent = g
|
||||
g.RouteList = append(g.RouteList, rr)
|
||||
}
|
||||
}
|
||||
}
|
||||
return g
|
||||
}
|
||||
|
@ -85,6 +85,14 @@ func View(fname string, path string, args ...string) *RouteImpl {
|
||||
return route(fname, path, false, false, args...)
|
||||
}
|
||||
|
||||
func MView(fname string, path string, args ...string) *RouteImpl {
|
||||
route := route(fname, path, false, false, args...)
|
||||
if !route.hasBefore("SuperModOnly", "AdminOnly") {
|
||||
route.Before("MemberOnly")
|
||||
}
|
||||
return route
|
||||
}
|
||||
|
||||
func MemberView(fname string, path string, args ...string) *RouteImpl {
|
||||
route := route(fname, path, false, false, args...)
|
||||
if !route.hasBefore("SuperModOnly", "AdminOnly") {
|
||||
@ -139,3 +147,13 @@ func (action *uploadAction) MaxSizeVar(varName string) *RouteImpl {
|
||||
action.Route.Before("NoUploadSessionMismatch")
|
||||
return action.Route
|
||||
}
|
||||
|
||||
type RouteSet struct {
|
||||
Name string
|
||||
Path string
|
||||
Items []*RouteImpl
|
||||
}
|
||||
|
||||
func Set(name string, path string, routes ...*RouteImpl) RouteSet {
|
||||
return RouteSet{name, path, routes}
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ func routes(r *Router) {
|
||||
topicGroup := newRouteGroup("/topics/",
|
||||
View("routes.TopicList", "/topics/"),
|
||||
View("routes.TopicListMostViewed", "/topics/most-viewed/"),
|
||||
MemberView("routes.CreateTopic", "/topics/create/", "extraData"),
|
||||
MView("routes.CreateTopic", "/topics/create/", "extraData"),
|
||||
)
|
||||
r.AddGroup(topicGroup)
|
||||
|
||||
@ -49,24 +49,47 @@ func userRoutes() *RouteGroup {
|
||||
return newRouteGroup("/user/").Routes(
|
||||
View("routes.ViewProfile", "/user/").LitBefore("req.URL.Path += extraData"),
|
||||
|
||||
MemberView("routes.AccountEdit", "/user/edit/"),
|
||||
MemberView("routes.AccountEditPassword", "/user/edit/password/"),
|
||||
Set("routes.AccountEdit","/user/edit",
|
||||
MView("", "/"),
|
||||
MView("Password", "/password/"),
|
||||
Action("PasswordSubmit", "/password/submit/"), // TODO: Full test this
|
||||
UploadAction("AvatarSubmit", "/avatar/submit/").MaxSizeVar("int(c.Config.MaxRequestSize)"),
|
||||
Action("RevokeAvatarSubmit", "/avatar/revoke/submit/"),
|
||||
Action("UsernameSubmit", "/username/submit/"), // TODO: Full test this
|
||||
MView("MFA", "/mfa/"),
|
||||
MView("MFASetup", "/mfa/setup/"),
|
||||
Action("MFASetupSubmit", "/mfa/setup/submit/"),
|
||||
Action("MFADisableSubmit", "/mfa/disable/submit/"),
|
||||
MView("Email", "/email/"),
|
||||
View("EmailTokenSubmit", "/token/", "extraData").NoHeader(),
|
||||
),
|
||||
|
||||
/*MView("routes.AccountEdit", "/user/edit/"),
|
||||
MView("routes.AccountEditPassword", "/user/edit/password/"),
|
||||
Action("routes.AccountEditPasswordSubmit", "/user/edit/password/submit/"), // TODO: Full test this
|
||||
UploadAction("routes.AccountEditAvatarSubmit", "/user/edit/avatar/submit/").MaxSizeVar("int(c.Config.MaxRequestSize)"),
|
||||
Action("routes.AccountEditRevokeAvatarSubmit", "/user/edit/avatar/revoke/submit/"),
|
||||
Action("routes.AccountEditUsernameSubmit", "/user/edit/username/submit/"), // TODO: Full test this
|
||||
MemberView("routes.AccountEditMFA", "/user/edit/mfa/"),
|
||||
MemberView("routes.AccountEditMFASetup", "/user/edit/mfa/setup/"),
|
||||
MView("routes.AccountEditMFA", "/user/edit/mfa/"),
|
||||
MView("routes.AccountEditMFASetup", "/user/edit/mfa/setup/"),
|
||||
Action("routes.AccountEditMFASetupSubmit", "/user/edit/mfa/setup/submit/"),
|
||||
Action("routes.AccountEditMFADisableSubmit", "/user/edit/mfa/disable/submit/"),
|
||||
MemberView("routes.AccountEditEmail", "/user/edit/email/"),
|
||||
View("routes.AccountEditEmailTokenSubmit", "/user/edit/token/", "extraData").NoHeader(),
|
||||
MView("routes.AccountEditEmail", "/user/edit/email/"),
|
||||
View("routes.AccountEditEmailTokenSubmit", "/user/edit/token/", "extraData").NoHeader(),*/
|
||||
|
||||
MemberView("routes.AccountLogins", "/user/edit/logins/"),
|
||||
MView("routes.AccountLogins", "/user/edit/logins/"),
|
||||
|
||||
MemberView("routes.LevelList", "/user/levels/"),
|
||||
//MemberView("routes.LevelRankings", "/user/rankings/"),
|
||||
//MemberView("routes.Alerts", "/user/alerts/"),
|
||||
MView("routes.LevelList", "/user/levels/"),
|
||||
//MView("routes.LevelRankings", "/user/rankings/"),
|
||||
//MView("routes.Alerts", "/user/alerts/"),
|
||||
|
||||
/*MView("routes.Convos", "/user/convos/"),
|
||||
MView("routes.ConvosCreate", "/user/convos/create/"),
|
||||
MView("routes.Convo", "/user/convo/","extraData"),
|
||||
Action("routes.ConvosCreateSubmit", "/user/convos/create/submit/"),
|
||||
Action("routes.ConvosDeleteSubmit", "/user/convos/delete/submit/","extraData"),
|
||||
Action("routes.ConvosCreateReplySubmit", "/user/convo/create/submit/"),
|
||||
Action("routes.ConvosDeleteReplySubmit", "/user/convo/delete/submit/"),*/
|
||||
)
|
||||
}
|
||||
|
||||
@ -76,7 +99,7 @@ func usersRoutes() *RouteGroup {
|
||||
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")?
|
||||
MView("routes.IPSearch", "/users/ips/"), // TODO: .Perms("ViewIPs")?
|
||||
)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user