From b00463cb1425be7371eae7ee5127b03e61cae86e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=BF=97=E5=BC=BA?= <357099073@qq.com> Date: Wed, 5 Nov 2025 16:18:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=90=8E=E7=AB=AF=E5=8F=8B?= =?UTF-8?q?=E5=A5=BD=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/controllers/default.go | 141 +++++++++++++++++++++++++++++++ server/routers/router.go | 151 ++++++++++++++++++++++++++++++++++ 2 files changed, 292 insertions(+) diff --git a/server/controllers/default.go b/server/controllers/default.go index 155cb79..2f38933 100644 --- a/server/controllers/default.go +++ b/server/controllers/default.go @@ -8,6 +8,147 @@ type MainController struct { beego.Controller } +// Get 处理根路径的 GET 请求,显示友好的欢迎页面 +func (c *MainController) Get() { + html := ` + + + + + API 服务运行正常 + + + +
+
+

API 服务运行正常

+
✓ 服务已成功启动并运行中
+
+
+
服务状态
+
运行中
+
+
+
API 版本
+
1.0.0
+
+
+
服务类型
+
RESTful API 服务
+
+
+ +
+ +` + c.Ctx.Output.Header("Content-Type", "text/html; charset=utf-8") + c.Ctx.Output.Body([]byte(html)) +} + // Hello 处理 /api/hello 请求 func (c *MainController) Hello() { c.Data["json"] = map[string]string{"message": "Hello from Beego backend!"} diff --git a/server/routers/router.go b/server/routers/router.go index 28c3b4d..f76bd52 100644 --- a/server/routers/router.go +++ b/server/routers/router.go @@ -28,6 +28,157 @@ func init() { } }) + // 处理浏览器访问 API 路径的情况 - 在路由之前拦截 + beego.InsertFilter("/api/*", beego.BeforeRouter, func(ctx *context.Context) { + // 检查是否是浏览器访问(Accept 头包含 text/html) + accept := ctx.Input.Header("Accept") + if strings.Contains(accept, "text/html") { + html := ` + + + + + API 服务运行正常 + + + +
+
+

API 服务运行正常

+
✓ 服务已成功启动并运行中
+
+
+
服务状态
+
运行中
+
+
+
API 版本
+
1.0.0
+
+
+
访问路径
+
` + ctx.Input.URL() + `
+
+
+
请求方法
+
` + ctx.Input.Method() + `
+
+
+ +
+ +` + ctx.Output.Header("Content-Type", "text/html; charset=utf-8") + ctx.Output.Status = 200 + ctx.Output.Body([]byte(html)) + return + } + }) + // 为除登录、登出和重置密码外的API路由应用JWT中间件 // 这样登录请求就不会被JWT中间件拦截 beego.InsertFilter("/api/*", beego.BeforeRouter, func(ctx *context.Context) {