Files
deploy-test/09-verify-static-frontends.sh
2026-04-21 12:24:04 +07:00

105 lines
3.3 KiB
Bash

#!/usr/bin/env bash
# =============================================================================
# 09-verify-static-frontends.sh — 验证静态前端与 Nginx 域名入口
#
# 用法:
# ./09-verify-static-frontends.sh
#
# 检查项:
# - /app/pc/dist / /app/cms/dist / /app/build-cms/dist / /app/build-down/dist 是否存在
# - Nginx Host 路由是否可达
# - 站点首页是否能返回 HTML
# =============================================================================
set -euo pipefail
source "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/common.sh"
init_dirs
init_script_log
if [[ -f "$ENV_FILE" ]]; then
set -a
# shellcheck source=/dev/null
source "$ENV_FILE"
set +a
fi
normalize_pc_proxy_env
require_tools curl
header "验证静态前端"
check_dist() {
local label="$1" path="$2"
if [[ -f "$path" ]]; then
success "$label dist 存在: $path"
else
error "$label dist 不存在: $path"
return 1
fi
}
check_site() {
local label="$1" host="$2" expected_path="$3" expected_title="${4:-}"
local body_file
local actual_title
body_file="$(mktemp)"
if curl -fsS --max-time 5 -H "Host: ${host}" http://127.0.0.1/nginx-health >/dev/null 2>&1; then
success "$label nginx-health 可达: http://${host}/nginx-health"
else
error "$label nginx-health 不可达: http://${host}/nginx-health"
rm -f "$body_file"
return 1
fi
if curl -fsS --max-time 10 -H "Host: ${host}" http://127.0.0.1/ > "$body_file"; then
if grep -qi "<html" "$body_file"; then
success "$label 首页可达: http://${host}/"
actual_title=$(grep -oiE '<title>[^<]*</title>' "$body_file" | head -1 | sed -E 's#</?title>##g' || true)
[[ -n "$actual_title" ]] && info " ${label} title: ${actual_title}"
info " ${label} dist: ${expected_path}"
if [[ -n "$expected_title" ]]; then
if [[ "$actual_title" == "$expected_title" ]]; then
success "$label title 符合预期"
else
error "$label title 不符合预期: 期望=${expected_title} 实际=${actual_title:-<empty>}"
rm -f "$body_file"
return 1
fi
fi
else
error "$label 首页返回成功但不是 HTML: http://${host}/"
rm -f "$body_file"
return 1
fi
else
error "$label 首页不可达: http://${host}/"
rm -f "$body_file"
return 1
fi
rm -f "$body_file"
}
FAILED=()
check_dist "PC" "$ROOT_DIR/pc/dist/index.html" || FAILED+=("pc-dist")
check_dist "CMS" "$ROOT_DIR/cms/dist/index.html" || FAILED+=("cms-dist")
check_dist "Build CMS" "$ROOT_DIR/build-cms/dist/index.html" || FAILED+=("build-cms-dist")
check_dist "Build Down" "$ROOT_DIR/build-down/dist/index.html" || FAILED+=("build-down-dist")
check_site "PC" "${PC_PROXY_DOMAIN}" "$ROOT_DIR/pc/dist/index.html" || FAILED+=("pc-nginx")
check_site "CMS" "cms-jack.imharry.work" "$ROOT_DIR/cms/dist/index.html" "欢迎使用MChat" || FAILED+=("cms-nginx")
check_site "Build CMS" "build-jack.imharry.work" "$ROOT_DIR/build-cms/dist/index.html" "打包管理系统" || FAILED+=("build-cms-nginx")
check_site "Build Down" "down-jack.imharry.work" "$ROOT_DIR/build-down/dist/index.html" "下载" || FAILED+=("build-down-nginx")
echo ""
pc_probe_msg_gateway "${PC_BACKEND_ORIGIN}"
echo ""
if [[ ${#FAILED[@]} -eq 0 ]]; then
success "静态前端验证通过"
else
error "以下检查失败: ${FAILED[*]}"
exit 1
fi