beego: update namespace
Showing
4 changed files
with
74 additions
and
5 deletions
| ... | @@ -218,9 +218,9 @@ func profIndex(rw http.ResponseWriter, r *http.Request) { | ... | @@ -218,9 +218,9 @@ func profIndex(rw http.ResponseWriter, r *http.Request) { |
| 218 | func healthcheck(rw http.ResponseWriter, req *http.Request) { | 218 | func healthcheck(rw http.ResponseWriter, req *http.Request) { |
| 219 | for name, h := range toolbox.AdminCheckList { | 219 | for name, h := range toolbox.AdminCheckList { |
| 220 | if err := h.Check(); err != nil { | 220 | if err := h.Check(); err != nil { |
| 221 | fmt.Fprintf(rw, "%s : ok\n", name) | ||
| 222 | } else { | ||
| 223 | fmt.Fprintf(rw, "%s : %s\n", name, err.Error()) | 221 | fmt.Fprintf(rw, "%s : %s\n", name, err.Error()) |
| 222 | } else { | ||
| 223 | fmt.Fprintf(rw, "%s : ok\n", name) | ||
| 224 | } | 224 | } |
| 225 | } | 225 | } |
| 226 | } | 226 | } | ... | ... |
| ... | @@ -161,7 +161,8 @@ func (input *BeegoInput) IsUpload() bool { | ... | @@ -161,7 +161,8 @@ func (input *BeegoInput) IsUpload() bool { |
| 161 | func (input *BeegoInput) IP() string { | 161 | func (input *BeegoInput) IP() string { |
| 162 | ips := input.Proxy() | 162 | ips := input.Proxy() |
| 163 | if len(ips) > 0 && ips[0] != "" { | 163 | if len(ips) > 0 && ips[0] != "" { |
| 164 | return ips[0] | 164 | rip := strings.Split(ips[0], ":") |
| 165 | return rip[0] | ||
| 165 | } | 166 | } |
| 166 | ip := strings.Split(input.Request.RemoteAddr, ":") | 167 | ip := strings.Split(input.Request.RemoteAddr, ":") |
| 167 | if len(ip) > 0 { | 168 | if len(ip) > 0 { | ... | ... |
| ... | @@ -14,12 +14,14 @@ import ( | ... | @@ -14,12 +14,14 @@ import ( |
| 14 | 14 | ||
| 15 | type namespaceCond func(*beecontext.Context) bool | 15 | type namespaceCond func(*beecontext.Context) bool |
| 16 | 16 | ||
| 17 | // Namespace is store all the info | ||
| 17 | type Namespace struct { | 18 | type Namespace struct { |
| 18 | prefix string | 19 | prefix string |
| 19 | condition namespaceCond | 20 | condition namespaceCond |
| 20 | handlers *ControllerRegistor | 21 | handlers *ControllerRegistor |
| 21 | } | 22 | } |
| 22 | 23 | ||
| 24 | // get new Namespace | ||
| 23 | func NewNamespace(prefix string) *Namespace { | 25 | func NewNamespace(prefix string) *Namespace { |
| 24 | cr := NewControllerRegistor() | 26 | cr := NewControllerRegistor() |
| 25 | return &Namespace{ | 27 | return &Namespace{ |
| ... | @@ -28,11 +30,30 @@ func NewNamespace(prefix string) *Namespace { | ... | @@ -28,11 +30,30 @@ func NewNamespace(prefix string) *Namespace { |
| 28 | } | 30 | } |
| 29 | } | 31 | } |
| 30 | 32 | ||
| 33 | // set condtion function | ||
| 34 | // if cond return true can run this namespace, else can't | ||
| 35 | // usage: | ||
| 36 | // ns.Cond(func (ctx *context.Context) bool{ | ||
| 37 | // if ctx.Input.Domain() == "api.beego.me" { | ||
| 38 | // return true | ||
| 39 | // } | ||
| 40 | // return false | ||
| 41 | // }) | ||
| 31 | func (n *Namespace) Cond(cond namespaceCond) *Namespace { | 42 | func (n *Namespace) Cond(cond namespaceCond) *Namespace { |
| 32 | n.condition = cond | 43 | n.condition = cond |
| 33 | return n | 44 | return n |
| 34 | } | 45 | } |
| 35 | 46 | ||
| 47 | // add filter in the Namespace | ||
| 48 | // action has before & after | ||
| 49 | // FilterFunc | ||
| 50 | // usage: | ||
| 51 | // Filter("before", func (ctx *context.Context){ | ||
| 52 | // _, ok := ctx.Input.Session("uid").(int) | ||
| 53 | // if !ok && ctx.Request.RequestURI != "/login" { | ||
| 54 | // ctx.Redirect(302, "/login") | ||
| 55 | // } | ||
| 56 | // }) | ||
| 36 | func (n *Namespace) Filter(action string, filter FilterFunc) *Namespace { | 57 | func (n *Namespace) Filter(action string, filter FilterFunc) *Namespace { |
| 37 | if action == "before" { | 58 | if action == "before" { |
| 38 | action = "BeforeRouter" | 59 | action = "BeforeRouter" |
| ... | @@ -43,71 +64,115 @@ func (n *Namespace) Filter(action string, filter FilterFunc) *Namespace { | ... | @@ -43,71 +64,115 @@ func (n *Namespace) Filter(action string, filter FilterFunc) *Namespace { |
| 43 | return n | 64 | return n |
| 44 | } | 65 | } |
| 45 | 66 | ||
| 67 | // same as beego.Rourer | ||
| 68 | // refer: https://godoc.org/github.com/astaxie/beego#Router | ||
| 46 | func (n *Namespace) Router(rootpath string, c ControllerInterface, mappingMethods ...string) *Namespace { | 69 | func (n *Namespace) Router(rootpath string, c ControllerInterface, mappingMethods ...string) *Namespace { |
| 47 | n.handlers.Add(rootpath, c, mappingMethods...) | 70 | n.handlers.Add(rootpath, c, mappingMethods...) |
| 48 | return n | 71 | return n |
| 49 | } | 72 | } |
| 50 | 73 | ||
| 74 | // same as beego.AutoRouter | ||
| 75 | // refer: https://godoc.org/github.com/astaxie/beego#AutoRouter | ||
| 51 | func (n *Namespace) AutoRouter(c ControllerInterface) *Namespace { | 76 | func (n *Namespace) AutoRouter(c ControllerInterface) *Namespace { |
| 52 | n.handlers.AddAuto(c) | 77 | n.handlers.AddAuto(c) |
| 53 | return n | 78 | return n |
| 54 | } | 79 | } |
| 55 | 80 | ||
| 81 | // same as beego.AutoPrefix | ||
| 82 | // refer: https://godoc.org/github.com/astaxie/beego#AutoPrefix | ||
| 56 | func (n *Namespace) AutoPrefix(prefix string, c ControllerInterface) *Namespace { | 83 | func (n *Namespace) AutoPrefix(prefix string, c ControllerInterface) *Namespace { |
| 57 | n.handlers.AddAutoPrefix(prefix, c) | 84 | n.handlers.AddAutoPrefix(prefix, c) |
| 58 | return n | 85 | return n |
| 59 | } | 86 | } |
| 60 | 87 | ||
| 88 | // same as beego.Get | ||
| 89 | // refer: https://godoc.org/github.com/astaxie/beego#Get | ||
| 61 | func (n *Namespace) Get(rootpath string, f FilterFunc) *Namespace { | 90 | func (n *Namespace) Get(rootpath string, f FilterFunc) *Namespace { |
| 62 | n.handlers.Get(rootpath, f) | 91 | n.handlers.Get(rootpath, f) |
| 63 | return n | 92 | return n |
| 64 | } | 93 | } |
| 65 | 94 | ||
| 95 | // same as beego.Post | ||
| 96 | // refer: https://godoc.org/github.com/astaxie/beego#Post | ||
| 66 | func (n *Namespace) Post(rootpath string, f FilterFunc) *Namespace { | 97 | func (n *Namespace) Post(rootpath string, f FilterFunc) *Namespace { |
| 67 | n.handlers.Post(rootpath, f) | 98 | n.handlers.Post(rootpath, f) |
| 68 | return n | 99 | return n |
| 69 | } | 100 | } |
| 70 | 101 | ||
| 102 | // same as beego.Delete | ||
| 103 | // refer: https://godoc.org/github.com/astaxie/beego#Delete | ||
| 71 | func (n *Namespace) Delete(rootpath string, f FilterFunc) *Namespace { | 104 | func (n *Namespace) Delete(rootpath string, f FilterFunc) *Namespace { |
| 72 | n.handlers.Delete(rootpath, f) | 105 | n.handlers.Delete(rootpath, f) |
| 73 | return n | 106 | return n |
| 74 | } | 107 | } |
| 75 | 108 | ||
| 109 | // same as beego.Put | ||
| 110 | // refer: https://godoc.org/github.com/astaxie/beego#Put | ||
| 76 | func (n *Namespace) Put(rootpath string, f FilterFunc) *Namespace { | 111 | func (n *Namespace) Put(rootpath string, f FilterFunc) *Namespace { |
| 77 | n.handlers.Put(rootpath, f) | 112 | n.handlers.Put(rootpath, f) |
| 78 | return n | 113 | return n |
| 79 | } | 114 | } |
| 80 | 115 | ||
| 116 | // same as beego.Head | ||
| 117 | // refer: https://godoc.org/github.com/astaxie/beego#Head | ||
| 81 | func (n *Namespace) Head(rootpath string, f FilterFunc) *Namespace { | 118 | func (n *Namespace) Head(rootpath string, f FilterFunc) *Namespace { |
| 82 | n.handlers.Head(rootpath, f) | 119 | n.handlers.Head(rootpath, f) |
| 83 | return n | 120 | return n |
| 84 | } | 121 | } |
| 85 | 122 | ||
| 123 | // same as beego.Options | ||
| 124 | // refer: https://godoc.org/github.com/astaxie/beego#Options | ||
| 86 | func (n *Namespace) Options(rootpath string, f FilterFunc) *Namespace { | 125 | func (n *Namespace) Options(rootpath string, f FilterFunc) *Namespace { |
| 87 | n.handlers.Options(rootpath, f) | 126 | n.handlers.Options(rootpath, f) |
| 88 | return n | 127 | return n |
| 89 | } | 128 | } |
| 90 | 129 | ||
| 130 | // same as beego.Patch | ||
| 131 | // refer: https://godoc.org/github.com/astaxie/beego#Patch | ||
| 91 | func (n *Namespace) Patch(rootpath string, f FilterFunc) *Namespace { | 132 | func (n *Namespace) Patch(rootpath string, f FilterFunc) *Namespace { |
| 92 | n.handlers.Patch(rootpath, f) | 133 | n.handlers.Patch(rootpath, f) |
| 93 | return n | 134 | return n |
| 94 | } | 135 | } |
| 95 | 136 | ||
| 137 | // same as beego.Any | ||
| 138 | // refer: https://godoc.org/github.com/astaxie/beego#Any | ||
| 96 | func (n *Namespace) Any(rootpath string, f FilterFunc) *Namespace { | 139 | func (n *Namespace) Any(rootpath string, f FilterFunc) *Namespace { |
| 97 | n.handlers.Any(rootpath, f) | 140 | n.handlers.Any(rootpath, f) |
| 98 | return n | 141 | return n |
| 99 | } | 142 | } |
| 100 | 143 | ||
| 144 | // same as beego.Handler | ||
| 145 | // refer: https://godoc.org/github.com/astaxie/beego#Handler | ||
| 101 | func (n *Namespace) Handler(rootpath string, h http.Handler) *Namespace { | 146 | func (n *Namespace) Handler(rootpath string, h http.Handler) *Namespace { |
| 102 | n.handlers.Handler(rootpath, h) | 147 | n.handlers.Handler(rootpath, h) |
| 103 | return n | 148 | return n |
| 104 | } | 149 | } |
| 105 | 150 | ||
| 106 | func (n *Namespace) Namespace(ns *Namespace) *Namespace { | 151 | // nest Namespace |
| 107 | n.handlers.Handler(ns.prefix, ns, true) | 152 | // usage: |
| 153 | //ns := beego.NewNamespace(“/v1”). | ||
| 154 | //Namespace( | ||
| 155 | // beego.NewNamespace("/shop"). | ||
| 156 | // Get("/:id", func(ctx *context.Context) { | ||
| 157 | // ctx.Output.Body([]byte("shopinfo")) | ||
| 158 | // }), | ||
| 159 | // beego.NewNamespace("/order"). | ||
| 160 | // Get("/:id", func(ctx *context.Context) { | ||
| 161 | // ctx.Output.Body([]byte("orderinfo")) | ||
| 162 | // }), | ||
| 163 | // beego.NewNamespace("/crm"). | ||
| 164 | // Get("/:id", func(ctx *context.Context) { | ||
| 165 | // ctx.Output.Body([]byte("crminfo")) | ||
| 166 | // }), | ||
| 167 | //) | ||
| 168 | func (n *Namespace) Namespace(ns ...*Namespace) *Namespace { | ||
| 169 | for _, ni := range ns { | ||
| 170 | n.handlers.Handler(ni.prefix, ni, true) | ||
| 171 | } | ||
| 108 | return n | 172 | return n |
| 109 | } | 173 | } |
| 110 | 174 | ||
| 175 | // Namespace implement the http.Handler | ||
| 111 | func (n *Namespace) ServeHTTP(rw http.ResponseWriter, r *http.Request) { | 176 | func (n *Namespace) ServeHTTP(rw http.ResponseWriter, r *http.Request) { |
| 112 | //trim the preifix from URL.Path | 177 | //trim the preifix from URL.Path |
| 113 | r.URL.Path = strings.TrimPrefix(r.URL.Path, n.prefix) | 178 | r.URL.Path = strings.TrimPrefix(r.URL.Path, n.prefix) |
| ... | @@ -130,6 +195,8 @@ func (n *Namespace) ServeHTTP(rw http.ResponseWriter, r *http.Request) { | ... | @@ -130,6 +195,8 @@ func (n *Namespace) ServeHTTP(rw http.ResponseWriter, r *http.Request) { |
| 130 | n.handlers.ServeHTTP(rw, r) | 195 | n.handlers.ServeHTTP(rw, r) |
| 131 | } | 196 | } |
| 132 | 197 | ||
| 198 | // register Namespace into beego.Handler | ||
| 199 | // support multi Namespace | ||
| 133 | func AddNamespace(nl ...*Namespace) { | 200 | func AddNamespace(nl ...*Namespace) { |
| 134 | for _, n := range nl { | 201 | for _, n := range nl { |
| 135 | Handler(n.prefix, n, true) | 202 | Handler(n.prefix, n, true) | ... | ... |
| ... | @@ -480,6 +480,7 @@ func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter Filter | ... | @@ -480,6 +480,7 @@ func (p *ControllerRegistor) InsertFilter(pattern string, pos int, filter Filter |
| 480 | return nil | 480 | return nil |
| 481 | } | 481 | } |
| 482 | 482 | ||
| 483 | // build the Filter by pattern | ||
| 483 | func (p *ControllerRegistor) buildFilter(pattern string, filter FilterFunc) (*FilterRouter, error) { | 484 | func (p *ControllerRegistor) buildFilter(pattern string, filter FilterFunc) (*FilterRouter, error) { |
| 484 | mr := new(FilterRouter) | 485 | mr := new(FilterRouter) |
| 485 | mr.params = make(map[int]string) | 486 | mr.params = make(map[int]string) | ... | ... |
-
Please register or sign in to post a comment