uniugm/admin/lib/web/example/example.go

213 lines
5.3 KiB
Go
Raw Normal View History

2025-04-18 17:17:23 +08:00
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 = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>GM MASTER</title>
<div style="padding-left: 50px;">
{{ $pathsList := .Paths }}
{{ range $pidex, $paths := $pathsList }}
<div style="float:left">
<h1 style="text-align: center">指令列表{{ incr $pidex }}</h1>
<div style="padding-left: 20px;padding-right: 20px">
<table border = "1" align="center">
{{ range $idx, $path := $paths }}
<tr>
<th colspan="5" style="background-color: green; text-align: left">{{ $path.Route.Desc }}</th>
</tr>
<tr>
<td><b>请求路径</b></td>
<th colspan="4" style="text-align: left">&nbsp;&nbsp;{{ $path.Path }}</th>
</tr>
<tr>
<td><b>方法</b></td>
<th colspan="4" style="text-align: left">&nbsp;&nbsp;{{ $path.Route.MethodDesc "或" }}</th>
</tr>
{{ if $path.Route.HasRequest }}
{{ range $idx1, $field := $path.Route.Fields }}
<tr>
<td><b>{{ printf "参数%d" $idx1 }}</b></td>
<td style="text-align: center">{{printf "%s" $field.Name }}</td>
<td style="text-align: center">{{printf "%s" $field.Type }}</td>
<td>{{printf "%s" $field.Desc }}</td>
{{ if eq $field.Default "" }}
<td>{{printf "默认:无" }}</td>
{{ else }}
<td>{{printf "默认:%s" $field.Default }}</td>
{{ end }}
</tr>
{{ end }}
{{ else }}
<tr><th colspan="5">无需参数</th>
{{ end }}
<!--<tr><th colspan="5"><hr></th></tr>-->
{{ end }}
</table>
</div>
</div>
{{ end }}
</div>
</head>
<body>
</body>
</html>
`