2017-11-10 00:16:15 +00:00
|
|
|
package main
|
|
|
|
|
2018-08-22 02:13:17 +00:00
|
|
|
import "strings"
|
|
|
|
|
2017-11-10 00:16:15 +00:00
|
|
|
type RouteImpl struct {
|
2017-11-12 11:29:23 +00:00
|
|
|
Name string
|
|
|
|
Path string
|
|
|
|
Vars []string
|
|
|
|
RunBefore []Runnable
|
2017-11-12 07:18:25 +00:00
|
|
|
|
|
|
|
Parent *RouteGroup
|
2017-11-10 00:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Runnable struct {
|
|
|
|
Contents string
|
|
|
|
Literal bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func addRoute(route *RouteImpl) {
|
|
|
|
routeList = append(routeList, route)
|
|
|
|
}
|
|
|
|
|
2017-11-10 03:33:11 +00:00
|
|
|
func (route *RouteImpl) Before(items ...string) *RouteImpl {
|
|
|
|
for _, item := range items {
|
|
|
|
route.RunBefore = append(route.RunBefore, Runnable{item, false})
|
|
|
|
}
|
|
|
|
return route
|
|
|
|
}
|
|
|
|
|
|
|
|
func (route *RouteImpl) LitBefore(items ...string) *RouteImpl {
|
|
|
|
for _, item := range items {
|
|
|
|
route.RunBefore = append(route.RunBefore, Runnable{item, true})
|
2017-11-10 00:16:15 +00:00
|
|
|
}
|
|
|
|
return route
|
|
|
|
}
|
|
|
|
|
2018-08-22 02:13:17 +00:00
|
|
|
func (route *RouteImpl) LitBeforeMultiline(items ...string) *RouteImpl {
|
|
|
|
for _, item := range items {
|
|
|
|
for _, line := range strings.Split(item, "\n") {
|
|
|
|
route.LitBefore(strings.TrimSpace(line))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return route
|
|
|
|
}
|
|
|
|
|
2017-11-12 07:18:25 +00:00
|
|
|
func (route *RouteImpl) hasBefore(items ...string) bool {
|
|
|
|
for _, item := range items {
|
2017-11-12 11:29:23 +00:00
|
|
|
if route.hasBeforeItem(item) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (route *RouteImpl) hasBeforeItem(item string) bool {
|
|
|
|
for _, before := range route.RunBefore {
|
|
|
|
if before.Contents == item {
|
|
|
|
return true
|
2017-11-12 07:18:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-08-22 02:13:17 +00:00
|
|
|
func (route *RouteImpl) NoGzip() *RouteImpl {
|
|
|
|
return route.LitBeforeMultiline(`gzw, ok := w.(gzipResponseWriter)
|
|
|
|
if ok {
|
|
|
|
w = gzw.ResponseWriter
|
|
|
|
w.Header().Del("Content-Type")
|
|
|
|
w.Header().Del("Content-Encoding")
|
|
|
|
}`)
|
|
|
|
}
|
|
|
|
|
2017-11-10 00:16:15 +00:00
|
|
|
func addRouteGroup(routeGroup *RouteGroup) {
|
|
|
|
routeGroups = append(routeGroups, routeGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
func blankRoute() *RouteImpl {
|
2017-11-12 11:29:23 +00:00
|
|
|
return &RouteImpl{"", "", []string{}, []Runnable{}, nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
func route(fname string, path string, args ...string) *RouteImpl {
|
|
|
|
return &RouteImpl{fname, path, args, []Runnable{}, nil}
|
2017-11-12 07:18:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func View(fname string, path string, args ...string) *RouteImpl {
|
2017-11-12 11:29:23 +00:00
|
|
|
return route(fname, path, args...)
|
2017-11-12 07:18:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func MemberView(fname string, path string, args ...string) *RouteImpl {
|
2017-11-12 11:29:23 +00:00
|
|
|
route := route(fname, path, args...)
|
2017-11-12 07:18:25 +00:00
|
|
|
if !route.hasBefore("SuperModOnly", "AdminOnly") {
|
|
|
|
route.Before("MemberOnly")
|
|
|
|
}
|
|
|
|
return route
|
|
|
|
}
|
|
|
|
|
|
|
|
func ModView(fname string, path string, args ...string) *RouteImpl {
|
2017-11-12 11:29:23 +00:00
|
|
|
route := route(fname, path, args...)
|
2017-11-12 07:18:25 +00:00
|
|
|
if !route.hasBefore("AdminOnly") {
|
|
|
|
route.Before("SuperModOnly")
|
|
|
|
}
|
|
|
|
return route
|
|
|
|
}
|
|
|
|
|
|
|
|
func Action(fname string, path string, args ...string) *RouteImpl {
|
2017-11-12 11:29:23 +00:00
|
|
|
route := route(fname, path, args...)
|
2017-11-12 07:18:25 +00:00
|
|
|
route.Before("NoSessionMismatch")
|
|
|
|
if !route.hasBefore("SuperModOnly", "AdminOnly") {
|
|
|
|
route.Before("MemberOnly")
|
|
|
|
}
|
|
|
|
return route
|
|
|
|
}
|
|
|
|
|
|
|
|
func AnonAction(fname string, path string, args ...string) *RouteImpl {
|
2017-11-12 11:29:23 +00:00
|
|
|
return route(fname, path, args...).Before("ParseForm")
|
2017-11-10 00:16:15 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 12:31:25 +00:00
|
|
|
func Special(fname string, path string, args ...string) *RouteImpl {
|
|
|
|
return route(fname, path, args...).LitBefore("req.URL.Path += extraData")
|
|
|
|
}
|
|
|
|
|
2018-01-14 12:03:20 +00:00
|
|
|
// Make this it's own type to force the user to manipulate methods on it to set parameters
|
|
|
|
type uploadAction struct {
|
|
|
|
Route *RouteImpl
|
|
|
|
}
|
|
|
|
|
|
|
|
func UploadAction(fname string, path string, args ...string) *uploadAction {
|
2017-11-12 11:29:23 +00:00
|
|
|
route := route(fname, path, args...)
|
2017-11-12 07:18:25 +00:00
|
|
|
if !route.hasBefore("SuperModOnly", "AdminOnly") {
|
|
|
|
route.Before("MemberOnly")
|
|
|
|
}
|
2018-01-14 12:03:20 +00:00
|
|
|
return &uploadAction{route}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (action *uploadAction) MaxSizeVar(varName string) *RouteImpl {
|
2018-08-22 02:13:17 +00:00
|
|
|
action.Route.LitBeforeMultiline(`err = common.HandleUploadRoute(w,req,user,` + varName + `)
|
2018-01-14 12:03:20 +00:00
|
|
|
if err != nil {
|
|
|
|
router.handleError(err,w,req,user)
|
|
|
|
return
|
|
|
|
}`)
|
|
|
|
action.Route.Before("NoUploadSessionMismatch")
|
|
|
|
return action.Route
|
2017-11-10 00:16:15 +00:00
|
|
|
}
|