From a00c1e3e17b9a5bed21b14efef4c7e702ab2840d Mon Sep 17 00:00:00 2001 From: QiuSW <105186638@qq.com> Date: Mon, 27 Jul 2026 11:20:39 +0800 Subject: [PATCH] chore: add Windows backend startup script --- README.md | 15 ++++++++++ start-backend.bat | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 start-backend.bat diff --git a/README.md b/README.md index 6b6852c..5355edb 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,21 @@ fixture,默认 APK 不携带真实订单资料。 ## 后端运行 +在项目根目录可直接使用 Windows 启动脚本。首次运行或更新代码后有 pending migration 时, +显式传入 `--migrate`;脚本不会隐式改动数据库。 + +```powershell +.\start-backend.bat --migrate +``` + +后续启动: + +```powershell +.\start-backend.bat +``` + +等价的手工命令如下: + ```powershell Set-Location backend-api $env:GOTOOLCHAIN = "local" diff --git a/start-backend.bat b/start-backend.bat new file mode 100644 index 0000000..c8bcf58 --- /dev/null +++ b/start-backend.bat @@ -0,0 +1,70 @@ +@echo off +setlocal EnableExtensions DisableDelayedExpansion + +set "PROJECT_ROOT=%~dp0" +set "BACKEND_ROOT=%PROJECT_ROOT%backend-api" + +if not "%~1"=="" if /i not "%~1"=="--migrate" ( + echo Usage: %~nx0 [--migrate] + echo --migrate Apply pending database migrations before starting the API. + exit /b 2 +) + +if not exist "%BACKEND_ROOT%\go.mod" ( + echo Missing backend project: "%BACKEND_ROOT%" + exit /b 1 +) + +where go >nul 2>nul +if errorlevel 1 ( + echo Go 1.23.0 is required but was not found on PATH. + exit /b 1 +) + +set "GOTOOLCHAIN=local" +set "CGO_ENABLED=1" +for /f "tokens=3" %%G in ('go version') do set "GO_VERSION=%%G" +if /i not "%GO_VERSION%"=="go1.23.0" ( + echo Go 1.23.0 is required. Current version: %GO_VERSION% + exit /b 1 +) + +where gcc >nul 2>nul +if errorlevel 1 ( + echo GCC is required because the SQLite driver uses CGO. + exit /b 1 +) + +pushd "%BACKEND_ROOT%" +if errorlevel 1 ( + echo Cannot enter backend directory: "%BACKEND_ROOT%" + exit /b 1 +) + +if not exist "var" mkdir "var" +if errorlevel 1 goto :startup_failed +if not exist "var\assets" mkdir "var\assets" +if errorlevel 1 goto :startup_failed + +if /i "%~1"=="--migrate" ( + echo Applying database migrations... + go run ./cmd/migrate up + if errorlevel 1 goto :startup_failed +) + +if defined CMROUBAO_HTTP_ADDR ( + echo Starting cmroubao backend with configured CMROUBAO_HTTP_ADDR... +) else ( + echo Starting cmroubao backend at 127.0.0.1:8080... +) +echo Press Ctrl+C to stop. +go run ./cmd/api +set "EXIT_CODE=%ERRORLEVEL%" +popd +exit /b %EXIT_CODE% + +:startup_failed +set "EXIT_CODE=%ERRORLEVEL%" +if "%EXIT_CODE%"=="0" set "EXIT_CODE=1" +popd +exit /b %EXIT_CODE%