-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.cmd
More file actions
183 lines (161 loc) · 5.47 KB
/
start.cmd
File metadata and controls
183 lines (161 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
@echo off
setlocal EnableExtensions EnableDelayedExpansion
rem Usage:
rem start.cmd -> start backend + frontend + ngrok (fast mode, recommended)
rem start.cmd dev -> start backend + frontend(dev) + ngrok
rem start.cmd initdb -> init db then start backend + frontend + ngrok
rem start.cmd jishe 3000 -> custom env/frontendPort (backend port auto-detected)
set "INITDB=0"
set "FAST_MODE=1"
set "CONDA_ENV=jishe"
set "BACKEND_PORT_RESOLVED="
set "FRONTEND_PORT=3000"
set "NGROK_BIN="
set "NGROK_PRIMARY=%LOCALAPPDATA%\Microsoft\WinGet\Packages\Ngrok.Ngrok_Microsoft.Winget.Source_8wekyb3d8bbwe\ngrok.exe"
if /I "%~1"=="initdb" (
set "INITDB=1"
shift
)
if /I "%~1"=="dev" (
set "FAST_MODE=0"
shift
)
if /I "%~1"=="fast" (
set "FAST_MODE=1"
shift
)
if not "%~1"=="" set "CONDA_ENV=%~1"
if not "%~2"=="" set "FRONTEND_PORT=%~2"
set "REPO_ROOT=%~dp0"
set "BACKEND_DIR=%REPO_ROOT%backend"
set "FRONTEND_DIR=%REPO_ROOT%frontend"
set "BACKEND_PYTHON="
if not exist "%BACKEND_DIR%" (
echo Backend dir not found: "%BACKEND_DIR%"
exit /b 1
)
if not exist "%FRONTEND_DIR%" (
echo Frontend dir not found: "%FRONTEND_DIR%"
exit /b 1
)
where conda >nul 2>nul
if errorlevel 1 (
echo conda not found. Please install Anaconda/Miniconda and add it to PATH.
exit /b 1
)
where npm >nul 2>nul
if errorlevel 1 (
echo npm not found. Please install Node.js and add it to PATH.
exit /b 1
)
if exist "%NGROK_PRIMARY%" set "NGROK_BIN=%NGROK_PRIMARY%"
if not defined NGROK_BIN (
for /r "%LOCALAPPDATA%\Microsoft\WinGet\Packages" %%I in (ngrok.exe) do (
echo %%~fI | findstr /I /C:"\Ngrok.Ngrok_" >nul
if not errorlevel 1 (
set "NGROK_BIN=%%~fI"
goto :ngrok_found
)
)
)
if not defined NGROK_BIN (
where ngrok >nul 2>nul
if not errorlevel 1 set "NGROK_BIN=ngrok"
)
:ngrok_found
rem === Find python executable ===
for %%P in (
"%USERPROFILE%\anaconda3\envs\%CONDA_ENV%\python.exe"
"%USERPROFILE%\miniconda3\envs\%CONDA_ENV%\python.exe"
"E:\anaconda3\envs\%CONDA_ENV%\python.exe"
"D:\anaconda3\envs\%CONDA_ENV%\python.exe"
) do (
if exist "%%~fP" (
set "BACKEND_PYTHON=%%~fP"
goto :python_found
)
)
:python_found
rem === Auto-detect backend port via TcpListener (skips Windows reserved ports) ===
echo Detecting available backend port...
for /l %%P in (9000,1,9200) do (
if not defined BACKEND_PORT_RESOLVED call :find_port %%P
)
if not defined BACKEND_PORT_RESOLVED (
echo No available port found in 9000-9200. Aborting.
pause
exit /b 1
)
set "BACKEND_PORT=%BACKEND_PORT_RESOLVED%"
echo Found free port: %BACKEND_PORT%
echo Starting backend: http://localhost:%BACKEND_PORT%
if "%FAST_MODE%"=="1" (
echo Starting frontend fast-static: http://localhost:%FRONTEND_PORT%
) else (
echo Starting frontend dev: http://localhost:%FRONTEND_PORT%
)
if defined NGROK_BIN (
echo Starting ngrok tunnel for frontend port %FRONTEND_PORT%
) else (
echo ngrok not found, app will start without tunnel.
)
if "%INITDB%"=="1" (
echo Initializing DB...
pushd "%BACKEND_DIR%" >nul
conda run --no-capture-output -n "%CONDA_ENV%" python scripts\init_db.py
if errorlevel 1 (
popd >nul
echo DB init failed.
exit /b 1
)
popd >nul
)
if defined BACKEND_PYTHON (
start "Backend" cmd /k "cd /d ""%BACKEND_DIR%"" && ""%BACKEND_PYTHON%"" -m uvicorn main:app --host 0.0.0.0 --port %BACKEND_PORT% || (echo Backend failed to start. & pause)"
) else (
start "Backend" cmd /k "cd /d ""%BACKEND_DIR%"" && conda run --no-capture-output -n ""%CONDA_ENV%"" python -m uvicorn main:app --host 0.0.0.0 --port %BACKEND_PORT% || (echo Backend failed to start. & pause)"
)
if "%FAST_MODE%"=="1" (
start "Frontend" cmd /k "cd /d ""%FRONTEND_DIR%"" && npm run build && node scripts/static-proxy-server.mjs http://127.0.0.1:%BACKEND_PORT% %FRONTEND_PORT% 0.0.0.0 || (echo Frontend failed to start. & pause)"
) else (
start "Frontend" cmd /k "cd /d ""%FRONTEND_DIR%"" && set ""VITE_API_PROXY_TARGET=http://127.0.0.1:%BACKEND_PORT%"" && npm run dev -- --host 0.0.0.0 --port %FRONTEND_PORT% || (echo Frontend failed to start. & pause)"
)
if defined NGROK_BIN (
call :wait_frontend_and_start_ngrok
)
endlocal
exit /b 0
rem === Subroutine: test if port is truly bindable via TcpListener ===
:find_port
powershell -NoProfile -ExecutionPolicy Bypass -Command "try { $l=[System.Net.Sockets.TcpListener]::new([System.Net.IPAddress]::Any,%1); $l.Start(); $l.Stop(); exit 0 } catch { exit 1 }" >nul 2>nul
if errorlevel 1 goto :eof
set "BACKEND_PORT_RESOLVED=%1"
goto :eof
:wait_frontend_and_start_ngrok
setlocal EnableExtensions EnableDelayedExpansion
set "MAX_WAIT_SECONDS=30"
set "WAITED_SECONDS=0"
where curl >nul 2>nul
if errorlevel 1 (
timeout /t 3 >nul
start "Tunnel (ngrok)" cmd /k "echo [Tunnel] %NGROK_BIN% http %FRONTEND_PORT% && ""%NGROK_BIN%"" http %FRONTEND_PORT% || (echo ngrok failed to start. & pause)"
endlocal
exit /b 0
)
echo Waiting frontend on http://127.0.0.1:%FRONTEND_PORT% (max %MAX_WAIT_SECONDS%s)...
:wait_frontend_loop
curl -sS -o nul "http://127.0.0.1:%FRONTEND_PORT%/" >nul 2>nul
if not errorlevel 1 (
echo Frontend is ready. Starting ngrok...
start "Tunnel (ngrok)" cmd /k "echo [Tunnel] %NGROK_BIN% http %FRONTEND_PORT% && ""%NGROK_BIN%"" http %FRONTEND_PORT% || (echo ngrok failed to start. & pause)"
endlocal
exit /b 0
)
if !WAITED_SECONDS! GEQ !MAX_WAIT_SECONDS! (
echo Frontend not ready in %MAX_WAIT_SECONDS%s. ngrok not started automatically.
endlocal
exit /b 0
)
set /a WAITED_SECONDS+=1
timeout /t 1 >nul
goto :wait_frontend_loop