This commit is contained in:
john
2018-08-17 19:01:49 +08:00
parent a307a532d0
commit 492306bddc
5 changed files with 44 additions and 40 deletions

View File

@ -36,7 +36,7 @@ func (c *Controller) Shut(r *ghttp.Request) {
}
// 出请求执行
// 退出请求执行
func (c *Controller) Exit() {
c.Request.Exit()
}

View File

@ -106,6 +106,8 @@ type handlerItem struct {
ctype reflect.Type // 控制器类型(反射类型)
fname string // 回调方法名称
faddr HandlerFunc // 准确的执行方法内存地址(与以上两个参数二选一)
finit HandlerFunc // 初始化请求回调方法(执行对象注册方式下有效)
fshut HandlerFunc // 完成请求回调方法(执行对象注册方式下有效)
router *Router // 注册时绑定的路由对象
}

View File

@ -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)
}
}

View File

@ -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)
}

View File

@ -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)