🎨 优化dockerfile,优化路由匹配

This commit is contained in:
2026-03-03 17:47:19 +08:00
parent 8e52b854ea
commit f8306be916
2 changed files with 17 additions and 7 deletions

View File

@@ -17,7 +17,13 @@ COPY web/ ./
RUN npm run build
# 阶段2: 构建后端
FROM golang:1.21-alpine as backend-builder
FROM golang:1.23-alpine as backend-builder
# 设置环境变量允许 Go 自动下载工具链
ENV GO111MODULE=on \
GOPROXY=https://goproxy.cn,direct \
CGO_ENABLED=0 \
GOTOOLCHAIN=auto
WORKDIR /app/server
@@ -25,10 +31,7 @@ WORKDIR /app/server
COPY server/go.mod server/go.sum ./
# 下载依赖
RUN go env -w GO111MODULE=on \
&& go env -w GOPROXY=https://goproxy.cn,direct \
&& go env -w CGO_ENABLED=0 \
&& go mod download
RUN go mod download
# 复制后端源码
COPY server/ ./

View File

@@ -143,14 +143,21 @@ func setupFrontendRoutes(router *gin.Engine) {
// SPA 路由处理:所有非 API 请求都返回 index.html
router.NoRoute(func(c *gin.Context) {
path := c.Request.URL.Path
// 如果是 API 请求,返回 404
if strings.HasPrefix(path, global.GVA_CONFIG.System.RouterPrefix) {
// 如果是 API 请求(/api/* 或 /v1/*),返回 404
routerPrefix := global.GVA_CONFIG.System.RouterPrefix
if routerPrefix == "" {
routerPrefix = "/api"
}
if strings.HasPrefix(path, routerPrefix) || strings.HasPrefix(path, "/v1") {
c.JSON(http.StatusNotFound, gin.H{
"code": 404,
"msg": "接口不存在",
})
return
}
// 其他请求返回前端页面
c.File("./dist/index.html")
})