...
Code Block | ||
---|---|---|
| ||
package main import ( "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/ghttp" ) // 前置中间件1 func MiddlewareBefore1(r *ghttp.Request) { r.SetParam("name", "GoFrame") r.Response.Writeln("set name") r.Middleware.Next() } // 前置中间件2 func MiddlewareBefore2(r *ghttp.Request) { r.SetParam("site", "https://goframe.org") r.Response.Writeln("set site") r.Middleware.Next() } func main() { s := g.Server() s.Group("/", func(group *ghttp.RouterGroup) { group.Middleware(MiddlewareBefore1, MiddlewareBefore2) group.ALL("/", func(r *ghttp.Request) { r.Response.Writefln( "%s: %s", r.GetParamVarGetParam("name").String(), r.GetParamVarGetParam("site").String(), ) }) }) s.SetPort(8199) s.Run() } |
...