Files
cmroubao/start-backend.bat
T

112 lines
2.8 KiB
Batchfile

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "PROJECT_ROOT=%~dp0"
set "SCRIPT_NAME=%~nx0"
set "BACKEND_ROOT=%PROJECT_ROOT%backend-api"
set "LOCAL_TLS_DIR=%PROJECT_ROOT%.local\cmroubao-tls"
set "LOCAL_TLS_CERT=%LOCAL_TLS_DIR%\server.crt"
set "LOCAL_TLS_KEY=%LOCAL_TLS_DIR%\server.key"
if not defined CMROUBAO_HTTP_ADDR (
if exist "%LOCAL_TLS_CERT%" if exist "%LOCAL_TLS_KEY%" (
set "CMROUBAO_HTTP_ADDR=0.0.0.0:8080"
)
)
if not defined CMROUBAO_TLS_CERT_FILE (
if not defined CMROUBAO_TLS_KEY_FILE (
if exist "%LOCAL_TLS_CERT%" if exist "%LOCAL_TLS_KEY%" (
set "CMROUBAO_TLS_CERT_FILE=%LOCAL_TLS_CERT%"
set "CMROUBAO_TLS_KEY_FILE=%LOCAL_TLS_KEY%"
)
)
)
set "RUN_MIGRATIONS=0"
set "ERP_DEBUG_LOG=0"
:parse_arguments
if "%~1"=="" goto :arguments_parsed
if /i "%~1"=="--migrate" (
if "%RUN_MIGRATIONS%"=="1" goto :usage
set "RUN_MIGRATIONS=1"
shift
goto :parse_arguments
)
if /i "%~1"=="--erp-debug" (
if "%ERP_DEBUG_LOG%"=="1" goto :usage
set "ERP_DEBUG_LOG=1"
shift
goto :parse_arguments
)
goto :usage
:usage
echo Usage: %SCRIPT_NAME% [--migrate] [--erp-debug]
echo --migrate Apply pending database migrations before starting the API.
echo --erp-debug Print redacted ERP request and response diagnostics for this process.
exit /b 2
:arguments_parsed
if "%ERP_DEBUG_LOG%"=="1" set "CMROUBAO_ERP_DEBUG_LOG=true"
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 "%RUN_MIGRATIONS%"=="1" (
echo Applying database migrations...
go run ./cmd/migrate up
if errorlevel 1 goto :startup_failed
)
if defined CMROUBAO_TLS_CERT_FILE (
echo Starting cmroubao backend with TLS at %CMROUBAO_HTTP_ADDR%...
) else 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%