github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/router_gen/route_impl.go (about) 1 package main 2 3 import "strings" 4 5 type RouteImpl struct { 6 Name string 7 Path string 8 Action bool 9 NoHead bool 10 Vars []string 11 RunBefore []Runnable 12 13 Parent *RouteGroup 14 } 15 16 type Runnable struct { 17 Contents string 18 Literal bool 19 } 20 21 func (r *RouteImpl) Before(items ...string) *RouteImpl { 22 for _, item := range items { 23 r.RunBefore = append(r.RunBefore, Runnable{item, false}) 24 } 25 return r 26 } 27 28 func (r *RouteImpl) LitBefore(items ...string) *RouteImpl { 29 for _, item := range items { 30 r.RunBefore = append(r.RunBefore, Runnable{item, true}) 31 } 32 return r 33 } 34 35 func (r *RouteImpl) LitBeforeMultiline(items ...string) *RouteImpl { 36 for _, item := range items { 37 for _, line := range strings.Split(item, "\n") { 38 r.LitBefore(strings.TrimSpace(line)) 39 } 40 } 41 return r 42 } 43 44 func (r *RouteImpl) hasBefore(items ...string) bool { 45 for _, item := range items { 46 if r.hasBeforeItem(item) { 47 return true 48 } 49 } 50 return false 51 } 52 53 func (r *RouteImpl) hasBeforeItem(item string) bool { 54 for _, before := range r.RunBefore { 55 if before.Contents == item { 56 return true 57 } 58 } 59 return false 60 } 61 62 func (r *RouteImpl) NoGzip() *RouteImpl { 63 return r.LitBefore("w = r.responseWriter(w)") 64 } 65 66 func (r *RouteImpl) NoHeader() *RouteImpl { 67 r.NoHead = true 68 return r 69 } 70 71 func blankRoute() *RouteImpl { 72 return &RouteImpl{"", "", false, false, []string{}, []Runnable{}, nil} 73 } 74 75 func route(fname, path string, action, special bool, args ...string) *RouteImpl { 76 return &RouteImpl{fname, path, action, special, args, []Runnable{}, nil} 77 } 78 79 func View(fname, path string, args ...string) *RouteImpl { 80 return route(fname, path, false, false, args...) 81 } 82 83 func MView(fname, path string, args ...string) *RouteImpl { 84 route := route(fname, path, false, false, args...) 85 if !route.hasBefore("SuperModOnly", "AdminOnly") { 86 route.Before("MemberOnly") 87 } 88 return route 89 } 90 91 func MemberView(fname, path string, args ...string) *RouteImpl { 92 route := route(fname, path, false, false, args...) 93 if !route.hasBefore("SuperModOnly", "AdminOnly") { 94 route.Before("MemberOnly") 95 } 96 return route 97 } 98 99 func ModView(fname, path string, args ...string) *RouteImpl { 100 route := route(fname, path, false, false, args...) 101 if !route.hasBefore("AdminOnly") { 102 route.Before("SuperModOnly") 103 } 104 return route 105 } 106 107 func Action(fname, path string, args ...string) *RouteImpl { 108 route := route(fname, path, true, false, args...) 109 route.Before("NoSessionMismatch") 110 if !route.hasBefore("SuperModOnly", "AdminOnly") { 111 route.Before("MemberOnly") 112 } 113 return route 114 } 115 116 func AnonAction(fname, path string, args ...string) *RouteImpl { 117 return route(fname, path, true, false, args...).Before("ParseForm") 118 } 119 120 func Special(fname, path string, args ...string) *RouteImpl { 121 return route(fname, path, false, true, args...).LitBefore("req.URL.Path += extraData") 122 } 123 124 // Make this it's own type to force the user to manipulate methods on it to set parameters 125 type uploadAction struct { 126 Route *RouteImpl 127 } 128 129 func UploadAction(fname, path string, args ...string) *uploadAction { 130 route := route(fname, path, true, false, args...) 131 if !route.hasBefore("SuperModOnly", "AdminOnly") { 132 route.Before("MemberOnly") 133 } 134 return &uploadAction{route} 135 } 136 137 func (a *uploadAction) MaxSizeVar(varName string) *RouteImpl { 138 a.Route.LitBeforeMultiline(`err = c.HandleUploadRoute(w,req,user,` + varName + `) 139 if err != nil { 140 return err 141 }`) 142 a.Route.Before("NoUploadSessionMismatch") 143 return a.Route 144 } 145 146 type RouteSet struct { 147 Name string 148 Path string 149 Items []*RouteImpl 150 } 151 152 func Set(name, path string, routes ...*RouteImpl) RouteSet { 153 return RouteSet{name, path, routes} 154 }