diff --git a/g/frame/gmvc/controller.go b/g/frame/gmvc/controller.go index 9d79771ef..400c02538 100644 --- a/g/frame/gmvc/controller.go +++ b/g/frame/gmvc/controller.go @@ -36,7 +36,7 @@ func (c *Controller) Shut(r *ghttp.Request) { } -// 推出请求执行 +// 退出请求执行 func (c *Controller) Exit() { c.Request.Exit() } diff --git a/g/net/ghttp/ghttp_server.go b/g/net/ghttp/ghttp_server.go index dd219f19c..6746ce752 100644 --- a/g/net/ghttp/ghttp_server.go +++ b/g/net/ghttp/ghttp_server.go @@ -106,6 +106,8 @@ type handlerItem struct { ctype reflect.Type // 控制器类型(反射类型) fname string // 回调方法名称 faddr HandlerFunc // 准确的执行方法内存地址(与以上两个参数二选一) + finit HandlerFunc // 初始化请求回调方法(执行对象注册方式下有效) + fshut HandlerFunc // 完成请求回调方法(执行对象注册方式下有效) router *Router // 注册时绑定的路由对象 } diff --git a/g/net/ghttp/ghttp_server_handler.go b/g/net/ghttp/ghttp_server_handler.go index 04664a939..df75e99aa 100644 --- a/g/net/ghttp/ghttp_server_handler.go +++ b/g/net/ghttp/ghttp_server_handler.go @@ -79,7 +79,7 @@ func (s *Server)handleRequest(w http.ResponseWriter, r *http.Request) { } // 执行回调控制器/执行对象/方法 - if handler != nil { + if handler != nil && !request.exit.Val() { s.callServeHandler(handler, request) } @@ -107,12 +107,10 @@ func (s *Server)callServeHandler(h *handlerItem, r *Request) { c.MethodByName("Init").Call([]reflect.Value{reflect.ValueOf(r)}) if !r.IsExited() { c.MethodByName(h.fname).Call(nil) + c.MethodByName("Shut").Call([]reflect.Value{reflect.ValueOf(r)}) } - c.MethodByName("Shut").Call([]reflect.Value{reflect.ValueOf(r)}) } else { - if !r.IsExited() { - h.faddr(r) - } + h.faddr(r) } } diff --git a/g/net/ghttp/ghttp_server_service_controller.go b/g/net/ghttp/ghttp_server_service_controller.go index 4f50c2c0d..e0ce46624 100644 --- a/g/net/ghttp/ghttp_server_service_controller.go +++ b/g/net/ghttp/ghttp_server_service_controller.go @@ -79,22 +79,6 @@ func (s *Server)BindControllerMethod(pattern string, c Controller, method string fname : mname, faddr : nil, } - // 如果方法中带有Index方法,那么额外自动增加一个路由规则匹配主URI - if strings.EqualFold(mname, "Index") { - p := key - if strings.EqualFold(p[len(p) - 6:], "/index") { - p = p[0 : len(p) - 6] - if len(p) == 0 { - p = "/" - } - } - m[p] = &handlerItem { - ctype : t, - fname : mname, - faddr : nil, - } - } - return s.bindHandlerByMap(m) } diff --git a/g/net/ghttp/ghttp_server_service_object.go b/g/net/ghttp/ghttp_server_service_object.go index bfc18221d..1e4160694 100644 --- a/g/net/ghttp/ghttp_server_service_object.go +++ b/g/net/ghttp/ghttp_server_service_object.go @@ -27,16 +27,29 @@ func (s *Server)BindObject(pattern string, obj interface{}, methods...string) er v := reflect.ValueOf(obj) t := v.Type() sname := t.Elem().Name() + finit := (func(*Request))(nil) + fshut := (func(*Request))(nil) + if v.MethodByName("Init").IsValid() { + finit = v.MethodByName("Init").Interface().(func(*Request)) + } + if v.MethodByName("Shut").IsValid() { + fshut = v.MethodByName("Shut").Interface().(func(*Request)) + } for i := 0; i < v.NumMethod(); i++ { mname := t.Method(i).Name if methodMap != nil && !methodMap[mname] { continue } + if mname == "Init" || mname == "Shut" { + continue + } key := s.mergeBuildInNameToPattern(pattern, sname, mname, true) m[key] = &handlerItem { ctype : nil, fname : "", faddr : v.Method(i).Interface().(func(*Request)), + finit : finit, + fshut : fshut, } // 如果方法中带有Index方法,那么额外自动增加一个路由规则匹配主URI if strings.EqualFold(mname, "Index") { @@ -51,6 +64,8 @@ func (s *Server)BindObject(pattern string, obj interface{}, methods...string) er ctype : nil, fname : "", faddr : v.Method(i).Interface().(func(*Request)), + finit : finit, + fshut : fshut, } } } @@ -69,26 +84,21 @@ func (s *Server)BindObjectMethod(pattern string, obj interface{}, method string) if !fval.IsValid() { return errors.New("invalid method name:" + mname) } + finit := (func(*Request))(nil) + fshut := (func(*Request))(nil) + if v.MethodByName("Init").IsValid() { + finit = v.MethodByName("Init").Interface().(func(*Request)) + } + if v.MethodByName("Shut").IsValid() { + fshut = v.MethodByName("Shut").Interface().(func(*Request)) + } key := s.mergeBuildInNameToPattern(pattern, sname, mname, false) m[key] = &handlerItem{ ctype : nil, fname : "", faddr : fval.Interface().(func(*Request)), - } - // 如果方法中带有Index方法,那么额外自动增加一个路由规则匹配主URI - if strings.EqualFold(mname, "Index") { - p := key - if strings.EqualFold(p[len(p) - 6:], "/index") { - p = p[0 : len(p) - 6] - if len(p) == 0 { - p = "/" - } - } - m[p] = &handlerItem { - ctype : nil, - fname : "", - faddr : fval.Interface().(func(*Request)), - } + finit : finit, + fshut : fshut, } return s.bindHandlerByMap(m) @@ -97,9 +107,17 @@ func (s *Server)BindObjectMethod(pattern string, obj interface{}, method string) // 绑定对象到URI请求处理中,会自动识别方法名称,并附加到对应的URI地址后面 // 需要注意对象方法的定义必须按照ghttp.HandlerFunc来定义 func (s *Server)BindObjectRest(pattern string, obj interface{}) error { - m := make(handlerMap) - v := reflect.ValueOf(obj) - t := v.Type() + m := make(handlerMap) + v := reflect.ValueOf(obj) + t := v.Type() + finit := (func(*Request))(nil) + fshut := (func(*Request))(nil) + if v.MethodByName("Init").IsValid() { + finit = v.MethodByName("Init").Interface().(func(*Request)) + } + if v.MethodByName("Shut").IsValid() { + fshut = v.MethodByName("Shut").Interface().(func(*Request)) + } for i := 0; i < v.NumMethod(); i++ { name := t.Method(i).Name method := strings.ToUpper(name) @@ -111,6 +129,8 @@ func (s *Server)BindObjectRest(pattern string, obj interface{}) error { ctype : nil, fname : "", faddr : v.Method(i).Interface().(func(*Request)), + finit : finit, + fshut : fshut, } } return s.bindHandlerByMap(m)