beego: Refactoring Filter & add comments
Showing
3 changed files
with
58 additions
and
106 deletions
| ... | @@ -140,54 +140,90 @@ func AutoPrefix(prefix string, c ControllerInterface) *App { | ... | @@ -140,54 +140,90 @@ func AutoPrefix(prefix string, c ControllerInterface) *App { |
| 140 | } | 140 | } |
| 141 | 141 | ||
| 142 | // register router for Get method | 142 | // register router for Get method |
| 143 | // usage: | ||
| 144 | // beego.Get("/", func(ctx *context.Context){ | ||
| 145 | // ctx.Output.Body("hello world") | ||
| 146 | // }) | ||
| 143 | func Get(rootpath string, f FilterFunc) *App { | 147 | func Get(rootpath string, f FilterFunc) *App { |
| 144 | BeeApp.Handlers.Get(rootpath, f) | 148 | BeeApp.Handlers.Get(rootpath, f) |
| 145 | return BeeApp | 149 | return BeeApp |
| 146 | } | 150 | } |
| 147 | 151 | ||
| 148 | // register router for Post method | 152 | // register router for Post method |
| 153 | // usage: | ||
| 154 | // beego.Post("/api", func(ctx *context.Context){ | ||
| 155 | // ctx.Output.Body("hello world") | ||
| 156 | // }) | ||
| 149 | func Post(rootpath string, f FilterFunc) *App { | 157 | func Post(rootpath string, f FilterFunc) *App { |
| 150 | BeeApp.Handlers.Post(rootpath, f) | 158 | BeeApp.Handlers.Post(rootpath, f) |
| 151 | return BeeApp | 159 | return BeeApp |
| 152 | } | 160 | } |
| 153 | 161 | ||
| 154 | // register router for Delete method | 162 | // register router for Delete method |
| 163 | // usage: | ||
| 164 | // beego.Delete("/api", func(ctx *context.Context){ | ||
| 165 | // ctx.Output.Body("hello world") | ||
| 166 | // }) | ||
| 155 | func Delete(rootpath string, f FilterFunc) *App { | 167 | func Delete(rootpath string, f FilterFunc) *App { |
| 156 | BeeApp.Handlers.Delete(rootpath, f) | 168 | BeeApp.Handlers.Delete(rootpath, f) |
| 157 | return BeeApp | 169 | return BeeApp |
| 158 | } | 170 | } |
| 159 | 171 | ||
| 160 | // register router for Put method | 172 | // register router for Put method |
| 173 | // usage: | ||
| 174 | // beego.Put("/api", func(ctx *context.Context){ | ||
| 175 | // ctx.Output.Body("hello world") | ||
| 176 | // }) | ||
| 161 | func Put(rootpath string, f FilterFunc) *App { | 177 | func Put(rootpath string, f FilterFunc) *App { |
| 162 | BeeApp.Handlers.Put(rootpath, f) | 178 | BeeApp.Handlers.Put(rootpath, f) |
| 163 | return BeeApp | 179 | return BeeApp |
| 164 | } | 180 | } |
| 165 | 181 | ||
| 166 | // register router for Head method | 182 | // register router for Head method |
| 183 | // usage: | ||
| 184 | // beego.Head("/api", func(ctx *context.Context){ | ||
| 185 | // ctx.Output.Body("hello world") | ||
| 186 | // }) | ||
| 167 | func Head(rootpath string, f FilterFunc) *App { | 187 | func Head(rootpath string, f FilterFunc) *App { |
| 168 | BeeApp.Handlers.Head(rootpath, f) | 188 | BeeApp.Handlers.Head(rootpath, f) |
| 169 | return BeeApp | 189 | return BeeApp |
| 170 | } | 190 | } |
| 171 | 191 | ||
| 172 | // register router for Options method | 192 | // register router for Options method |
| 193 | // usage: | ||
| 194 | // beego.Options("/api", func(ctx *context.Context){ | ||
| 195 | // ctx.Output.Body("hello world") | ||
| 196 | // }) | ||
| 173 | func Options(rootpath string, f FilterFunc) *App { | 197 | func Options(rootpath string, f FilterFunc) *App { |
| 174 | BeeApp.Handlers.Options(rootpath, f) | 198 | BeeApp.Handlers.Options(rootpath, f) |
| 175 | return BeeApp | 199 | return BeeApp |
| 176 | } | 200 | } |
| 177 | 201 | ||
| 178 | // register router for Patch method | 202 | // register router for Patch method |
| 203 | // usage: | ||
| 204 | // beego.Patch("/api", func(ctx *context.Context){ | ||
| 205 | // ctx.Output.Body("hello world") | ||
| 206 | // }) | ||
| 179 | func Patch(rootpath string, f FilterFunc) *App { | 207 | func Patch(rootpath string, f FilterFunc) *App { |
| 180 | BeeApp.Handlers.Patch(rootpath, f) | 208 | BeeApp.Handlers.Patch(rootpath, f) |
| 181 | return BeeApp | 209 | return BeeApp |
| 182 | } | 210 | } |
| 183 | 211 | ||
| 184 | // register router for all method | 212 | // register router for all method |
| 213 | // usage: | ||
| 214 | // beego.Any("/api", func(ctx *context.Context){ | ||
| 215 | // ctx.Output.Body("hello world") | ||
| 216 | // }) | ||
| 185 | func Any(rootpath string, f FilterFunc) *App { | 217 | func Any(rootpath string, f FilterFunc) *App { |
| 186 | BeeApp.Handlers.Any(rootpath, f) | 218 | BeeApp.Handlers.Any(rootpath, f) |
| 187 | return BeeApp | 219 | return BeeApp |
| 188 | } | 220 | } |
| 189 | 221 | ||
| 190 | // register router for own Handler | 222 | // register router for own Handler |
| 223 | // usage: | ||
| 224 | // beego.Handler("/api", func(ctx *context.Context){ | ||
| 225 | // ctx.Output.Body("hello world") | ||
| 226 | // }) | ||
| 191 | func Handler(rootpath string, h http.Handler, options ...interface{}) *App { | 227 | func Handler(rootpath string, h http.Handler, options ...interface{}) *App { |
| 192 | BeeApp.Handlers.Handler(rootpath, h, options...) | 228 | BeeApp.Handlers.Handler(rootpath, h, options...) |
| 193 | return BeeApp | 229 | return BeeApp | ... | ... |
| ... | @@ -6,10 +6,7 @@ | ... | @@ -6,10 +6,7 @@ |
| 6 | 6 | ||
| 7 | package beego | 7 | package beego |
| 8 | 8 | ||
| 9 | import ( | 9 | import "regexp" |
| 10 | "regexp" | ||
| 11 | "strings" | ||
| 12 | ) | ||
| 13 | 10 | ||
| 14 | // FilterRouter defines filter operation before controller handler execution. | 11 | // FilterRouter defines filter operation before controller handler execution. |
| 15 | // it can match patterned url and do filter function when action arrives. | 12 | // it can match patterned url and do filter function when action arrives. |
| ... | @@ -57,103 +54,3 @@ func (mr *FilterRouter) ValidRouter(router string) (bool, map[string]string) { | ... | @@ -57,103 +54,3 @@ func (mr *FilterRouter) ValidRouter(router string) (bool, map[string]string) { |
| 57 | } | 54 | } |
| 58 | return false, nil | 55 | return false, nil |
| 59 | } | 56 | } |
| 60 | |||
| 61 | func buildFilter(pattern string, filter FilterFunc) (*FilterRouter, error) { | ||
| 62 | mr := new(FilterRouter) | ||
| 63 | mr.params = make(map[int]string) | ||
| 64 | mr.filterFunc = filter | ||
| 65 | parts := strings.Split(pattern, "/") | ||
| 66 | j := 0 | ||
| 67 | for i, part := range parts { | ||
| 68 | if strings.HasPrefix(part, ":") { | ||
| 69 | expr := "(.*)" | ||
| 70 | //a user may choose to override the default expression | ||
| 71 | // similar to expressjs: ‘/user/:id([0-9]+)’ | ||
| 72 | if index := strings.Index(part, "("); index != -1 { | ||
| 73 | expr = part[index:] | ||
| 74 | part = part[:index] | ||
| 75 | //match /user/:id:int ([0-9]+) | ||
| 76 | //match /post/:username:string ([\w]+) | ||
| 77 | } else if lindex := strings.LastIndex(part, ":"); lindex != 0 { | ||
| 78 | switch part[lindex:] { | ||
| 79 | case ":int": | ||
| 80 | expr = "([0-9]+)" | ||
| 81 | part = part[:lindex] | ||
| 82 | case ":string": | ||
| 83 | expr = `([\w]+)` | ||
| 84 | part = part[:lindex] | ||
| 85 | } | ||
| 86 | } | ||
| 87 | mr.params[j] = part | ||
| 88 | parts[i] = expr | ||
| 89 | j++ | ||
| 90 | } | ||
| 91 | if strings.HasPrefix(part, "*") { | ||
| 92 | expr := "(.*)" | ||
| 93 | if part == "*.*" { | ||
| 94 | mr.params[j] = ":path" | ||
| 95 | parts[i] = "([^.]+).([^.]+)" | ||
| 96 | j++ | ||
| 97 | mr.params[j] = ":ext" | ||
| 98 | j++ | ||
| 99 | } else { | ||
| 100 | mr.params[j] = ":splat" | ||
| 101 | parts[i] = expr | ||
| 102 | j++ | ||
| 103 | } | ||
| 104 | } | ||
| 105 | //url like someprefix:id(xxx).html | ||
| 106 | if strings.Contains(part, ":") && strings.Contains(part, "(") && strings.Contains(part, ")") { | ||
| 107 | var out []rune | ||
| 108 | var start bool | ||
| 109 | var startexp bool | ||
| 110 | var param []rune | ||
| 111 | var expt []rune | ||
| 112 | for _, v := range part { | ||
| 113 | if start { | ||
| 114 | if v != '(' { | ||
| 115 | param = append(param, v) | ||
| 116 | continue | ||
| 117 | } | ||
| 118 | } | ||
| 119 | if startexp { | ||
| 120 | if v != ')' { | ||
| 121 | expt = append(expt, v) | ||
| 122 | continue | ||
| 123 | } | ||
| 124 | } | ||
| 125 | if v == ':' { | ||
| 126 | param = make([]rune, 0) | ||
| 127 | param = append(param, ':') | ||
| 128 | start = true | ||
| 129 | } else if v == '(' { | ||
| 130 | startexp = true | ||
| 131 | start = false | ||
| 132 | mr.params[j] = string(param) | ||
| 133 | j++ | ||
| 134 | expt = make([]rune, 0) | ||
| 135 | expt = append(expt, '(') | ||
| 136 | } else if v == ')' { | ||
| 137 | startexp = false | ||
| 138 | expt = append(expt, ')') | ||
| 139 | out = append(out, expt...) | ||
| 140 | } else { | ||
| 141 | out = append(out, v) | ||
| 142 | } | ||
| 143 | } | ||
| 144 | parts[i] = string(out) | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | if j != 0 { | ||
| 149 | pattern = strings.Join(parts, "/") | ||
| 150 | regex, regexErr := regexp.Compile(pattern) | ||
| 151 | if regexErr != nil { | ||
| 152 | return nil, regexErr | ||
| 153 | } | ||
| 154 | mr.regex = regex | ||
| 155 | mr.hasregex = true | ||
| 156 | } | ||
| 157 | mr.pattern = pattern | ||
| 158 | return mr, nil | ||
| 159 | } | ... | ... |
| ... | @@ -448,7 +448,7 @@ func (p *ControllerRegistor) AddAutoPrefix(prefix string, c ControllerInterface) | ... | @@ -448,7 +448,7 @@ func (p *ControllerRegistor) AddAutoPrefix(prefix string, c ControllerInterface) |
| 448 | // [Deprecated] use InsertFilter. | 448 | // [Deprecated] use InsertFilter. |
| 449 | // Add FilterFunc with pattern for action. | 449 | // Add FilterFunc with pattern for action. |
| 450 | func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc) error { | 450 | func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc) error { |
| 451 | mr, err := buildFilter(pattern, filter) | 451 | mr, err := p.buildFilter(pattern, filter) |
| 452 | if err != nil { | 452 | if err != nil { |
| 453 | return err | 453 | return err |
| 454 | } | 454 | } |
| ... | @@ -471,7 +471,7 @@ func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc | ... | @@ -471,7 +471,7 @@ func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc |
| 471 | 471 | ||
| 472 | // Add a FilterFunc with pattern rule and action constant. | 472 | // Add a FilterFunc with pattern rule and action constant. |
| 473 | func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter FilterFunc) error { | 473 | func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter FilterFunc) error { |
| 474 | mr, err := buildFilter(pattern, filter) | 474 | mr, err := p.buildFilter(pattern, filter) |
| 475 | if err != nil { | 475 | if err != nil { |
| 476 | return err | 476 | return err |
| 477 | } | 477 | } |
| ... | @@ -480,6 +480,25 @@ func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter Filter | ... | @@ -480,6 +480,25 @@ func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter Filter |
| 480 | return nil | 480 | return nil |
| 481 | } | 481 | } |
| 482 | 482 | ||
| 483 | func (p *ControllerRegistor) buildFilter(pattern string, filter FilterFunc) (*FilterRouter, error) { | ||
| 484 | mr := new(FilterRouter) | ||
| 485 | mr.params = make(map[int]string) | ||
| 486 | mr.filterFunc = filter | ||
| 487 | j, params, parts := p.splitRoute(pattern) | ||
| 488 | if j != 0 { | ||
| 489 | pattern = strings.Join(parts, "/") | ||
| 490 | regex, regexErr := regexp.Compile(pattern) | ||
| 491 | if regexErr != nil { | ||
| 492 | return nil, regexErr | ||
| 493 | } | ||
| 494 | mr.regex = regex | ||
| 495 | mr.hasregex = true | ||
| 496 | } | ||
| 497 | mr.params = params | ||
| 498 | mr.pattern = pattern | ||
| 499 | return mr, nil | ||
| 500 | } | ||
| 501 | |||
| 483 | // UrlFor does another controller handler in this request function. | 502 | // UrlFor does another controller handler in this request function. |
| 484 | // it can access any controller method. | 503 | // it can access any controller method. |
| 485 | func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string { | 504 | func (p *ControllerRegistor) UrlFor(endpoint string, values ...string) string { | ... | ... |
-
Please register or sign in to post a comment