From ab3da5a7307fb9993ba1ff8925b80068e2c86212 Mon Sep 17 00:00:00 2001 From: Sisyphus Date: Tue, 25 Aug 2026 23:26:11 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9C=AC=E5=9C=B0=20dev=20=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=20SSH=20=E9=9A=A7=E9=81=93=E5=A4=8D=E7=94=A8=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E7=8E=AF=E5=A2=83=20PG=EF=BC=88=E5=9B=9E=E9=80=80=20S?= =?UTF-8?q?QLite=EF=BC=89+=20=E6=B5=8B=E8=AF=95=E6=9C=BA=20PG=20=E5=8F=91?= =?UTF-8?q?=E5=B8=83=E5=88=B0=E6=9C=AC=E6=9C=BA=E5=9B=9E=E7=8E=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/deploy-test.yml | 4 ++++ start-dev.bat | 41 ++++++++++++++++++++++++++++---- stop-dev.bat | 10 ++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/.gitea/workflows/deploy-test.yml b/.gitea/workflows/deploy-test.yml index 96875d7..39073ca 100644 --- a/.gitea/workflows/deploy-test.yml +++ b/.gitea/workflows/deploy-test.yml @@ -66,6 +66,10 @@ jobs: POSTGRES_DB: perler_lab volumes: - pg-data:/var/lib/postgresql/data + # 仅发布到测试机本机回环:供开发者 SSH 隧道连入本地 dev 使用, + # 不经过公网网卡,无安全组/DOCKER-USER 暴露问题 + ports: + - "127.0.0.1:15432:5432" healthcheck: test: ["CMD-SHELL", "pg_isready -U perler -d perler_lab"] interval: 5s diff --git a/start-dev.bat b/start-dev.bat index 332fe16..934c38a 100644 --- a/start-dev.bat +++ b/start-dev.bat @@ -53,12 +53,45 @@ if not errorlevel 1 ( ) echo. +rem ---------- 2.5 DB: SSH tunnel to test-env PostgreSQL, fallback SQLite ---------- +rem Preferred: tunnel localhost:15432 -> test machine loopback 15432 -> +rem perler-lab-pg container 5432. Local dev then shares the test-env database +rem (same accounts/data as test.beads.520mxp.cn). +rem Fallback: if ssh is unavailable / test machine unreachable, use a local +rem SQLite file DB (perler-lab-server/data/perler-lab.db) via DB_DRIVER=sqlite. +set "DATABASE_URL=" +set "DB_DRIVER=sqlite" +set "TUNNEL_UP=" + +rem Reuse an already-running tunnel (port 15432 listening) +netstat -ano | findstr ":15432 " | findstr "LISTENING" >nul 2>nul +if not errorlevel 1 set "TUNNEL_UP=1" + +if not defined TUNNEL_UP ( + where ssh >nul 2>nul + if not errorlevel 1 ( + echo [START] Opening SSH tunnel to test-env PostgreSQL (localhost:15432)... + start "PERLER LAB DB Tunnel" /min cmd /c "ssh -N -L 15432:127.0.0.1:15432 -o BatchMode=yes -o StrictHostKeyChecking=accept-new -o ServerAliveInterval=30 test" + rem Wait for the tunnel to come up (6s) + ping -n 7 127.0.0.1 >nul + netstat -ano | findstr ":15432 " | findstr "LISTENING" >nul 2>nul + if not errorlevel 1 set "TUNNEL_UP=1" + ) +) + +if defined TUNNEL_UP ( + echo [INFO] DB = test-env PostgreSQL via tunnel (localhost:15432/perler_lab) + set "DATABASE_URL=postgres://perler:7c5608fe52a5223a6c6107b6@127.0.0.1:15432/perler_lab" + set "DB_DRIVER=" +) else ( + echo [WARN] Tunnel unavailable - DB = local SQLite file (data/perler-lab.db) +) +echo. + rem ---------- 3. Start backend (separate minimized window) ---------- -rem DB_DRIVER=sqlite: local dev runs on a zero-dependency SQLite file DB. -rem The default PostgreSQL (test2, 172.16.0.11:54322) is unreachable from -rem office networks, which crashed the backend on startup (pg connect timeout). +rem DB comes from section 2.5: DATABASE_URL (tunnel) or DB_DRIVER=sqlite (fallback). echo [START] Starting backend service http://localhost:8788 ... -start "PERLER LAB Backend" /min cmd /c "cd /d ""%~dp0perler-lab-server"" && set DB_DRIVER=sqlite&& npx tsx src/server.ts" +start "PERLER LAB Backend" /min cmd /c "cd /d ""%~dp0perler-lab-server"" && npx tsx src/server.ts" rem ---------- 4. Wait 3 seconds for the backend to boot ---------- rem ping is used instead of timeout because timeout fails when stdin is diff --git a/stop-dev.bat b/stop-dev.bat index 6aa9bee..457829a 100644 --- a/stop-dev.bat +++ b/stop-dev.bat @@ -19,6 +19,16 @@ rem ============================================================ echo Stopping PERLER LAB processes (ports 5173 / 8788)... echo. +rem Kill the SSH DB tunnel (port 15432) - only if the process is ssh.exe, +rem so an unrelated service on that port is never touched. +for /f "tokens=5" %%A in ('netstat -ano ^| findstr ":15432 " ^| findstr "LISTENING"') do ( + tasklist /fi "PID eq %%A" 2>nul | findstr /i "ssh.exe" >nul 2>nul + if not errorlevel 1 ( + echo [tunnel] terminating ssh.exe PID %%A + taskkill /F /PID %%A >nul 2>nul + ) +) + set "KILLED_PIDS=" set "FOUND="