From 6a16edfa24ad3590588516d76d9f6b94fdd86a72 Mon Sep 17 00:00:00 2001 From: copper-xw Date: Wed, 30 Sep 2026 17:39:03 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=AD=A3=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E5=B0=B1=E7=BB=AA=E6=8E=A2=E6=B5=8B=E5=B9=B6?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E7=AB=AF=E5=8F=A3=E5=8F=AF=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MySQL 就绪探测改为走 TCP(127.0.0.1:3306),避免初始化阶段只监听 socket 的临时服务器让探测提前通过,导致后端连入时遇到 unexpected EOF。 后端监听端口改为读取 APP_PORT(默认 8082),与脚本探测的空闲端口一致, 并在启动时和最终提示中打印完整访问地址;npm 默认使用 npmmirror 镜像, 同时将根目录编译产物 /server 加入忽略。 --- .gitignore | 1 + cmd/server/main.go | 10 ++++++++-- dev-verify.sh | 18 ++++++++++-------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 98dfff04..0ff9890a 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ node_modules/ *.exe~ nul +/server server.exe warehouse-mms.exe frontend/dist/ diff --git a/cmd/server/main.go b/cmd/server/main.go index 83d099fb..ac82f5c8 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -50,9 +50,15 @@ func main() { // WebSocket route 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") - if err := r.Run(":8082"); err != nil { + if err := r.Run(addr); err != nil { log.Fatalf("Failed to start server: %v", err) } } diff --git a/dev-verify.sh b/dev-verify.sh index 14ef1352..dbff838d 100755 --- a/dev-verify.sh +++ b/dev-verify.sh @@ -13,7 +13,7 @@ # APP_PORT 期望的网页端口;被占用时自动顺延(默认 8080) # DB_PORT 期望映射到本机的 MySQL 端口;被占用时顺延(默认 13306) # KEEP_DATA=1 退出时保留 .dev-run/ 和 MySQL 容器(默认全部删除) -# NPM_REGISTRY 重装依赖时使用的 npm 镜像源(如 https://registry.npmmirror.com) +# NPM_REGISTRY 重装依赖时使用的 npm 镜像源(默认 https://registry.npmmirror.com) set -euo pipefail @@ -23,6 +23,8 @@ RUNDIR="$PWD/.dev-run" DB_CONTAINER="wmms-dev-mysql" DB_PASSWORD="wmms1234" DB_NAME="wmms" +# 默认走国内镜像,避免官方源 ECONNRESET;可用 NPM_REGISTRY 覆盖。 +NPM_REGISTRY="${NPM_REGISTRY:-https://registry.npmmirror.com}" find_free_port() { local start="$1" p="$1" @@ -70,11 +72,7 @@ echo "==> 1/4 构建前端(vite build)" need_install=1 fi if [ "$need_install" = "1" ]; then - if [ -n "${NPM_REGISTRY:-}" ]; then - npm ci --registry="$NPM_REGISTRY" - else - npm ci - fi + npm ci --registry="$NPM_REGISTRY" else chmod +x node_modules/.bin/* 2>/dev/null || true fi @@ -102,7 +100,10 @@ docker run -d --name "$DB_CONTAINER" \ echo " 等待 MySQL 就绪..." 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 fi if ! docker ps --format '{{.Names}}' | grep -qx "$DB_CONTAINER"; then @@ -114,8 +115,9 @@ for ((i = 0; i < 90; i++)); do done 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" -DB_DSN="$DSN" "$BIN" & +APP_PORT="$APP_PORT" DB_DSN="$DSN" "$BIN" & PID=$! READY=0