fix: 修复 Docker 部署 API 地址与数据库连接问题
This commit is contained in:
95
start.sh
95
start.sh
@@ -1,9 +1,18 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
#!/usr/bin/env sh
|
||||
set -eu
|
||||
|
||||
if (set -o pipefail) >/dev/null 2>&1; then
|
||||
set -o pipefail
|
||||
fi
|
||||
|
||||
export MSYS_NO_PATHCONV=1
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
SCRIPT_PATH="$0"
|
||||
case "$SCRIPT_PATH" in
|
||||
/*) ;;
|
||||
*) SCRIPT_PATH="$(pwd)/$SCRIPT_PATH" ;;
|
||||
esac
|
||||
SCRIPT_DIR="$(CDPATH= cd -- "$(dirname -- "$SCRIPT_PATH")" && pwd)"
|
||||
ENV_FILE="$SCRIPT_DIR/.env"
|
||||
ENV_EXAMPLE_FILE="$SCRIPT_DIR/.env.example"
|
||||
ADMIN_SECRET_FILE="$SCRIPT_DIR/server/.secrets/admin.json"
|
||||
@@ -14,9 +23,9 @@ 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; }
|
||||
info() { printf '%b\n' "${GREEN}[INFO]${NC} $*"; }
|
||||
warn() { printf '%b\n' "${YELLOW}[WARN]${NC} $*"; }
|
||||
error() { printf '%b\n' "${RED}[ERROR]${NC} $*"; exit 1; }
|
||||
|
||||
if [ ! -f "$ENV_FILE" ]; then
|
||||
if [ -f "$ENV_EXAMPLE_FILE" ]; then
|
||||
@@ -27,15 +36,49 @@ if [ ! -f "$ENV_FILE" ]; then
|
||||
fi
|
||||
fi
|
||||
|
||||
ENV_OVERRIDE_WEB_HOST_SET=false
|
||||
ENV_OVERRIDE_SERVER_HOST_SET=false
|
||||
|
||||
if [ "${WEB_HOST+x}" = x ]; then
|
||||
ENV_OVERRIDE_WEB_HOST_SET=true
|
||||
ENV_OVERRIDE_WEB_HOST="$WEB_HOST"
|
||||
fi
|
||||
|
||||
if [ "${SERVER_HOST+x}" = x ]; then
|
||||
ENV_OVERRIDE_SERVER_HOST_SET=true
|
||||
ENV_OVERRIDE_SERVER_HOST="$SERVER_HOST"
|
||||
fi
|
||||
|
||||
set -a
|
||||
. "$ENV_FILE"
|
||||
set +a
|
||||
|
||||
if [ "$ENV_OVERRIDE_WEB_HOST_SET" = true ]; then
|
||||
WEB_HOST="$ENV_OVERRIDE_WEB_HOST"
|
||||
fi
|
||||
|
||||
if [ "$ENV_OVERRIDE_SERVER_HOST_SET" = true ]; then
|
||||
SERVER_HOST="$ENV_OVERRIDE_SERVER_HOST"
|
||||
fi
|
||||
|
||||
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:-}"
|
||||
DEFAULT_SERVER_RELOAD="false"
|
||||
|
||||
case "$APP_ENV" in
|
||||
local|dev|development)
|
||||
DEFAULT_SERVER_RELOAD="true"
|
||||
;;
|
||||
esac
|
||||
|
||||
if [ "$APP_DEBUG" = "true" ]; then
|
||||
DEFAULT_SERVER_RELOAD="true"
|
||||
fi
|
||||
|
||||
EFFECTIVE_SERVER_RELOAD="${SERVER_RELOAD:-$DEFAULT_SERVER_RELOAD}"
|
||||
|
||||
setup_ready() {
|
||||
[ "$SETUP_COMPLETED" = "true" ] && [ -f "$ADMIN_SECRET_FILE" ]
|
||||
@@ -75,16 +118,16 @@ server_probe_python() {
|
||||
}
|
||||
|
||||
probe_server_health() {
|
||||
local probe_url="${1:-$(server_probe_url)}"
|
||||
local probe_python=""
|
||||
_probe_url="${1:-$(server_probe_url)}"
|
||||
_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
|
||||
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"'
|
||||
curl --silent --fail --max-time 2 "$_probe_url" | grep -q '"status"[[:space:]]*:[[:space:]]*"ok"'
|
||||
return $?
|
||||
fi
|
||||
|
||||
@@ -92,16 +135,16 @@ probe_server_health() {
|
||||
}
|
||||
|
||||
probe_server_smoke() {
|
||||
local probe_url="${1:-$(server_smoke_url)}"
|
||||
local probe_python=""
|
||||
_probe_url="${1:-$(server_smoke_url)}"
|
||||
_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
|
||||
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
|
||||
curl --silent --fail --max-time 3 "$_probe_url" >/dev/null 2>&1
|
||||
return $?
|
||||
fi
|
||||
|
||||
@@ -109,10 +152,10 @@ probe_server_smoke() {
|
||||
}
|
||||
|
||||
probe_server_ready() {
|
||||
local health_url="${1:-$(server_probe_url)}"
|
||||
local smoke_url="${2:-$(server_smoke_url)}"
|
||||
_health_url="${1:-$(server_probe_url)}"
|
||||
_smoke_url="${2:-$(server_smoke_url)}"
|
||||
|
||||
probe_server_health "$health_url" && probe_server_smoke "$smoke_url"
|
||||
probe_server_health "$_health_url" && probe_server_smoke "$_smoke_url"
|
||||
}
|
||||
|
||||
prepare_web() {
|
||||
@@ -154,10 +197,10 @@ start_setup_web() {
|
||||
}
|
||||
|
||||
start_all() {
|
||||
local server_pid=""
|
||||
local started_server=false
|
||||
local probe_url=""
|
||||
local smoke_url=""
|
||||
server_pid=""
|
||||
started_server=false
|
||||
probe_url=""
|
||||
smoke_url=""
|
||||
|
||||
prepare_server
|
||||
|
||||
@@ -176,7 +219,7 @@ start_all() {
|
||||
|
||||
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
|
||||
if [ "$APP_DEBUG" = "true" ] && [ "$EFFECTIVE_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
|
||||
@@ -192,8 +235,8 @@ start_all() {
|
||||
fi
|
||||
|
||||
wait_for_server_ready() {
|
||||
local attempt=1
|
||||
local max_attempts="$SERVER_STARTUP_TIMEOUT"
|
||||
attempt=1
|
||||
max_attempts="$SERVER_STARTUP_TIMEOUT"
|
||||
|
||||
info "Waiting for FastAPI readiness before starting the web frontend..."
|
||||
|
||||
|
||||
Reference in New Issue
Block a user