Files
X-Financial/web/start.sh
caoxiaozhu 828e8f5aaf feat: 优化网络绑定配置支持外部访问
主要修改点:
1. 网络绑定调整
   - .env.example: WEB_HOST/VITE_WEB_HOST 从 127.0.0.1 改为 0.0.0.0
   - server/src/app/core/config.py: 默认 web_host 从 127.0.0.1 改为 0.0.0.0
   - web/package.json: Vite 脚本 host 从 127.0.0.1 改为 0.0.0.0
   - web/vite.config.js: normalizeState 中 WEB_HOST 默认值从 127.0.0.1 改为 0.0.0.0

2. CORS 配置扩展
   - .env.example: CORS_ORIGINS 添加 http://0.0.0.0:5173

3. Shell 脚本权限修复
   - server/start.sh, start.sh: 添加可执行权限 (644 -> 755)

4. Setup 表单与验证逻辑简化
   - web/src/composables/useSetupView.js:
     - 新增 readCurrentWebEndpoint() 从 window.location 自动检测当前 web host/port
     - 简化 runtimeInputsReady: 移除 web_host/web_port 必填验证,仅保留 server_host/server_port
     - 简化 buildRuntimeFingerprint(): 移除 web_host/web_port
     - buildPayload() 改用 readCurrentWebEndpoint() 解析 web 配置
   - web/vite.config.js:
     - 新增 resolveRuntimePayload(): 运行时解析 web_host/web_port
     - 移除 validateRuntimePayload() 中的 web_host/web_port 字段验证
     - 移除 testRuntimePorts() 中 web 端口冲突检测逻辑
     - 移除 canReuseCurrentWebPort() 函数

5. CSS 清理
   - web/src/assets/styles/views/setup-view.css: 移除未使用的 .setup-summary-grid 和 .setup-summary-item 样式
2026-05-08 09:27:45 +08:00

178 lines
4.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
export MSYS_NO_PATHCONV=1
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
ROOT_ENV_FILE="$ROOT_DIR/.env"
MODE="${1:-start}"
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
info() { echo -e "${GREEN}[INFO]${NC} $*"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
error() { echo -e "${RED}[ERROR]${NC} $*"; exit 1; }
if [ -f "$ROOT_ENV_FILE" ]; then
set -a
. "$ROOT_ENV_FILE"
set +a
fi
WEB_HOST="${WEB_HOST:-0.0.0.0}"
WEB_PORT="${WEB_PORT:-5173}"
export VITE_SETUP_COMPLETED="${SETUP_COMPLETED:-false}"
export VITE_COMPANY_NAME="${COMPANY_NAME:-}"
export VITE_COMPANY_CODE="${COMPANY_CODE:-}"
export VITE_ADMIN_EMAIL="${ADMIN_EMAIL:-}"
export VITE_WEB_HOST="${WEB_HOST}"
export VITE_WEB_PORT="${WEB_PORT}"
export VITE_SERVER_HOST="${SERVER_HOST:-127.0.0.1}"
export VITE_SERVER_PORT="${SERVER_PORT:-8000}"
export VITE_POSTGRES_HOST="${POSTGRES_HOST:-127.0.0.1}"
export VITE_POSTGRES_PORT="${POSTGRES_PORT:-5432}"
export VITE_POSTGRES_DB="${POSTGRES_DB:-x_financial}"
export VITE_POSTGRES_USER="${POSTGRES_USER:-postgres}"
export VITE_REDIS_URL="${REDIS_URL:-}"
is_wsl() {
grep -qi microsoft /proc/version 2>/dev/null
}
is_windows_path() {
case "$SCRIPT_DIR" in
/mnt/*) return 0 ;;
/d/*|/c/*|/e/*|/f/*) return 0 ;;
*) return 1 ;;
esac
}
use_windows_npm() {
is_wsl && is_windows_path && command -v powershell.exe >/dev/null 2>&1 && command -v wslpath >/dev/null 2>&1
}
windows_project_path() {
wslpath -w "$SCRIPT_DIR"
}
run_windows_powershell() {
local command="$1"
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "$command"
}
run_windows_npm_install() {
local win_path
local win_path_ps
win_path="$(windows_project_path)"
win_path_ps="${win_path//\'/\'\'}"
run_windows_powershell "Set-Location -LiteralPath '$win_path_ps'; npm install"
}
run_windows_npm_start() {
local win_path
local win_path_ps
win_path="$(windows_project_path)"
win_path_ps="${win_path//\'/\'\'}"
exec powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Set-Location -LiteralPath '$win_path_ps'; npm start -- --host $WEB_HOST --port $WEB_PORT"
}
dependencies_ready() {
[ -d "node_modules" ] || return 1
[ -f "node_modules/vite/bin/vite.js" ] || return 1
[ -f "node_modules/pg/package.json" ] || return 1
[ -f "node_modules/vue-router/package.json" ] || return 1
if use_windows_npm; then
local win_path
local win_path_ps
win_path="$(windows_project_path)"
win_path_ps="${win_path//\'/\'\'}"
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Set-Location -LiteralPath '$win_path_ps'; node -e \"require('rollup'); require('pg'); require('vue-router')\"" >/dev/null 2>&1
return $?
fi
[ -e "node_modules/.bin/vite" ] || [ -e "node_modules/.bin/vite.cmd" ] || return 1
node -e "require('rollup'); require('pg'); require('vue-router')" >/dev/null 2>&1
}
ensure_runtime_tools() {
if use_windows_npm; then
info "Detected WSL on a Windows-mounted project"
info "Using Windows npm to manage web dependencies"
if ! run_windows_powershell "node -v > \$null; npm -v > \$null" >/dev/null 2>&1; then
error "Windows Node.js/npm is not available in PATH. Install Node.js on Windows first."
fi
return 0
fi
if ! command -v node >/dev/null 2>&1; then
error "Node.js is not installed. Install it first: https://nodejs.org"
fi
if ! command -v npm >/dev/null 2>&1; then
error "npm is not installed. It should come with Node.js."
fi
info "Node.js $(node -v) | npm $(npm -v)"
}
ensure_dependencies() {
ensure_runtime_tools
if dependencies_ready; then
info "Web dependencies are ready."
return 0
fi
warn "Web dependencies are missing or incomplete"
info "Installing web dependencies..."
if use_windows_npm; then
run_windows_npm_install
else
npm install
fi
if ! dependencies_ready; then
error "Web dependencies are still incomplete after installation. Try deleting web/node_modules and running npm install manually."
fi
info "Web dependencies are ready."
}
start_dev_server() {
info "Starting X-Financial Reimbursement Admin..."
info "Access: http://$WEB_HOST:$WEB_PORT"
echo ""
if use_windows_npm; then
run_windows_npm_start
fi
exec npm start -- --host "$WEB_HOST" --port "$WEB_PORT"
}
case "$MODE" in
deps)
ensure_dependencies
;;
start)
ensure_dependencies
start_dev_server
;;
*)
error "Unknown mode: $MODE. Use one of: deps, start"
;;
esac