package main import ( "admin/lib/web" "fmt" "html/template" "sort" ) type MyContext struct { rawCtx web.RawContext } func (ctx *MyContext) SetRawContext(rawCtx web.RawContext) { ctx.rawCtx = rawCtx } func (ctx *MyContext) GetRawContext() web.RawContext { return ctx.rawCtx } func main() { coreRouter := "iris" // gin|iris engine := web.NewEngine(":8888", coreRouter, func() web.Context { return new(MyContext) }) groupV1 := engine.Group("/v1") { type Test1 struct { RoleId string `json:"role_id" desc:"角色id字段" required:"true"` F1 int `json:"f1" desc:"这是字段1" default:"324"` F2 string `json:"f2" desc:"这是字段2" default:"abcd"` F3 bool `json:"f3" desc:"这是字段3" default:"false"` } groupV1.Get("/test1", "设置玩家背包数据", web.AccessMode_Write, Test1{}, func(ctx *MyContext, params *Test1) { fmt.Printf("receive test1 path params:%+v\n", params) ctx.GetRawContext().Json(200, map[string]any{ "msg": "ok", "code": 200, }) }) groupV1.Post("/test1", "获取玩家数据", web.AccessMode_Read, Test1{}, func(ctx *MyContext, params *Test1) { fmt.Printf("receive test1 path params:%+v\n", params) ctx.GetRawContext().Json(200, map[string]any{ "msg": "ok", "code": 200, }) }) // URI --> :8888/v1/test1?f1=123&f2=sdfsd&f3=true // BODY --> :8888/v1/test1 {"f1":123,"f2":"sdfds","f3":"true"} } engine.Get("/test2", "获取玩家比赛", web.AccessMode_Read, nil, func(ctx *MyContext) { fmt.Printf("receive test2 request\n") ctx.GetRawContext().Json(200, map[string]any{ "msg": "ok", "code": 200, }) }) engine.Post("/test2", "测试gm指令名字", web.AccessMode_Read, nil, func(ctx *MyContext) { fmt.Printf("receive test2 request\n") ctx.GetRawContext().Json(200, map[string]any{ "msg": "ok", "code": 200, }) }) groupV2 := engine.Group("v2") { type Test2 struct { F1 int `json:"f1" desc:"这是字段1"` F2 string `json:"f2" desc:"这是字段2"` F3 bool `json:"f3" desc:"这是字段3"` } groupV2.Post("test3", "测试gm指令名字123", web.AccessMode_Write, Test2{}, func(ctx *MyContext, params *Test2) { fmt.Printf("receive test3\n") ctx.GetRawContext().Json(200, map[string]any{ "msg": "ok", "code": 200, }) }) } engine.Get("path_tree", "测试gm指令名字3424", web.AccessMode_Write, nil, func(ctx *MyContext) { tree := engine.TravelPathTree() ctx.GetRawContext().Json(200, tree) }) engine.Get("/grid", "获取指令描述表", web.AccessMode_Read, nil, func(c *MyContext) { tree := &Tree{tree: engine.TravelPathTree()} tmpl, err := template.New("html_test").Funcs(template.FuncMap(map[string]any{ "incr": incr, })).Parse(tplText) if err != nil { panic(err) } err = tmpl.Execute(c.GetRawContext().Writer(), tree) if err != nil { panic(err) } }) err := engine.Run() if err != nil { panic(err) } } type Path struct { Path string Route *web.RouteDescInfo } type Tree struct { tree map[string]*web.RouteDescInfo } func (t *Tree) Paths() [][]*Path { splitCol := 4 list := make([]*Path, 0) for k, v := range t.tree { list = append(list, &Path{Path: k, Route: v}) } sort.SliceStable(list, func(i, j int) bool { return list[i].Path < list[j].Path }) if len(list) <= splitCol { return [][]*Path{list} } section := len(list) / splitCol paths := make([][]*Path, splitCol) for i := 0; i < splitCol; i++ { paths[i] = make([]*Path, 0) start := i * section for j := 0; j < section; j++ { start += j paths[i] = append(paths[i], list[start]) } } if len(list)%splitCol > 0 { idx := 0 for i := len(list) - len(list)%splitCol; i < len(list); i++ { paths[idx] = append(paths[idx], list[i]) idx++ } } return paths } func incr(src int) int { return src + 1 } var tplText = `
{{ $path.Route.Desc }} | |||||
---|---|---|---|---|---|
请求路径 | {{ $path.Path }} | ||||
方法 | {{ $path.Route.MethodDesc "或" }} | ||||
{{ printf "参数%d" $idx1 }} | {{printf "%s" $field.Name }} | {{printf "%s" $field.Type }} | {{printf "%s" $field.Desc }} | {{ if eq $field.Default "" }}{{printf "默认:无" }} | {{ else }}{{printf "默认:%s" $field.Default }} | {{ end }}
无需参数 | {{ end }} {{ end }}