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
|
2018-11-12 09:23:36 +00:00
|
|
|
Action bool
|
|
|
|
NoHead bool
|
2017-11-12 11:29:23 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-10-20 01:42:19 +00:00
|
|
|
func (r *RouteImpl) Before(items ...string) *RouteImpl {
|
2017-11-10 03:33:11 +00:00
|
|
|
for _, item := range items {
|
2019-10-20 01:42:19 +00:00
|
|
|
r.RunBefore = append(r.RunBefore, Runnable{item, false})
|
2017-11-10 03:33:11 +00:00
|
|
|
}
|
2019-10-20 01:42:19 +00:00
|
|
|
return r
|
2017-11-10 03:33:11 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 01:42:19 +00:00
|
|
|
func (r *RouteImpl) LitBefore(items ...string) *RouteImpl {
|
2017-11-10 03:33:11 +00:00
|
|
|
for _, item := range items {
|
2019-10-20 01:42:19 +00:00
|
|
|
r.RunBefore = append(r.RunBefore, Runnable{item, true})
|
2017-11-10 00:16:15 +00:00
|
|
|
}
|
2019-10-20 01:42:19 +00:00
|
|
|
return r
|
2017-11-10 00:16:15 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 01:42:19 +00:00
|
|
|
func (r *RouteImpl) LitBeforeMultiline(items ...string) *RouteImpl {
|
2018-08-22 02:13:17 +00:00
|
|
|
for _, item := range items {
|
|
|
|
for _, line := range strings.Split(item, "\n") {
|
2019-10-20 01:42:19 +00:00
|
|
|
r.LitBefore(strings.TrimSpace(line))
|
2018-08-22 02:13:17 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-20 01:42:19 +00:00
|
|
|
return r
|
2018-08-22 02:13:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 01:42:19 +00:00
|
|
|
func (r *RouteImpl) hasBefore(items ...string) bool {
|
2017-11-12 07:18:25 +00:00
|
|
|
for _, item := range items {
|
2019-10-20 01:42:19 +00:00
|
|
|
if r.hasBeforeItem(item) {
|
2017-11-12 11:29:23 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-10-20 01:42:19 +00:00
|
|
|
func (r *RouteImpl) hasBeforeItem(item string) bool {
|
|
|
|
for _, before := range r.RunBefore {
|
2017-11-12 11:29:23 +00:00
|
|
|
if before.Contents == item {
|
|
|
|
return true
|
2017-11-12 07:18:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-10-20 01:42:19 +00:00
|
|
|
func (r *RouteImpl) NoGzip() *RouteImpl {
|
|
|
|
return r.LitBeforeMultiline(`gzw, ok := w.(c.GzipResponseWriter)
|
2018-08-22 02:13:17 +00:00
|
|
|
if ok {
|
|
|
|
w = gzw.ResponseWriter
|
2020-02-26 10:34:38 +00:00
|
|
|
h := w.Header()
|
|
|
|
h.Del("Content-Type")
|
|
|
|
h.Del("Content-Encoding")
|
2018-08-22 02:13:17 +00:00
|
|
|
}`)
|
|
|
|
}
|
|
|
|
|
2019-10-20 01:42:19 +00:00
|
|
|
func (r *RouteImpl) NoHeader() *RouteImpl {
|
|
|
|
r.NoHead = true
|
|
|
|
return r
|
2017-11-10 00:16:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func blankRoute() *RouteImpl {
|
2018-11-12 09:23:36 +00:00
|
|
|
return &RouteImpl{"", "", false, false, []string{}, []Runnable{}, nil}
|
2017-11-12 11:29:23 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 10:34:38 +00:00
|
|
|
func route(fname, path string, action, special bool, args ...string) *RouteImpl {
|
2018-11-12 09:23:36 +00:00
|
|
|
return &RouteImpl{fname, path, action, special, args, []Runnable{}, nil}
|
2017-11-12 07:18:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 10:34:38 +00:00
|
|
|
func View(fname, path string, args ...string) *RouteImpl {
|
2018-11-12 09:23:36 +00:00
|
|
|
return route(fname, path, false, false, args...)
|
2017-11-12 07:18:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 10:34:38 +00:00
|
|
|
func MView(fname, path string, args ...string) *RouteImpl {
|
2019-07-12 08:33:25 +00:00
|
|
|
route := route(fname, path, false, false, args...)
|
|
|
|
if !route.hasBefore("SuperModOnly", "AdminOnly") {
|
|
|
|
route.Before("MemberOnly")
|
|
|
|
}
|
|
|
|
return route
|
|
|
|
}
|
|
|
|
|
2020-02-26 10:34:38 +00:00
|
|
|
func MemberView(fname, path string, args ...string) *RouteImpl {
|
2018-11-12 09:23:36 +00:00
|
|
|
route := route(fname, path, false, false, args...)
|
2017-11-12 07:18:25 +00:00
|
|
|
if !route.hasBefore("SuperModOnly", "AdminOnly") {
|
|
|
|
route.Before("MemberOnly")
|
|
|
|
}
|
|
|
|
return route
|
|
|
|
}
|
|
|
|
|
2020-02-26 10:34:38 +00:00
|
|
|
func ModView(fname, path string, args ...string) *RouteImpl {
|
2018-11-12 09:23:36 +00:00
|
|
|
route := route(fname, path, false, false, args...)
|
2017-11-12 07:18:25 +00:00
|
|
|
if !route.hasBefore("AdminOnly") {
|
|
|
|
route.Before("SuperModOnly")
|
|
|
|
}
|
|
|
|
return route
|
|
|
|
}
|
|
|
|
|
2020-02-26 10:34:38 +00:00
|
|
|
func Action(fname, path string, args ...string) *RouteImpl {
|
2018-11-12 09:23:36 +00:00
|
|
|
route := route(fname, path, true, false, args...)
|
2017-11-12 07:18:25 +00:00
|
|
|
route.Before("NoSessionMismatch")
|
|
|
|
if !route.hasBefore("SuperModOnly", "AdminOnly") {
|
|
|
|
route.Before("MemberOnly")
|
|
|
|
}
|
|
|
|
return route
|
|
|
|
}
|
|
|
|
|
2020-02-26 10:34:38 +00:00
|
|
|
func AnonAction(fname, path string, args ...string) *RouteImpl {
|
2018-11-12 09:23:36 +00:00
|
|
|
return route(fname, path, true, false, args...).Before("ParseForm")
|
2017-11-10 00:16:15 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 10:34:38 +00:00
|
|
|
func Special(fname, path string, args ...string) *RouteImpl {
|
2018-11-12 09:23:36 +00:00
|
|
|
return route(fname, path, false, true, args...).LitBefore("req.URL.Path += extraData")
|
2018-01-18 12:31:25 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-02-26 10:34:38 +00:00
|
|
|
func UploadAction(fname, path string, args ...string) *uploadAction {
|
2018-11-12 09:23:36 +00:00
|
|
|
route := route(fname, path, true, false, 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}
|
|
|
|
}
|
|
|
|
|
2020-03-09 05:16:44 +00:00
|
|
|
func (a *uploadAction) MaxSizeVar(varName string) *RouteImpl {
|
|
|
|
a.Route.LitBeforeMultiline(`err = c.HandleUploadRoute(w,req,user,` + varName + `)
|
2018-01-14 12:03:20 +00:00
|
|
|
if err != nil {
|
2018-11-12 09:23:36 +00:00
|
|
|
return err
|
2018-01-14 12:03:20 +00:00
|
|
|
}`)
|
2020-03-09 05:16:44 +00:00
|
|
|
a.Route.Before("NoUploadSessionMismatch")
|
|
|
|
return a.Route
|
2017-11-10 00:16:15 +00:00
|
|
|
}
|
2019-07-12 08:33:25 +00:00
|
|
|
|
|
|
|
type RouteSet struct {
|
2019-10-20 01:42:19 +00:00
|
|
|
Name string
|
|
|
|
Path string
|
2019-07-12 08:33:25 +00:00
|
|
|
Items []*RouteImpl
|
|
|
|
}
|
|
|
|
|
2020-02-26 10:34:38 +00:00
|
|
|
func Set(name, path string, routes ...*RouteImpl) RouteSet {
|
2019-07-12 08:33:25 +00:00
|
|
|
return RouteSet{name, path, routes}
|
|
|
|
}
|