mirror of
https://gitee.com/johng/gf
synced 2026-07-01 19:31:23 +08:00
33 lines
922 B
Go
33 lines
922 B
Go
package main
|
||
|
||
import (
|
||
"github.com/gogf/gf/g"
|
||
"github.com/gogf/gf/g/net/ghttp"
|
||
)
|
||
|
||
// 优先调用的HOOK
|
||
func beforeServeHook1(r *ghttp.Request) {
|
||
r.SetParam("name", "GoFrame")
|
||
r.Response.Writeln("set name")
|
||
}
|
||
|
||
// 随后调用的HOOK
|
||
func beforeServeHook2(r *ghttp.Request) {
|
||
r.SetParam("site", "https://goframe.org")
|
||
r.Response.Writeln("set site")
|
||
}
|
||
|
||
// 允许对同一个路由同一个事件注册多个回调函数,按照注册顺序进行优先级调用。
|
||
// 为便于在路由表中对比查看优先级,这里讲HOOK回调函数单独定义为了两个函数。
|
||
func main() {
|
||
s := g.Server()
|
||
s.BindHandler("/", func(r *ghttp.Request) {
|
||
r.Response.Writeln(r.GetParam("name").String())
|
||
r.Response.Writeln(r.GetParam("site").String())
|
||
})
|
||
s.BindHookHandler("/", ghttp.HOOK_BEFORE_SERVE, beforeServeHook1)
|
||
s.BindHookHandler("/", ghttp.HOOK_BEFORE_SERVE, beforeServeHook2)
|
||
s.SetPort(8199)
|
||
s.Run()
|
||
}
|