fix(dev): 兼容 Compose 数据库容器启动
保留显式数据库连接配置,自动识别 Compose PostgreSQL 容器及宿主机端口,并在容器不存在时跳过不适用的 Docker 建库。\n\n验证:bash -n;ShellCheck;scripts/create-database.sh;pnpm dev 真实启动 API 与 Web
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
|
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
||||||
|
|
||||||
if [[ -n "${AI_GATEWAY_PG_CONTAINER:-}" ]]; then
|
if [[ -n "${AI_GATEWAY_PG_CONTAINER:-}" ]]; then
|
||||||
CONTAINER="$AI_GATEWAY_PG_CONTAINER"
|
CONTAINER="$AI_GATEWAY_PG_CONTAINER"
|
||||||
elif docker inspect easyai-pgvector >/dev/null 2>&1; then
|
elif docker inspect easyai-pgvector >/dev/null 2>&1; then
|
||||||
@@ -8,7 +10,15 @@ elif docker inspect easyai-pgvector >/dev/null 2>&1; then
|
|||||||
elif docker inspect postgres >/dev/null 2>&1; then
|
elif docker inspect postgres >/dev/null 2>&1; then
|
||||||
CONTAINER="postgres"
|
CONTAINER="postgres"
|
||||||
else
|
else
|
||||||
CONTAINER="easyai-pgvector"
|
CONTAINER="$(
|
||||||
|
docker compose -f "${PROJECT_ROOT}/docker-compose.yml" ps -q postgres 2>/dev/null \
|
||||||
|
| head -n 1
|
||||||
|
)"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [[ -z "$CONTAINER" ]] || ! docker inspect "$CONTAINER" >/dev/null 2>&1; then
|
||||||
|
echo "[ai-gateway] no PostgreSQL Docker container found; set AI_GATEWAY_PG_CONTAINER or create the database through the configured connection" >&2
|
||||||
|
exit 1
|
||||||
fi
|
fi
|
||||||
PGUSER="${AI_GATEWAY_PG_USER:-easyai}"
|
PGUSER="${AI_GATEWAY_PG_USER:-easyai}"
|
||||||
DB_NAME="${AI_GATEWAY_DATABASE_NAME:-easyai_ai_gateway}"
|
DB_NAME="${AI_GATEWAY_DATABASE_NAME:-easyai_ai_gateway}"
|
||||||
|
|||||||
+63
-15
@@ -24,6 +24,47 @@ load_local_env() {
|
|||||||
|
|
||||||
load_local_env
|
load_local_env
|
||||||
|
|
||||||
|
find_postgres_container() {
|
||||||
|
local candidate compose_container
|
||||||
|
|
||||||
|
if [[ -n "${AI_GATEWAY_PG_CONTAINER:-}" ]]; then
|
||||||
|
if docker inspect "$AI_GATEWAY_PG_CONTAINER" >/dev/null 2>&1; then
|
||||||
|
printf '%s\n' "$AI_GATEWAY_PG_CONTAINER"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
for candidate in easyai-pgvector postgres; do
|
||||||
|
if docker inspect "$candidate" >/dev/null 2>&1; then
|
||||||
|
printf '%s\n' "$candidate"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
compose_container="$(
|
||||||
|
docker compose -f "${PROJECT_ROOT}/docker-compose.yml" ps -q postgres 2>/dev/null \
|
||||||
|
| head -n 1
|
||||||
|
)"
|
||||||
|
if [[ -n "$compose_container" ]] && docker inspect "$compose_container" >/dev/null 2>&1; then
|
||||||
|
printf '%s\n' "$compose_container"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
postgres_host_port() {
|
||||||
|
local mapping port
|
||||||
|
mapping="$(docker port "$1" 5432/tcp 2>/dev/null | head -n 1)"
|
||||||
|
port="${mapping##*:}"
|
||||||
|
if [[ "$port" =~ ^[0-9]+$ ]]; then
|
||||||
|
printf '%s\n' "$port"
|
||||||
|
else
|
||||||
|
printf '5432\n'
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
stop_stale_api_processes() {
|
stop_stale_api_processes() {
|
||||||
local api_port="${HTTP_ADDR:-:8088}"
|
local api_port="${HTTP_ADDR:-:8088}"
|
||||||
api_port="${api_port##*:}"
|
api_port="${api_port##*:}"
|
||||||
@@ -66,37 +107,44 @@ stop_stale_api_processes() {
|
|||||||
sleep 0.5
|
sleep 0.5
|
||||||
}
|
}
|
||||||
|
|
||||||
if [[ -z "${AI_GATEWAY_PG_CONTAINER:-}" ]]; then
|
database_url_was_configured=0
|
||||||
if docker inspect easyai-pgvector >/dev/null 2>&1; then
|
pg_container_was_configured=0
|
||||||
export AI_GATEWAY_PG_CONTAINER="easyai-pgvector"
|
[[ -n "${AI_GATEWAY_DATABASE_URL:-}" ]] && database_url_was_configured=1
|
||||||
elif docker inspect postgres >/dev/null 2>&1; then
|
[[ -n "${AI_GATEWAY_PG_CONTAINER:-}" ]] && pg_container_was_configured=1
|
||||||
export AI_GATEWAY_PG_CONTAINER="postgres"
|
|
||||||
else
|
postgres_container="$(find_postgres_container || true)"
|
||||||
export AI_GATEWAY_PG_CONTAINER="easyai-pgvector"
|
if [[ -n "$postgres_container" ]]; then
|
||||||
fi
|
export AI_GATEWAY_PG_CONTAINER="$postgres_container"
|
||||||
|
elif [[ "$pg_container_was_configured" == "1" ]]; then
|
||||||
|
echo "[ai-gateway] configured PostgreSQL container is unavailable; using the database URL without Docker database creation" >&2
|
||||||
fi
|
fi
|
||||||
|
|
||||||
export AI_GATEWAY_PG_USER="${AI_GATEWAY_PG_USER:-easyai}"
|
export AI_GATEWAY_PG_USER="${AI_GATEWAY_PG_USER:-easyai}"
|
||||||
if [[ -z "${AI_GATEWAY_PG_PASSWORD:-}" ]] && docker inspect "$AI_GATEWAY_PG_CONTAINER" >/dev/null 2>&1; then
|
if [[ -z "${AI_GATEWAY_PG_PASSWORD:-}" && -n "$postgres_container" ]]; then
|
||||||
AI_GATEWAY_PG_PASSWORD="$(
|
AI_GATEWAY_PG_PASSWORD="$(
|
||||||
docker inspect "$AI_GATEWAY_PG_CONTAINER" --format '{{range .Config.Env}}{{println .}}{{end}}' \
|
docker inspect "$postgres_container" --format '{{range .Config.Env}}{{println .}}{{end}}' \
|
||||||
| awk -F= '$1 == "POSTGRES_PASSWORD" {print $2; exit}'
|
| awk -F= '$1 == "POSTGRES_PASSWORD" {print $2; exit}'
|
||||||
)"
|
)"
|
||||||
export AI_GATEWAY_PG_PASSWORD
|
export AI_GATEWAY_PG_PASSWORD
|
||||||
fi
|
fi
|
||||||
export AI_GATEWAY_PG_PASSWORD="${AI_GATEWAY_PG_PASSWORD:-easyai2025}"
|
export AI_GATEWAY_PG_PASSWORD="${AI_GATEWAY_PG_PASSWORD:-easyai2025}"
|
||||||
export AI_GATEWAY_DATABASE_NAME="${AI_GATEWAY_DATABASE_NAME:-easyai_ai_gateway}"
|
export AI_GATEWAY_DATABASE_NAME="${AI_GATEWAY_DATABASE_NAME:-easyai_ai_gateway}"
|
||||||
if docker inspect "$AI_GATEWAY_PG_CONTAINER" >/dev/null 2>&1; then
|
if [[ "$database_url_was_configured" == "0" ]]; then
|
||||||
export AI_GATEWAY_DATABASE_URL="postgresql://${AI_GATEWAY_PG_USER}:${AI_GATEWAY_PG_PASSWORD}@localhost:5432/${AI_GATEWAY_DATABASE_NAME}?sslmode=disable"
|
postgres_port=5432
|
||||||
else
|
if [[ -n "$postgres_container" ]]; then
|
||||||
export AI_GATEWAY_DATABASE_URL="${AI_GATEWAY_DATABASE_URL:-postgresql://${AI_GATEWAY_PG_USER}:${AI_GATEWAY_PG_PASSWORD}@localhost:5432/${AI_GATEWAY_DATABASE_NAME}?sslmode=disable}"
|
postgres_port="$(postgres_host_port "$postgres_container")"
|
||||||
|
fi
|
||||||
|
export AI_GATEWAY_DATABASE_URL="postgresql://${AI_GATEWAY_PG_USER}:${AI_GATEWAY_PG_PASSWORD}@localhost:${postgres_port}/${AI_GATEWAY_DATABASE_NAME}?sslmode=disable"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
echo "[ai-gateway] using configured local database connection (credentials redacted)"
|
echo "[ai-gateway] using configured local database connection (credentials redacted)"
|
||||||
|
|
||||||
if [[ "${AI_GATEWAY_SKIP_DB_CREATE:-}" == "1" ]]; then
|
if [[ "${AI_GATEWAY_SKIP_DB_CREATE:-}" == "1" ]]; then
|
||||||
echo "[ai-gateway] skipping Docker database creation"
|
echo "[ai-gateway] skipping Docker database creation"
|
||||||
else
|
elif [[ -n "$postgres_container" && ( "$database_url_was_configured" == "0" || "$pg_container_was_configured" == "1" ) ]]; then
|
||||||
scripts/create-database.sh
|
scripts/create-database.sh
|
||||||
|
else
|
||||||
|
echo "[ai-gateway] no matching Docker database container selected; skipping database creation"
|
||||||
fi
|
fi
|
||||||
pnpm nx run api:migrate
|
pnpm nx run api:migrate
|
||||||
stop_stale_api_processes
|
stop_stale_api_processes
|
||||||
|
|||||||
Reference in New Issue
Block a user