1
0
mirror of https://github.com/astaxie/beego.git synced 2025-07-03 16:40:18 +00:00

beego: support namespace

ns := beego.NewNamespace("/v1/api/")
ns.Cond(func(ctx *context.Context)bool{
	    if ctx.Input.Domain() == "www.beego.me" {
	    	return true
	    }
	    return false
	})
.Filter("before", Authenticate)
.Router("/order",	&admin.OrderController{})
.Get("/version",func (ctx *context.Context) {
	ctx.Output.Body([]byte("1.0.0"))
})
.Post("/login",func (ctx *context.Context) {
	if ctx.Query("username") == "admin" && ctx.Query("username") ==
"password" {

	}
})
.Namespace(
	NewNamespace("/shop").
		Get("/order/:id", func(ctx *context.Context) {
		ctx.Output.Body([]byte(ctx.Input.Param(":id")))
	}),
)
This commit is contained in:
astaxie
2014-05-16 23:47:15 +08:00
parent 2ed9b2bffd
commit e657dcfd5f
5 changed files with 294 additions and 5 deletions

View File

@ -69,6 +69,7 @@ type controllerInfo struct {
handler http.Handler
runfunction FilterFunc
routerType int
isPrefix bool
}
// ControllerRegistor containers registered router rules, controller handlers and filters.
@ -277,11 +278,16 @@ func (p *ControllerRegistor) AddMethod(method, pattern string, f FilterFunc) {
}
}
func (p *ControllerRegistor) Handler(pattern string, h http.Handler) {
func (p *ControllerRegistor) Handler(pattern string, h http.Handler, options ...interface{}) {
paramnums, params, parts := p.splitRoute(pattern)
route := &controllerInfo{}
route.routerType = routerTypeHandler
route.handler = h
if len(options) > 0 {
if v, ok := options[0].(bool); ok {
route.isPrefix = v
}
}
if paramnums == 0 {
route.pattern = pattern
p.fixrouters = append(p.fixrouters, route)
@ -446,6 +452,7 @@ func (p *ControllerRegistor) AddFilter(pattern, action string, filter FilterFunc
if err != nil {
return err
}
switch action {
case "BeforeRouter":
p.filters[BeforeRouter] = append(p.filters[BeforeRouter], mr)
@ -760,6 +767,14 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
break
}
}
if route.routerType == routerTypeHandler && route.isPrefix &&
strings.HasPrefix(requestPath, route.pattern) {
routerInfo = route
runrouter = route.controllerType
findrouter = true
break
}
}
}