fix: harden source startup script

This commit is contained in:
2026-04-23 10:28:20 +08:00
parent 3bd175a2e6
commit 562c95c4b5

View File

@@ -3,22 +3,33 @@ setlocal
set "ROOT=%~dp0" set "ROOT=%~dp0"
set "BACKEND_DIR=%ROOT%backend" set "BACKEND_DIR=%ROOT%backend"
set "VENV_PYTHON=%BACKEND_DIR%\.venv\Scripts\python.exe" set "VENV_DIR=%BACKEND_DIR%\.venv"
set "SOURCE_BACKEND_ENV=set DATABASE_URL=sqlite:///./aivideo.sqlite3 && set CELERY_TASK_ALWAYS_EAGER=true && set STORAGE_PROVIDER=local && set LOCAL_STORAGE_PATH=storage_data && set STORAGE_PUBLIC_BASE_URL=http://127.0.0.1:8000/storage" set "VENV_PYTHON=%VENV_DIR%\Scripts\python.exe"
if /i "%~1"==":run_backend" goto run_backend
if /i "%~1"==":run_frontend" goto run_frontend
cd /d "%ROOT%" cd /d "%ROOT%"
echo [1/6] Checking Python... echo [1/6] Checking Python...
set "PYTHON_CMD=" set "PYTHON_CMD="
where py >nul 2>nul where py >nul 2>nul
if not errorlevel 1 (
py -3 -c "import sys" >nul 2>nul
if not errorlevel 1 set "PYTHON_CMD=py -3" if not errorlevel 1 set "PYTHON_CMD=py -3"
if not defined PYTHON_CMD (
where python >nul 2>nul
if not errorlevel 1 set "PYTHON_CMD=python"
) )
if not defined PYTHON_CMD ( if not defined PYTHON_CMD (
echo Python was not found in PATH. where python >nul 2>nul
if not errorlevel 1 (
python -c "import sys" >nul 2>nul
if not errorlevel 1 set "PYTHON_CMD=python"
)
)
if not defined PYTHON_CMD (
echo A working Python 3 installation was not found.
echo Install Python 3.11 or later, then make sure py -3 or python works in a new terminal.
pause pause
exit /b 1 exit /b 1
) )
@@ -41,9 +52,25 @@ if exist "%BACKEND_DIR%\.env" (
echo The backend will start with built-in defaults ^(SQLite, local storage, mock task flow^). echo The backend will start with built-in defaults ^(SQLite, local storage, mock task flow^).
) )
if not exist "%VENV_PYTHON%" ( set "VENV_READY="
if exist "%VENV_PYTHON%" (
"%VENV_PYTHON%" -c "import sys" >nul 2>nul
if not errorlevel 1 set "VENV_READY=1"
)
if not defined VENV_READY (
if exist "%VENV_DIR%" (
echo Existing backend virtual environment is broken or stale. Recreating it...
rmdir /s /q "%VENV_DIR%"
if exist "%VENV_DIR%" (
echo Failed to remove the broken backend virtual environment.
pause
exit /b 1
)
)
echo [4/6] Creating backend virtual environment... echo [4/6] Creating backend virtual environment...
call %PYTHON_CMD% -m venv "%BACKEND_DIR%\.venv" %PYTHON_CMD% -m venv "%VENV_DIR%"
if errorlevel 1 ( if errorlevel 1 (
echo Failed to create backend virtual environment. echo Failed to create backend virtual environment.
pause pause
@@ -51,7 +78,7 @@ if not exist "%VENV_PYTHON%" (
) )
echo Installing backend dependencies... echo Installing backend dependencies...
call "%VENV_PYTHON%" -m pip install --disable-pip-version-check -r "%BACKEND_DIR%\requirements.txt" "%VENV_PYTHON%" -m pip install --disable-pip-version-check -r "%BACKEND_DIR%\requirements.txt"
if errorlevel 1 ( if errorlevel 1 (
echo Failed to install backend dependencies. echo Failed to install backend dependencies.
pause pause
@@ -74,8 +101,8 @@ if not exist "%ROOT%node_modules" (
) )
echo [6/6] Starting backend and unified frontend... echo [6/6] Starting backend and unified frontend...
start "AIVideo Backend" cmd /k "cd /d ""%BACKEND_DIR%"" && %SOURCE_BACKEND_ENV% && ""%VENV_PYTHON%"" -m uvicorn app.main:app --reload --host 0.0.0.0 --port 8000" start "AIVideo Backend" cmd /k call "%~f0" :run_backend
start "AIVideo Frontend" cmd /k "cd /d ""%ROOT%"" && npm run dev" start "AIVideo Frontend" cmd /k call "%~f0" :run_frontend
echo. echo.
echo User UI: http://localhost:3000 echo User UI: http://localhost:3000
@@ -84,3 +111,19 @@ echo API: http://localhost:8000
echo. echo.
echo Two windows have been opened for backend and frontend. echo Two windows have been opened for backend and frontend.
pause pause
goto :eof
:run_backend
cd /d "%BACKEND_DIR%"
set "DATABASE_URL=sqlite:///./aivideo.sqlite3"
set "CELERY_TASK_ALWAYS_EAGER=true"
set "STORAGE_PROVIDER=local"
set "LOCAL_STORAGE_PATH=storage_data"
set "STORAGE_PUBLIC_BASE_URL=http://127.0.0.1:8000/storage"
"%VENV_PYTHON%" -m uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
goto :eof
:run_frontend
cd /d "%ROOT%"
npm run dev
goto :eof