更新后端友好界面

This commit is contained in:
李志强 2025-11-05 16:18:29 +08:00
parent ad8978617c
commit b00463cb14
2 changed files with 292 additions and 0 deletions

View File

@ -8,6 +8,147 @@ type MainController struct {
beego.Controller
}
// Get 处理根路径的 GET 请求,显示友好的欢迎页面
func (c *MainController) Get() {
html := `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API 服务运行正常</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
background: white;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
padding: 60px 40px;
max-width: 600px;
width: 100%;
text-align: center;
animation: fadeIn 0.5s ease-in;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(-20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.success-icon {
width: 80px;
height: 80px;
background: #4CAF50;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto 30px;
animation: scaleIn 0.5s ease-out;
}
@keyframes scaleIn {
from {
transform: scale(0);
}
to {
transform: scale(1);
}
}
.success-icon::after {
content: '✓';
color: white;
font-size: 50px;
font-weight: bold;
}
h1 {
color: #333;
font-size: 32px;
margin-bottom: 15px;
font-weight: 600;
}
.status {
color: #4CAF50;
font-size: 18px;
margin-bottom: 30px;
font-weight: 500;
}
.info {
background: #f5f5f5;
border-radius: 10px;
padding: 20px;
margin: 30px 0;
text-align: left;
}
.info-item {
padding: 10px 0;
border-bottom: 1px solid #e0e0e0;
}
.info-item:last-child {
border-bottom: none;
}
.info-label {
color: #666;
font-size: 14px;
margin-bottom: 5px;
}
.info-value {
color: #333;
font-size: 16px;
font-weight: 500;
}
.footer {
margin-top: 30px;
color: #999;
font-size: 14px;
}
</style>
</head>
<body>
<div class="container">
<div class="success-icon"></div>
<h1>API 服务运行正常</h1>
<div class="status"> 服务已成功启动并运行中</div>
<div class="info">
<div class="info-item">
<div class="info-label">服务状态</div>
<div class="info-value">运行中</div>
</div>
<div class="info-item">
<div class="info-label">API 版本</div>
<div class="info-value">1.0.0</div>
</div>
<div class="info-item">
<div class="info-label">服务类型</div>
<div class="info-value">RESTful API 服务</div>
</div>
</div>
<div class="footer">
<p>这是一个 API 服务请使用 API 客户端或前端应用进行访问</p>
<p style="margin-top: 10px;">如需查看 API 文档请联系系统管理员</p>
</div>
</div>
</body>
</html>`
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!"}

View File

@ -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 := `<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>API 服务运行正常</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
background: white;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
padding: 60px 40px;
max-width: 600px;
width: 100%;
text-align: center;
animation: fadeIn 0.5s ease-in;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(-20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.success-icon {
width: 80px;
height: 80px;
background: #4CAF50;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
margin: 0 auto 30px;
animation: scaleIn 0.5s ease-out;
}
@keyframes scaleIn {
from {
transform: scale(0);
}
to {
transform: scale(1);
}
}
.success-icon::after {
content: '✓';
color: white;
font-size: 50px;
font-weight: bold;
}
h1 {
color: #333;
font-size: 32px;
margin-bottom: 15px;
font-weight: 600;
}
.status {
color: #4CAF50;
font-size: 18px;
margin-bottom: 30px;
font-weight: 500;
}
.info {
background: #f5f5f5;
border-radius: 10px;
padding: 20px;
margin: 30px 0;
text-align: left;
}
.info-item {
padding: 10px 0;
border-bottom: 1px solid #e0e0e0;
}
.info-item:last-child {
border-bottom: none;
}
.info-label {
color: #666;
font-size: 14px;
margin-bottom: 5px;
}
.info-value {
color: #333;
font-size: 16px;
font-weight: 500;
}
.footer {
margin-top: 30px;
color: #999;
font-size: 14px;
}
</style>
</head>
<body>
<div class="container">
<div class="success-icon"></div>
<h1>API 服务运行正常</h1>
<div class="status"> 服务已成功启动并运行中</div>
<div class="info">
<div class="info-item">
<div class="info-label">服务状态</div>
<div class="info-value">运行中</div>
</div>
<div class="info-item">
<div class="info-label">API 版本</div>
<div class="info-value">1.0.0</div>
</div>
<div class="info-item">
<div class="info-label">访问路径</div>
<div class="info-value">` + ctx.Input.URL() + `</div>
</div>
<div class="info-item">
<div class="info-label">请求方法</div>
<div class="info-value">` + ctx.Input.Method() + `</div>
</div>
</div>
<div class="footer">
<p>这是一个 API 服务请使用 API 客户端或前端应用进行访问</p>
<p style="margin-top: 10px;">如需查看 API 文档请联系系统管理员</p>
</div>
</div>
</body>
</html>`
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) {