fix: 修正试用脚本就绪探测并支持端口可配置

MySQL 就绪探测改为走 TCP(127.0.0.1:3306),避免初始化阶段只监听 socket
的临时服务器让探测提前通过,导致后端连入时遇到 unexpected EOF。
后端监听端口改为读取 APP_PORT(默认 8082),与脚本探测的空闲端口一致,
并在启动时和最终提示中打印完整访问地址;npm 默认使用 npmmirror 镜像,
同时将根目录编译产物 /server 加入忽略。
This commit is contained in:
copper-xw 2026-09-30 17:39:03 +08:00
parent ca26f625f5
commit 6a16edfa24
3 changed files with 19 additions and 10 deletions

1
.gitignore vendored
View File

@ -5,6 +5,7 @@
node_modules/ node_modules/
*.exe~ *.exe~
nul nul
/server
server.exe server.exe
warehouse-mms.exe warehouse-mms.exe
frontend/dist/ frontend/dist/

View File

@ -50,9 +50,15 @@ func main() {
// WebSocket route // WebSocket route
r.GET("/ws", handler.HandleWebSocket) r.GET("/ws", handler.HandleWebSocket)
log.Println("Server starting on :8082") // 端口可通过 APP_PORT 覆盖,默认 8082,便于试用脚本探测空闲端口。
port := os.Getenv("APP_PORT")
if port == "" {
port = "8082"
}
addr := ":" + port
log.Printf("Server starting on %s", addr)
log.Println("Default users: admin/admin123, user/user123") log.Println("Default users: admin/admin123, user/user123")
if err := r.Run(":8082"); err != nil { if err := r.Run(addr); err != nil {
log.Fatalf("Failed to start server: %v", err) log.Fatalf("Failed to start server: %v", err)
} }
} }

View File

@ -13,7 +13,7 @@
# APP_PORT 期望的网页端口;被占用时自动顺延(默认 8080) # APP_PORT 期望的网页端口;被占用时自动顺延(默认 8080)
# DB_PORT 期望映射到本机的 MySQL 端口;被占用时顺延(默认 13306) # DB_PORT 期望映射到本机的 MySQL 端口;被占用时顺延(默认 13306)
# KEEP_DATA=1 退出时保留 .dev-run/ 和 MySQL 容器(默认全部删除) # KEEP_DATA=1 退出时保留 .dev-run/ 和 MySQL 容器(默认全部删除)
# NPM_REGISTRY 重装依赖时使用的 npm 镜像源(如 https://registry.npmmirror.com) # NPM_REGISTRY 重装依赖时使用的 npm 镜像源(默认 https://registry.npmmirror.com)
set -euo pipefail set -euo pipefail
@ -23,6 +23,8 @@ RUNDIR="$PWD/.dev-run"
DB_CONTAINER="wmms-dev-mysql" DB_CONTAINER="wmms-dev-mysql"
DB_PASSWORD="wmms1234" DB_PASSWORD="wmms1234"
DB_NAME="wmms" DB_NAME="wmms"
# 默认走国内镜像,避免官方源 ECONNRESET;可用 NPM_REGISTRY 覆盖。
NPM_REGISTRY="${NPM_REGISTRY:-https://registry.npmmirror.com}"
find_free_port() { find_free_port() {
local start="$1" p="$1" local start="$1" p="$1"
@ -70,11 +72,7 @@ echo "==> 1/4 构建前端(vite build)"
need_install=1 need_install=1
fi fi
if [ "$need_install" = "1" ]; then if [ "$need_install" = "1" ]; then
if [ -n "${NPM_REGISTRY:-}" ]; then npm ci --registry="$NPM_REGISTRY"
npm ci --registry="$NPM_REGISTRY"
else
npm ci
fi
else else
chmod +x node_modules/.bin/* 2>/dev/null || true chmod +x node_modules/.bin/* 2>/dev/null || true
fi fi
@ -102,7 +100,10 @@ docker run -d --name "$DB_CONTAINER" \
echo " 等待 MySQL 就绪..." echo " 等待 MySQL 就绪..."
for ((i = 0; i < 90; i++)); do for ((i = 0; i < 90; i++)); do
if docker exec "$DB_CONTAINER" mysqladmin ping -h localhost -uroot -p"$DB_PASSWORD" --silent >/dev/null 2>&1; then # 必须走 TCP(127.0.0.1:3306),不能用 localhost 的 socket:初始化阶段的
# 临时服务器只监听 socket,socket ping 会提前成功,但 3306 尚未就绪,
# 后端连入时正逢重启,报 unexpected EOF / bad connection。
if docker exec "$DB_CONTAINER" mysqladmin ping -h 127.0.0.1 -P 3306 -uroot -p"$DB_PASSWORD" --silent >/dev/null 2>&1; then
break break
fi fi
if ! docker ps --format '{{.Names}}' | grep -qx "$DB_CONTAINER"; then if ! docker ps --format '{{.Names}}' | grep -qx "$DB_CONTAINER"; then
@ -114,8 +115,9 @@ for ((i = 0; i < 90; i++)); do
done done
echo "==> 4/4 启动后端(端口 $APP_PORT)" echo "==> 4/4 启动后端(端口 $APP_PORT)"
echo " 访问地址: http://localhost:$APP_PORT"
DSN="root:$DB_PASSWORD@tcp(127.0.0.1:$DB_PORT)/$DB_NAME?charset=utf8mb4&parseTime=true" DSN="root:$DB_PASSWORD@tcp(127.0.0.1:$DB_PORT)/$DB_NAME?charset=utf8mb4&parseTime=true"
DB_DSN="$DSN" "$BIN" & APP_PORT="$APP_PORT" DB_DSN="$DSN" "$BIN" &
PID=$! PID=$!
READY=0 READY=0