Skip to content

Commit 2943d09

Browse files
committed
feat(api):show api route list
1 parent be10ebb commit 2943d09

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

cmd/server/powerx.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"PowerX/internal/bootstrap"
45
"PowerX/internal/middleware/recovery"
56
"PowerX/internal/model"
67
"PowerX/pkg/pluginx"
@@ -104,6 +105,10 @@ func main() {
104105
httpx.SetErrorHandler(handler.ErrorHandle)
105106
httpx.SetErrorHandlerCtx(handler.ErrorHandleCtx)
106107

108+
// 添加这段:打印所有路由
109+
if c.API.ShowRouteList {
110+
bootstrap.PrintRoutes(server)
111+
}
107112
// ---------- 启动服务器 ----------
108113
logx.Infof("Starting server at %s:%d...\n", c.Server.Host, c.Server.Port)
109114
server.Start()

etc/powerx-example.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ Server:
66
Host: 0.0.0.0 # 服务器地址
77
Port: 8888 # 服务器端口
88
Timeout: 30000 # 超时时间
9+
ShowRouteList : true # 是否显示路由列表
10+
MaxBytes: 10485760 # 最大请求大小(单位:字节)
11+
12+
API:
13+
Prefix: /api/v1 # API前缀
14+
ShowRouteList : true # 是否显示路由列表
915

1016
Cors:
1117
AllowAll: true

internal/bootstrap/route.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package bootstrap
2+
3+
import (
4+
"fmt"
5+
"github.com/zeromicro/go-zero/rest"
6+
"strings"
7+
)
8+
9+
func PrintRoutes(server *rest.Server) {
10+
fmt.Println("📌 Registered Routes:")
11+
12+
routes := server.Routes()
13+
if len(routes) == 0 {
14+
fmt.Println("No routes found.")
15+
return
16+
}
17+
18+
// 先算出最大宽度
19+
maxMethodLen := len("METHOD")
20+
maxPathLen := len("PATH")
21+
22+
for _, route := range routes {
23+
if len(route.Method) > maxMethodLen {
24+
maxMethodLen = len(route.Method)
25+
}
26+
if len(route.Path) > maxPathLen {
27+
maxPathLen = len(route.Path)
28+
}
29+
}
30+
31+
// 分隔线
32+
sep := fmt.Sprintf("+-%s-+-%s-+",
33+
strings.Repeat("-", maxMethodLen),
34+
strings.Repeat("-", maxPathLen),
35+
)
36+
37+
// 打印表头
38+
fmt.Println(sep)
39+
fmt.Printf("| %-*s | %-*s |\n", maxMethodLen, "METHOD", maxPathLen, "PATH")
40+
fmt.Println(sep)
41+
42+
// 打印每一行
43+
for _, route := range routes {
44+
fmt.Printf("| %-*s | %-*s |\n", maxMethodLen, route.Method, maxPathLen, route.Path)
45+
}
46+
fmt.Println(sep)
47+
}

internal/config/config.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ type Config struct {
2020
Env string
2121
Region string
2222
Server rest.RestConf
23-
EtcDir string `json:",optional"`
24-
Log log.LogConf
25-
Cors Cors
26-
JWT struct {
23+
API struct {
24+
Prefix string
25+
ShowRouteList bool
26+
}
27+
EtcDir string `json:",optional"`
28+
Log log.LogConf
29+
Cors Cors
30+
JWT struct {
2731
JWTSecret string
2832
MPJWTSecret string
2933
WebJWTSecret string

0 commit comments

Comments
 (0)