From f8306be916bffaa46a5e28f7f9fd0de02bff77d2 Mon Sep 17 00:00:00 2001 From: Eg <1711788888@qq.com> Date: Tue, 3 Mar 2026 17:47:19 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E4=BC=98=E5=8C=96dockerfile=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=B7=AF=E7=94=B1=E5=8C=B9=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 13 ++++++++----- server/initialize/router.go | 11 +++++++++-- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index a9d6c1b..e524de5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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/ ./ diff --git a/server/initialize/router.go b/server/initialize/router.go index a765245..5d8b65c 100644 --- a/server/initialize/router.go +++ b/server/initialize/router.go @@ -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") })