mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
完善服务注册示例代码
This commit is contained in:
@ -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,
|
||||
|
||||
22
geg/frame/mvc/controller/demo/buildin_vars.go
Normal file
22
geg/frame/mvc/controller/demo/buildin_vars.go
Normal 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")
|
||||
}
|
||||
26
geg/frame/mvc/controller/demo/product.go
Normal file
26
geg/frame/mvc/controller/demo/product.go
Normal 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"))
|
||||
}
|
||||
20
geg/frame/mvc/controller/stats/stats1.go
Normal file
20
geg/frame/mvc/controller/stats/stats1.go
Normal 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))
|
||||
}
|
||||
20
geg/frame/mvc/controller/stats/stats2.go
Normal file
20
geg/frame/mvc/controller/stats/stats2.go
Normal 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)))
|
||||
}
|
||||
@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user