优化pc版本首页
This commit is contained in:
+76
-5
@@ -5,6 +5,11 @@ ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||
COMPOSE_FILE="${ROOT_DIR}/deploy/docker-compose.dev.yml"
|
||||
BACKEND_ENV="${ROOT_DIR}/backend/.env"
|
||||
BACKEND_ENV_EXAMPLE="${ROOT_DIR}/backend/.env.example"
|
||||
BACKEND_PORT="${DEV_BACKEND_PORT:-8080}"
|
||||
FRONTEND_PORT="${DEV_FRONTEND_PORT:-5173}"
|
||||
BACKEND_HEALTH_URL="${DEV_BACKEND_HEALTH_URL:-http://127.0.0.1:${BACKEND_PORT}/health}"
|
||||
FRONTEND_HEALTH_URL="${DEV_FRONTEND_HEALTH_URL:-http://127.0.0.1:${FRONTEND_PORT}/}"
|
||||
HTTP_READY_TIMEOUT="${DEV_HTTP_READY_TIMEOUT:-120}"
|
||||
|
||||
BACKEND_PID=""
|
||||
FRONTEND_PID=""
|
||||
@@ -53,6 +58,16 @@ show_help() {
|
||||
|
||||
环境变量:
|
||||
DEV_RESET_DB=1 等同于 --reset-db
|
||||
DEV_BACKEND_PORT=8080
|
||||
后端端口占用检查端口
|
||||
DEV_FRONTEND_PORT=5173
|
||||
前端端口占用检查端口
|
||||
DEV_BACKEND_HEALTH_URL=http://127.0.0.1:8080/health
|
||||
后端健康检查地址
|
||||
DEV_FRONTEND_HEALTH_URL=http://127.0.0.1:5173/
|
||||
前端健康检查地址
|
||||
DEV_HTTP_READY_TIMEOUT=120
|
||||
HTTP 服务就绪等待秒数
|
||||
|
||||
示例:
|
||||
./scripts/dev.sh # 正常启动
|
||||
@@ -135,6 +150,55 @@ wait_container_healthy() {
|
||||
exit 1
|
||||
}
|
||||
|
||||
wait_http_ready() {
|
||||
local url="$1"
|
||||
local label="$2"
|
||||
local pid="${3:-}"
|
||||
local attempt
|
||||
|
||||
log "等待 ${label} HTTP 服务就绪..."
|
||||
for ((attempt = 1; attempt <= HTTP_READY_TIMEOUT; attempt++)); do
|
||||
if [[ -n "${pid}" ]] && ! kill -0 "${pid}" >/dev/null 2>&1; then
|
||||
wait "${pid}" || true
|
||||
log_error "${label} 进程已退出,请检查上方日志"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if curl -fsS --max-time 2 "${url}" >/dev/null 2>&1; then
|
||||
log_success "${label} 已就绪:${url}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
sleep 1
|
||||
done
|
||||
|
||||
log_error "${label} HTTP 服务启动超时:${url}"
|
||||
exit 1
|
||||
}
|
||||
|
||||
ensure_port_free() {
|
||||
local port="$1"
|
||||
local label="$2"
|
||||
|
||||
if command -v lsof >/dev/null 2>&1; then
|
||||
if lsof -nP -iTCP:"${port}" -sTCP:LISTEN >/dev/null 2>&1; then
|
||||
log_error "${label}端口 ${port} 已被占用,请先停止旧进程或调整端口"
|
||||
exit 1
|
||||
fi
|
||||
return 0
|
||||
fi
|
||||
|
||||
if command -v nc >/dev/null 2>&1; then
|
||||
if nc -z 127.0.0.1 "${port}" >/dev/null 2>&1; then
|
||||
log_error "${label}端口 ${port} 已被占用,请先停止旧进程或调整端口"
|
||||
exit 1
|
||||
fi
|
||||
return 0
|
||||
fi
|
||||
|
||||
log_warn "未找到 lsof 或 nc,跳过 ${label}端口 ${port} 占用检查"
|
||||
}
|
||||
|
||||
mysql_root() {
|
||||
docker exec -i -e MYSQL_PWD=rootsecret hfb-mysql mysql -uroot "$@"
|
||||
}
|
||||
@@ -240,6 +304,9 @@ main() {
|
||||
if [[ "${SEED_ONLY}" != "1" && "${NO_FRONTEND}" == "0" ]]; then
|
||||
need_cmd npm
|
||||
fi
|
||||
if [[ "${SEED_ONLY}" != "1" && ( "${NO_BACKEND}" == "0" || "${NO_FRONTEND}" == "0" ) ]]; then
|
||||
need_cmd curl
|
||||
fi
|
||||
|
||||
trap cleanup EXIT
|
||||
trap 'cleanup; exit 130' INT TERM
|
||||
@@ -260,11 +327,20 @@ main() {
|
||||
fi
|
||||
|
||||
if [[ "${NO_BACKEND}" == "0" ]]; then
|
||||
ensure_port_free "${BACKEND_PORT}" "后端"
|
||||
start_backend
|
||||
wait_http_ready "${BACKEND_HEALTH_URL}" "后端" "${BACKEND_PID}"
|
||||
fi
|
||||
|
||||
if [[ "${NO_FRONTEND}" == "0" ]]; then
|
||||
ensure_port_free "${FRONTEND_PORT}" "前端"
|
||||
start_frontend
|
||||
wait_http_ready "${FRONTEND_HEALTH_URL}" "前端" "${FRONTEND_PID}"
|
||||
fi
|
||||
|
||||
if [[ -z "${BACKEND_PID}" && -z "${FRONTEND_PID}" ]]; then
|
||||
log "仅启动 Docker 开发依赖,前后端未启动"
|
||||
return 0
|
||||
fi
|
||||
|
||||
log_success "开发环境已启动"
|
||||
@@ -273,11 +349,6 @@ main() {
|
||||
log "后台:http://localhost:5173/admin/login(admin / admin123456)"
|
||||
fi
|
||||
|
||||
if [[ -z "${BACKEND_PID}" && -z "${FRONTEND_PID}" ]]; then
|
||||
log "仅启动 Docker 开发依赖,前后端未启动"
|
||||
return 0
|
||||
fi
|
||||
|
||||
log "按 Ctrl+C 停止前后端;Docker 依赖会保留运行"
|
||||
|
||||
watch_processes
|
||||
|
||||
Reference in New Issue
Block a user