Files
X-Financial/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

237 lines
6.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
export MSYS_NO_PATHCONV=1
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="$SCRIPT_DIR/.env"
ENV_EXAMPLE_FILE="$SCRIPT_DIR/.env.example"
MODE="${1:-all}"
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 "$ENV_FILE" ]; then
if [ -f "$ENV_EXAMPLE_FILE" ]; then
warn ".env not found. Creating it from .env.example"
cp "$ENV_EXAMPLE_FILE" "$ENV_FILE"
else
error ".env and .env.example are both missing."
fi
fi
set -a
. "$ENV_FILE"
set +a
SERVER_STARTUP_TIMEOUT="${SERVER_STARTUP_TIMEOUT:-300}"
SETUP_COMPLETED="${SETUP_COMPLETED:-false}"
APP_DEBUG="${APP_DEBUG:-true}"
APP_ENV="${APP_ENV:-local}"
SERVER_RELOAD="${SERVER_RELOAD:-}"
server_probe_url() {
echo "http://${SERVER_HOST:-127.0.0.1}:${SERVER_PORT:-8000}${API_V1_PREFIX:-/api/v1}/health"
}
server_smoke_url() {
echo "http://${SERVER_HOST:-127.0.0.1}:${SERVER_PORT:-8000}${API_V1_PREFIX:-/api/v1}/employees/meta"
}
server_probe_python() {
if [ -x "$SCRIPT_DIR/server/.venv/Scripts/python.exe" ]; then
echo "$SCRIPT_DIR/server/.venv/Scripts/python.exe"
return 0
fi
if [ -x "$SCRIPT_DIR/server/.venv/bin/python" ]; then
echo "$SCRIPT_DIR/server/.venv/bin/python"
return 0
fi
return 1
}
probe_server_health() {
local probe_url="${1:-$(server_probe_url)}"
local probe_python=""
if probe_python="$(server_probe_python)"; then
"$probe_python" -c "import json, sys, urllib.request; data = json.load(urllib.request.urlopen(sys.argv[1], timeout=2)); raise SystemExit(0 if data.get('status') == 'ok' else 1)" "$probe_url" >/dev/null 2>&1
return $?
fi
if command -v curl >/dev/null 2>&1; then
curl --silent --fail --max-time 2 "$probe_url" | grep -q '"status"[[:space:]]*:[[:space:]]*"ok"'
return $?
fi
return 1
}
probe_server_smoke() {
local probe_url="${1:-$(server_smoke_url)}"
local probe_python=""
if probe_python="$(server_probe_python)"; then
"$probe_python" -c "import sys, urllib.request; response = urllib.request.urlopen(sys.argv[1], timeout=3); raise SystemExit(0 if response.status == 200 else 1)" "$probe_url" >/dev/null 2>&1
return $?
fi
if command -v curl >/dev/null 2>&1; then
curl --silent --fail --max-time 3 "$probe_url" >/dev/null 2>&1
return $?
fi
return 1
}
probe_server_ready() {
local health_url="${1:-$(server_probe_url)}"
local smoke_url="${2:-$(server_smoke_url)}"
probe_server_health "$health_url" && probe_server_smoke "$smoke_url"
}
prepare_web() {
info "Preparing web dependencies..."
(
cd "$SCRIPT_DIR/web"
./start.sh deps
)
}
prepare_server() {
info "Preparing server dependencies..."
(
cd "$SCRIPT_DIR/server"
./start.sh deps
)
}
start_web() {
prepare_web
cd "$SCRIPT_DIR/web"
exec ./start.sh start
}
start_server() {
prepare_server
cd "$SCRIPT_DIR/server"
exec ./start.sh start
}
start_setup_web() {
warn "Initial setup is not completed. Starting web only."
warn "Finish the setup form first. After setup is saved, run ./start.sh again to launch FastAPI as well."
prepare_web
cd "$SCRIPT_DIR/web"
exec ./start.sh start
}
start_all() {
local server_pid=""
local started_server=false
local probe_url=""
local smoke_url=""
prepare_server
cleanup() {
if [ "$started_server" = true ] && [ -n "$server_pid" ] && kill -0 "$server_pid" 2>/dev/null; then
warn "Stopping FastAPI server..."
kill "$server_pid" 2>/dev/null || true
wait "$server_pid" 2>/dev/null || true
fi
}
trap cleanup EXIT INT TERM
probe_url="$(server_probe_url)"
smoke_url="$(server_smoke_url)"
if probe_server_ready "$probe_url" "$smoke_url"; then
warn "FastAPI is already ready at $probe_url. Reusing the existing backend process."
if [ "$APP_DEBUG" = "true" ] && [ "$SERVER_RELOAD" != "true" ]; then
warn "This backend may be stale because SERVER_RELOAD is disabled. If new API routes are missing, stop the old backend process and rerun ./start.sh."
fi
elif probe_server_health "$probe_url"; then
error "An existing backend process is responding at $probe_url, but the smoke check failed at $smoke_url. Stop the old FastAPI process and rerun ./start.sh."
else
info "Starting FastAPI server..."
(
cd "$SCRIPT_DIR/server"
./start.sh start
) &
server_pid=$!
started_server=true
fi
wait_for_server_ready() {
local attempt=1
local max_attempts="$SERVER_STARTUP_TIMEOUT"
info "Waiting for FastAPI readiness before starting the web frontend..."
while [ "$attempt" -le "$max_attempts" ]; do
if probe_server_ready "$probe_url" "$smoke_url"; then
info "FastAPI is ready. Starting web frontend next."
return 0
fi
if [ "$started_server" = true ] && ! kill -0 "$server_pid" 2>/dev/null; then
if probe_server_ready "$probe_url" "$smoke_url"; then
warn "FastAPI is already available at $probe_url. Continuing with the existing process."
started_server=false
server_pid=""
return 0
fi
wait "$server_pid" 2>/dev/null || true
error "FastAPI process exited before becoming ready. Run ./server/start.sh start directly to inspect the backend error."
fi
sleep 1
attempt=$((attempt + 1))
done
if probe_server_health "$probe_url"; then
error "FastAPI answered health checks at $probe_url, but the smoke check failed at $smoke_url. The running backend is stale or incompatible."
fi
error "FastAPI did not become ready within ${SERVER_STARTUP_TIMEOUT}s. Inspect server/logs/app.log."
}
wait_for_server_ready
prepare_web
info "Starting web frontend..."
cd "$SCRIPT_DIR/web"
./start.sh start
}
case "$MODE" in
web)
start_web
;;
server)
start_server
;;
all)
if [ "$SETUP_COMPLETED" = "true" ]; then
start_all
else
start_setup_web
fi
;;
*)
error "Unknown mode: $MODE. Use one of: web, server, all"
;;
esac