From e6add93ae36ecf7b2f2b3c57cb97b7a39e3d63c7 Mon Sep 17 00:00:00 2001 From: shaw Date: Sat, 20 Dec 2025 19:13:26 +0800 Subject: [PATCH] fix(build): add -tags embed to ensure frontend is embedded - Add -tags=embed flag to GoReleaser builds - Add -tags embed flag to Dockerfile builds - Fix Dockerfile COPY order to prevent frontend dist being overwritten - Update README build instructions with embed tag explanation --- .goreleaser.yaml | 2 ++ Dockerfile | 11 ++++++----- README.md | 16 ++++++++-------- README_CN.md | 16 ++++++++-------- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 94f1c585..5a0586ab 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -11,6 +11,8 @@ builds: dir: backend main: ./cmd/server binary: sub2api + flags: + - -tags=embed env: - CGO_ENABLED=0 goos: diff --git a/Dockerfile b/Dockerfile index d50e2a0a..098d4e3a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,14 +40,15 @@ WORKDIR /app/backend COPY backend/go.mod backend/go.sum ./ RUN go mod download -# Copy frontend dist from previous stage -COPY --from=frontend-builder /app/frontend/../backend/internal/web/dist ./internal/web/dist - -# Copy backend source +# Copy backend source first COPY backend/ ./ -# Build the binary (BuildType=release for CI builds) +# Copy frontend dist from previous stage (must be after backend copy to avoid being overwritten) +COPY --from=frontend-builder /app/backend/internal/web/dist ./internal/web/dist + +# Build the binary (BuildType=release for CI builds, embed frontend) RUN CGO_ENABLED=0 GOOS=linux go build \ + -tags embed \ -ldflags="-s -w -X main.Commit=${COMMIT} -X main.Date=${DATE:-$(date -u +%Y-%m-%dT%H:%M:%SZ)} -X main.BuildType=release" \ -o /app/sub2api \ ./cmd/server diff --git a/README.md b/README.md index 5606a61b..e25a6e8a 100644 --- a/README.md +++ b/README.md @@ -220,21 +220,21 @@ cd sub2api cd frontend npm install npm run build +# Output will be in ../backend/internal/web/dist/ -# 3. Copy frontend build to backend (for embedding) -cp -r dist ../backend/internal/web/ - -# 4. Build backend (requires frontend dist to be present) +# 3. Build backend with embedded frontend cd ../backend -go build -o sub2api ./cmd/server +go build -tags embed -o sub2api ./cmd/server -# 5. Create configuration file +# 4. Create configuration file cp ../deploy/config.example.yaml ./config.yaml -# 6. Edit configuration +# 5. Edit configuration nano config.yaml ``` +> **Note:** The `-tags embed` flag embeds the frontend into the binary. Without this flag, the binary will not serve the frontend UI. + **Key configuration in `config.yaml`:** ```yaml @@ -265,7 +265,7 @@ default: ``` ```bash -# 7. Run the application +# 6. Run the application ./sub2api ``` diff --git a/README_CN.md b/README_CN.md index ced2e096..a93fb9d8 100644 --- a/README_CN.md +++ b/README_CN.md @@ -220,21 +220,21 @@ cd sub2api cd frontend npm install npm run build +# 构建产物输出到 ../backend/internal/web/dist/ -# 3. 复制前端构建产物到后端(用于嵌入) -cp -r dist ../backend/internal/web/ - -# 4. 编译后端(需要前端 dist 目录存在) +# 3. 编译后端(嵌入前端) cd ../backend -go build -o sub2api ./cmd/server +go build -tags embed -o sub2api ./cmd/server -# 5. 创建配置文件 +# 4. 创建配置文件 cp ../deploy/config.example.yaml ./config.yaml -# 6. 编辑配置 +# 5. 编辑配置 nano config.yaml ``` +> **注意:** `-tags embed` 参数会将前端嵌入到二进制文件中。不使用此参数编译的程序将不包含前端界面。 + **`config.yaml` 关键配置:** ```yaml @@ -265,7 +265,7 @@ default: ``` ```bash -# 7. 运行应用 +# 6. 运行应用 ./sub2api ```