12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package beego
- import (
- "strings"
- "github.com/astaxie/beego/context"
- )
- type PolicyFunc func(*context.Context)
- func (p *ControllerRegister) FindPolicy(cont *context.Context) []PolicyFunc {
- var urlPath = cont.Input.URL()
- if !BConfig.RouterCaseSensitive {
- urlPath = strings.ToLower(urlPath)
- }
- httpMethod := cont.Input.Method()
- isWildcard := false
-
- t, ok := p.policies[httpMethod]
-
- if !ok {
- t, ok = p.policies["*"]
- isWildcard = true
- }
- if ok {
- runObjects := t.Match(urlPath, cont)
- if r, ok := runObjects.([]PolicyFunc); ok {
- return r
- } else if !isWildcard {
-
- t, ok = p.policies["*"]
- if ok {
- runObjects = t.Match(urlPath, cont)
- if r, ok = runObjects.([]PolicyFunc); ok {
- return r
- }
- }
- }
- }
- return nil
- }
- func (p *ControllerRegister) addToPolicy(method, pattern string, r ...PolicyFunc) {
- method = strings.ToUpper(method)
- p.enablePolicy = true
- if !BConfig.RouterCaseSensitive {
- pattern = strings.ToLower(pattern)
- }
- if t, ok := p.policies[method]; ok {
- t.AddRouter(pattern, r)
- } else {
- t := NewTree()
- t.AddRouter(pattern, r)
- p.policies[method] = t
- }
- }
- func Policy(pattern, method string, policy ...PolicyFunc) {
- BeeApp.Handlers.addToPolicy(method, pattern, policy...)
- }
- func (p *ControllerRegister) execPolicy(cont *context.Context, urlPath string) (started bool) {
- if !p.enablePolicy {
- return false
- }
-
- policyList := p.FindPolicy(cont)
- if len(policyList) > 0 {
-
- for _, runPolicy := range policyList {
- runPolicy(cont)
- if cont.ResponseWriter.Started {
- return true
- }
- }
- return false
- }
- return false
- }
|