From f71579f9f4cd51ff2f9bc3cad89dd93f5549bc1d Mon Sep 17 00:00:00 2001 From: John Date: Wed, 16 May 2018 17:37:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A7=E5=88=B6=E5=99=A8=E5=8F=8A=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E5=AF=B9=E8=B1=A1=E6=B3=A8=E5=86=8C=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=9B=B4=E7=81=B5=E6=B4=BB=E7=9A=84=E5=8A=A8=E6=80=81=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E7=89=B9=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/net/ghttp/http_server_service.go | 34 ++++++++++++++++----------- geg/frame/mvc/controller/demo/rule.go | 19 +++++++++++++++ geg/frame/mvc/main.go | 6 ++--- 3 files changed, 42 insertions(+), 17 deletions(-) create mode 100644 geg/frame/mvc/controller/demo/rule.go diff --git a/g/net/ghttp/http_server_service.go b/g/net/ghttp/http_server_service.go index 54a907b36..678d3e652 100644 --- a/g/net/ghttp/http_server_service.go +++ b/g/net/ghttp/http_server_service.go @@ -34,23 +34,29 @@ func (s *Server)bindHandlerByMap(m HandlerMap) error { return nil } -// 将方法名称按照设定的规则转换为URI并附加到指定的URI后面 -func (s *Server)appendMethodNameToUriWithPattern(pattern string, name string) string { +// 将方法名称按照设定的规则合并到pattern中. +// 规则1:pattern中的URI包含{method}关键字,则替换该关键字为方法名称 +// 规则2:如果不满足规则1,那么直接将防发明附加到pattern中的URI后面 +func (s *Server)mergeMethodNameToPattern(pattern string, name string) string { + // 方法名中间存在大写字母,转换为小写URI地址以“-”号链接每个单词 + method := "" + for i := 0; i < len(name); i++ { + if i > 0 && gutil.IsLetterUpper(name[i]) { + method += "-" + } + method += strings.ToLower(string(name[i])) + } + if strings.Index(pattern, "{method}") != -1 { + return strings.Replace(pattern, "{method}", method, -1) + } // 检测域名后缀 array := strings.Split(pattern, "@") // 分离URI(其实可能包含HTTP Method) uri := array[0] uri = strings.TrimRight(uri, "/") + "/" - // 方法名中间存在大写字母,转换为小写URI地址以“-”号链接每个单词 - for i := 0; i < len(name); i++ { - if i > 0 && gutil.IsLetterUpper(name[i]) { - uri += "-" - } - uri += strings.ToLower(string(name[i])) - } // 加上指定域名后缀 if len(array) > 1 { - uri += "@" + array[1] + return uri + "@" + array[1] } return uri } @@ -72,7 +78,7 @@ func (s *Server)BindObject(pattern string, obj interface{}) error { t := v.Type() for i := 0; i < v.NumMethod(); i++ { name := t.Method(i).Name - key := s.appendMethodNameToUriWithPattern(pattern, name) + key := s.mergeMethodNameToPattern(pattern, name) m[key] = &HandlerItem { ctype : nil, fname : "", @@ -100,7 +106,7 @@ func (s *Server)BindObjectMethod(pattern string, obj interface{}, methods string if !fval.IsValid() { return errors.New("invalid method name:" + name) } - key := s.appendMethodNameToUriWithPattern(pattern, name) + key := s.mergeMethodNameToPattern(pattern, name) m[key] = &HandlerItem{ ctype : nil, fname : "", @@ -152,7 +158,7 @@ func (s *Server)BindController(pattern string, c Controller) error { if name == "Init" || name == "Shut" || name == "Exit" { continue } - key := s.appendMethodNameToUriWithPattern(pattern, name) + key := s.mergeMethodNameToPattern(pattern, name) m[key] = &HandlerItem { ctype : v.Elem().Type(), fname : name, @@ -181,7 +187,7 @@ func (s *Server)BindControllerMethod(pattern string, c Controller, methods strin if !cval.MethodByName(name).IsValid() { return errors.New("invalid method name:" + name) } - key := s.appendMethodNameToUriWithPattern(pattern, name) + key := s.mergeMethodNameToPattern(pattern, name) m[key] = &HandlerItem { ctype : ctype, fname : name, diff --git a/geg/frame/mvc/controller/demo/rule.go b/geg/frame/mvc/controller/demo/rule.go new file mode 100644 index 000000000..224f9a885 --- /dev/null +++ b/geg/frame/mvc/controller/demo/rule.go @@ -0,0 +1,19 @@ +package demo + +import ( + "gitee.com/johng/gf/g" + "gitee.com/johng/gf/g/frame/gmvc" +) + +type ControllerRule struct { + gmvc.Controller +} + +func init() { + g.Server().BindController("/rule/{method}/:name", &ControllerRule{}) +} + +func (c *ControllerRule) Show() { + c.Response.Write(c.Request.Get("name")) +} + diff --git a/geg/frame/mvc/main.go b/geg/frame/mvc/main.go index e4161b36b..0544de505 100644 --- a/geg/frame/mvc/main.go +++ b/geg/frame/mvc/main.go @@ -1,11 +1,11 @@ package main import ( - "gitee.com/johng/gf/g/net/ghttp" + "gitee.com/johng/gf/g" _ "gitee.com/johng/gf/geg/frame/mvc/controller/demo" ) func main() { - ghttp.GetServer().SetPort(8199) - ghttp.GetServer().Run() + g.Server().SetPort(8199) + g.Server().Run() }