chore: add Windows backend startup script

This commit is contained in:
QiuSW
2026-07-27 11:20:39 +08:00
parent 45b66eeea7
commit a00c1e3e17
2 changed files with 85 additions and 0 deletions
+15
View File
@@ -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"
+70
View File
@@ -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%