chore: 统一环境配置,支持 .env 快速切换本地/远程基础设施

This commit is contained in:
2026-07-13 14:17:49 +08:00
parent 8c4deb5be4
commit 0bf48fd449
8 changed files with 273 additions and 57 deletions

View File

@@ -17,6 +17,17 @@ MCI_SERVER_PORT=${MCI_SERVER_PORT:-8080}
ADMIN_WEB_PORT=${ADMIN_WEB_PORT:-80}
CHANNEL_SERVICE_PORT=${CHANNEL_SERVICE_PORT:-8000}
# Docker Compose 内部连接必须使用容器名,覆盖 .env 中的 host 配置。
# 当用本脚本启动应用服务时,应用与基础设施在同一 Docker 网络。
dc() {
MYSQL_HOST=mci-mysql \
REDIS_HOST=mci-redis \
RABBITMQ_HOST=mci-rabbitmq \
SEAWEEDFS_HOST=mci-seaweedfs \
MCI_SERVER_URL=http://mci-server:8080/msg-platform \
docker compose "$@"
}
WITH_APPS=""
BUILD_FLAG=""
INIT_DATA=""
@@ -60,7 +71,7 @@ wait_for_healthy() {
echo "Waiting for $service to become healthy..."
while [ "$retry_count" -lt "$max_retries" ]; do
container_name=$(docker compose -p sino-mci --profile infra --profile app ps -q "$service" 2>/dev/null | head -n1 || true)
container_name=$(dc -p sino-mci --profile infra --profile app ps -q "$service" 2>/dev/null | head -n1 || true)
if [ -n "$container_name" ]; then
local status
@@ -76,7 +87,7 @@ wait_for_healthy() {
done
echo "$service did not become healthy within $((max_retries * 2)) seconds."
docker compose -p sino-mci --profile infra --profile app logs --tail=50 "$service"
dc -p sino-mci --profile infra --profile app logs --tail=50 "$service"
return 1
}
@@ -102,7 +113,7 @@ init_database() {
}
echo "Starting infrastructure services..."
docker compose -p sino-mci --profile infra up -d $BUILD_FLAG
dc -p sino-mci --profile infra up -d $BUILD_FLAG
wait_for_healthy mysql 60
wait_for_healthy redis 30
@@ -123,7 +134,7 @@ echo "SeaweedFS: http://localhost:${SEAWEEDFS_MASTER_PORT}"
if [ -n "$WITH_APPS" ]; then
echo "Starting application services..."
# Include infra profile so that depends_on conditions can resolve.
docker compose -p sino-mci --profile infra --profile app up -d $BUILD_FLAG
dc -p sino-mci --profile infra --profile app up -d $BUILD_FLAG
wait_for_healthy mci-server 90
wait_for_healthy channel-service 60

71
scripts/run-local.sh Executable file
View File

@@ -0,0 +1,71 @@
#!/bin/bash
set -euo pipefail
# Sino-MCI 本地开发启动脚本
# 自动加载项目根目录 .env 文件中的环境变量,然后启动后端或前端。
#
# 用法:
# ./scripts/run-local.sh backend
# ./scripts/run-local.sh frontend
# ./scripts/run-local.sh channel
#
# 切换环境:
# cp .env.local .env # 本地基础设施
# cp .env.remote .env # 远程基础设施(如 192.168.1.173
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$SCRIPT_DIR/.."
cd "$PROJECT_ROOT"
if [ ! -f .env ]; then
echo ".env file not found. Creating from .env.local ..."
cp .env.local .env
fi
# 加载 .env忽略注释与空行
set -o allexport
# shellcheck source=/dev/null
source .env
set +o allexport
echo "Loaded environment from: $PROJECT_ROOT/.env"
echo " MYSQL_HOST=${MYSQL_HOST:-localhost}"
echo " REDIS_HOST=${REDIS_HOST:-localhost}"
echo " RABBITMQ_HOST=${RABBITMQ_HOST:-localhost}"
echo " MCI_SERVER_URL=${MCI_SERVER_URL:-http://localhost:8080/msg-platform}"
echo " VITE_API_BASE_URL=${VITE_API_BASE_URL:-http://localhost:8080}"
echo ""
SERVICE=${1:-}
if [ -z "$SERVICE" ]; then
echo "Usage: $0 <backend|frontend|channel>"
exit 1
fi
case "$SERVICE" in
backend|server)
cd backend/mci-server
echo "Starting mci-server with Maven..."
./mvnw spring-boot:run -Dspring-boot.run.profiles=dev
;;
frontend|web)
cd admin-web
echo "Starting admin-web with Vite..."
npm run dev
;;
channel|channel-service)
cd channel-service
echo "Starting channel-service..."
if [ ! -d .venv ]; then
python3 -m venv .venv
fi
source .venv/bin/activate
pip install -r requirements.txt -q
python -m app.main
;;
*)
echo "Unknown service: $SERVICE"
echo "Usage: $0 <backend|frontend|channel>"
exit 1
;;
esac