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") })