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