mirror of
https://github.com/astaxie/beego.git
synced 2025-07-04 09:20:18 +00:00
phase #1
This commit is contained in:
42
router.go
42
router.go
@ -29,6 +29,8 @@ import (
|
||||
|
||||
beecontext "github.com/astaxie/beego/context"
|
||||
"github.com/astaxie/beego/logs"
|
||||
"github.com/astaxie/beego/param"
|
||||
"github.com/astaxie/beego/response"
|
||||
"github.com/astaxie/beego/toolbox"
|
||||
"github.com/astaxie/beego/utils"
|
||||
)
|
||||
@ -116,6 +118,7 @@ type controllerInfo struct {
|
||||
handler http.Handler
|
||||
runFunction FilterFunc
|
||||
routerType int
|
||||
methodParams []*param.MethodParam
|
||||
}
|
||||
|
||||
// ControllerRegister containers registered router rules, controller handlers and filters.
|
||||
@ -151,6 +154,10 @@ func NewControllerRegister() *ControllerRegister {
|
||||
// Add("/api",&RestController{},"get,post:ApiFunc"
|
||||
// Add("/simple",&SimpleController{},"get:GetFunc;post:PostFunc")
|
||||
func (p *ControllerRegister) Add(pattern string, c ControllerInterface, mappingMethods ...string) {
|
||||
p.addWithMethodParams(pattern, c, nil, mappingMethods...)
|
||||
}
|
||||
|
||||
func (p *ControllerRegister) addWithMethodParams(pattern string, c ControllerInterface, methodParams []*param.MethodParam, mappingMethods ...string) {
|
||||
reflectVal := reflect.ValueOf(c)
|
||||
t := reflect.Indirect(reflectVal).Type()
|
||||
methods := make(map[string]string)
|
||||
@ -181,6 +188,7 @@ func (p *ControllerRegister) Add(pattern string, c ControllerInterface, mappingM
|
||||
route.methods = methods
|
||||
route.routerType = routerTypeBeego
|
||||
route.controllerType = t
|
||||
route.methodParams = methodParams
|
||||
if len(methods) == 0 {
|
||||
for _, m := range HTTPMETHOD {
|
||||
p.addToRouter(m, pattern, route)
|
||||
@ -247,7 +255,7 @@ func (p *ControllerRegister) Include(cList ...ControllerInterface) {
|
||||
key := t.PkgPath() + ":" + t.Name()
|
||||
if comm, ok := GlobalControllerRouter[key]; ok {
|
||||
for _, a := range comm {
|
||||
p.Add(a.Router, c, strings.Join(a.AllowHTTPMethods, ",")+":"+a.Method)
|
||||
p.addWithMethodParams(a.Router, c, a.MethodParams, strings.Join(a.AllowHTTPMethods, ",")+":"+a.Method)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -626,11 +634,12 @@ func (p *ControllerRegister) execFilter(context *beecontext.Context, urlPath str
|
||||
func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
|
||||
startTime := time.Now()
|
||||
var (
|
||||
runRouter reflect.Type
|
||||
findRouter bool
|
||||
runMethod string
|
||||
routerInfo *controllerInfo
|
||||
isRunnable bool
|
||||
runRouter reflect.Type
|
||||
findRouter bool
|
||||
runMethod string
|
||||
methodParams []*param.MethodParam
|
||||
routerInfo *controllerInfo
|
||||
isRunnable bool
|
||||
)
|
||||
context := p.pool.Get().(*beecontext.Context)
|
||||
context.Reset(rw, r)
|
||||
@ -742,6 +751,7 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
||||
routerInfo.handler.ServeHTTP(rw, r)
|
||||
} else {
|
||||
runRouter = routerInfo.controllerType
|
||||
methodParams = routerInfo.methodParams
|
||||
method := r.Method
|
||||
if r.Method == "POST" && context.Input.Query("_method") == "PUT" {
|
||||
method = "PUT"
|
||||
@ -804,9 +814,14 @@ func (p *ControllerRegister) ServeHTTP(rw http.ResponseWriter, r *http.Request)
|
||||
execController.Options()
|
||||
default:
|
||||
if !execController.HandlerFunc(runMethod) {
|
||||
var in []reflect.Value
|
||||
method := vc.MethodByName(runMethod)
|
||||
method.Call(in)
|
||||
var in []reflect.Value = param.ConvertParams(methodParams, method.Type(), context)
|
||||
out := method.Call(in)
|
||||
|
||||
//For backward compatibility we only handle response if we had incoming methodParams
|
||||
if methodParams != nil {
|
||||
p.handleParamResponse(context, execController, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -886,6 +901,17 @@ Admin:
|
||||
}
|
||||
}
|
||||
|
||||
func (p *ControllerRegister) handleParamResponse(context *beecontext.Context, execController ControllerInterface, results []reflect.Value) {
|
||||
//looping in reverse order for the case when both error and value are returned and error sets the response status code
|
||||
for i := len(results) - 1; i >= 0; i-- {
|
||||
result := results[i]
|
||||
if !result.IsNil() {
|
||||
resultValue := result.Interface()
|
||||
response.RenderMethodResult(resultValue, context)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// FindRouter Find Router info for URL
|
||||
func (p *ControllerRegister) FindRouter(context *beecontext.Context) (routerInfo *controllerInfo, isFind bool) {
|
||||
var urlPath = context.Input.URL()
|
||||
|
Reference in New Issue
Block a user