389 Star 2.6K Fork 641

GVPJohn/gf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
group1.go 1.76 KB
一键复制 编辑 原始数据 按行查看 历史
John 提交于 2018-12-26 10:17 . fix issue in controller interface definition
package main
import (
"gitee.com/johng/gf/g"
"gitee.com/johng/gf/g/frame/gmvc"
"gitee.com/johng/gf/g/net/ghttp"
)
type Object struct {}
func (o *Object) Show(r *ghttp.Request) {
r.Response.Writeln("Object Show")
}
func (o *Object) Delete(r *ghttp.Request) {
r.Response.Writeln("Object REST Delete")
}
func (o *Object) Shut(r *ghttp.Request) {
r.Response.Writeln("Object Shut")
}
type Controller struct {
gmvc.Controller
}
func (c *Controller) Show() {
c.Response.Writeln("Controller Show")
}
func (c *Controller) Post() {
c.Response.Writeln("Controller REST Post")
}
func (c *Controller) Shut() {
c.Response.Writeln("Controller Shut")
}
func Handler(r *ghttp.Request) {
r.Response.Writeln("Handler")
}
func HookHandler(r *ghttp.Request) {
r.Response.Writeln("Hook Handler")
}
func main() {
s := g.Server()
obj := new(Object)
ctl := new(Controller)
// 分组路由方法注册
//g := s.Group("/api")
//g.ALL ("*", HookHandler, ghttp.HOOK_BEFORE_SERVE)
//g.ALL ("/handler", Handler)
//g.ALL ("/ctl", ctl)
//g.GET ("/ctl/my-show", ctl, "Show")
//g.REST("/ctl/rest", ctl)
//g.ALL ("/obj", obj)
//g.GET ("/obj/my-show", obj, "Show")
//g.REST("/obj/rest", obj)
// 分组路由批量注册
s.Group("/api").Bind("/api", []ghttp.GroupItem{
{"ALL", "/handler", Handler},
{"ALL", "/ctl", ctl},
{"GET", "/ctl/my-show", ctl, "Show"},
{"REST", "/ctl/rest", ctl},
{"ALL", "/obj", obj},
{"GET", "/obj/my-show", obj, "Show"},
{"REST", "/obj/rest", obj},
{"ALL", "*", HookHandler, ghttp.HOOK_BEFORE_SERVE},
})
s.SetPort(8199)
s.Run()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/johng/gf.git
git@gitee.com:johng/gf.git
johng
gf
gf
v1.4.6

搜索帮助

0d507c66 1850385 C8b1a773 1850385