完善服务注册示例代码

This commit is contained in:
john
2018-07-30 21:01:19 +08:00
parent 3ac7169cf7
commit da6093a900
6 changed files with 109 additions and 4 deletions

View File

@ -97,7 +97,11 @@ func (s *Server)BindObject(pattern string, obj interface{}) error {
}
// 如果方法中带有Index方法那么额外自动增加一个路由规则匹配主URI
if strings.EqualFold(method, "Index") {
m[pattern] = &handlerItem {
p := key
if strings.EqualFold(p[len(p) - 6:], "/index") {
p = p[0 : len(p) - 6]
}
m[p] = &handlerItem {
ctype : nil,
fname : "",
faddr : v.Method(i).Interface().(func(*Request)),
@ -128,7 +132,11 @@ func (s *Server)BindObjectMethod(pattern string, obj interface{}, methods string
}
// 如果方法中带有Index方法那么额外自动增加一个路由规则匹配主URI
if strings.EqualFold(mname, "Index") {
m[pattern] = &handlerItem {
p := key
if strings.EqualFold(p[len(p) - 6:], "/index") {
p = p[0 : len(p) - 6]
}
m[p] = &handlerItem {
ctype : nil,
fname : "",
faddr : fval.Interface().(func(*Request)),
@ -181,7 +189,11 @@ func (s *Server)BindController(pattern string, c Controller) error {
}
// 如果方法中带有Index方法那么额外自动增加一个路由规则匹配主URI
if strings.EqualFold(mname, "Index") {
m[pattern] = &handlerItem {
p := key
if strings.EqualFold(p[len(p) - 6:], "/index") {
p = p[0 : len(p) - 6]
}
m[p] = &handlerItem {
ctype : v.Elem().Type(),
fname : mname,
faddr : nil,
@ -212,7 +224,11 @@ func (s *Server)BindControllerMethod(pattern string, c Controller, methods strin
}
// 如果方法中带有Index方法那么额外自动增加一个路由规则匹配主URI
if strings.EqualFold(mname, "Index") {
m[pattern] = &handlerItem {
p := key
if strings.EqualFold(p[len(p) - 6:], "/index") {
p = p[0 : len(p) - 6]
}
m[p] = &handlerItem {
ctype : t,
fname : mname,
faddr : nil,

View File

@ -0,0 +1,22 @@
package demo
import (
"gitee.com/johng/gf/g"
"gitee.com/johng/gf/g/frame/gmvc"
)
type Order struct {
gmvc.Controller
}
func init() {
g.Server().BindController("/{.struct}/{.method}", &Order{})
}
func (o *Order) Index() {
o.Response.Write("Order Index")
}
func (o *Order) List() {
o.Response.Write("Order List")
}

View File

@ -0,0 +1,26 @@
package demo
import (
"gitee.com/johng/gf/g"
"gitee.com/johng/gf/g/net/ghttp"
"gitee.com/johng/gf/g/util/gconv"
)
type Product struct {
total int
}
func init() {
p := &Product{}
g.Server().BindHandler("/product/total", p.Total)
g.Server().BindHandler("/product/list/{page}.html", p.List)
}
func (p *Product) Total(r *ghttp.Request) {
p.total++
r.Response.Write("total: ", gconv.String(p.total))
}
func (p *Product) List(r *ghttp.Request) {
r.Response.Write("page: ", r.Get("page"))
}

View File

@ -0,0 +1,20 @@
package stats
import (
"gitee.com/johng/gf/g"
"gitee.com/johng/gf/g/net/ghttp"
"gitee.com/johng/gf/g/util/gconv"
)
var (
total1 int
)
func init() {
g.Server().BindHandler("/stats/total1", showTotal1)
}
func showTotal1(r *ghttp.Request) {
total1++
r.Response.Write("total:", gconv.String(total1))
}

View File

@ -0,0 +1,20 @@
package stats
import (
"gitee.com/johng/gf/g"
"gitee.com/johng/gf/g/net/ghttp"
"gitee.com/johng/gf/g/util/gconv"
"gitee.com/johng/gf/g/container/gtype"
)
var (
total2 = gtype.NewInt()
)
func init() {
g.Server().BindHandler("/stats/total2", showTotal2)
}
func showTotal2(r *ghttp.Request) {
r.Response.Write("total:", gconv.String(total2.Add(1)))
}

View File

@ -3,6 +3,7 @@ package main
import (
"gitee.com/johng/gf/g"
_ "gitee.com/johng/gf/geg/frame/mvc/controller/demo"
_ "gitee.com/johng/gf/geg/frame/mvc/controller/stats"
)
func main() {