Files
platforms/scripts/run_platform.sh
2026-07-28 14:43:56 +08:00

97 lines
2.5 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
set -Eeuo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
BACKEND_DIR="${PROJECT_ROOT}/backend/api"
FRONTEND_DIR="${PROJECT_ROOT}/frontend/platform_admin"
BACKEND_PID=""
FRONTEND_PID=""
CLEANED_UP=0
RUNTIME_CONFIG_ROOT=""
cleanup() {
if [[ "${CLEANED_UP}" -eq 1 ]]; then
return
fi
CLEANED_UP=1
trap - EXIT INT TERM
for pid in "${BACKEND_PID}" "${FRONTEND_PID}"; do
if [[ -n "${pid}" ]] && kill -0 "${pid}" 2>/dev/null; then
kill "${pid}" 2>/dev/null || true
fi
done
for pid in "${BACKEND_PID}" "${FRONTEND_PID}"; do
if [[ -n "${pid}" ]]; then
wait "${pid}" 2>/dev/null || true
fi
done
if [[ -n "${RUNTIME_CONFIG_ROOT}" ]]; then
rm -f "${RUNTIME_CONFIG_ROOT}/etc/heqi_dev.yaml"
rmdir "${RUNTIME_CONFIG_ROOT}/etc" "${RUNTIME_CONFIG_ROOT}" 2>/dev/null || true
fi
}
require_command() {
if ! command -v "$1" >/dev/null 2>&1; then
echo "缺少命令:$1" >&2
exit 1
fi
}
require_command go
require_command pnpm
if [[ ! -d "${FRONTEND_DIR}/node_modules" ]]; then
echo "前端依赖尚未安装请先运行cd ${FRONTEND_DIR} && pnpm install" >&2
exit 1
fi
export BSM_RuntimeMode="${BSM_RuntimeMode:-dev}"
export BSM_JwtSecretKey="${BSM_JwtSecretKey:-local-dev-key-16}"
export VITE_API_BASE_URL="${VITE_API_BASE_URL:-http://localhost:12426/heqi/platform/v1}"
if [[ -z "${BSM_Prefix:-}" ]]; then
RUNTIME_CONFIG_ROOT="$(mktemp -d "${TMPDIR:-/tmp}/heqi-platform.XXXXXX")"
mkdir -p "${RUNTIME_CONFIG_ROOT}/etc"
cp "${BACKEND_DIR}/etc/platform_dev.yaml" "${RUNTIME_CONFIG_ROOT}/etc/heqi_dev.yaml"
export BSM_Prefix="${RUNTIME_CONFIG_ROOT}"
fi
trap cleanup EXIT INT TERM
echo "启动平台后端http://localhost:12426/heqi/platform/v1"
(
cd "${BACKEND_DIR}"
exec go run ./cmd/main/main.go
) &
BACKEND_PID=$!
echo "启动平台前端http://localhost:5173"
(
cd "${FRONTEND_DIR}"
exec pnpm exec vite --config ./config/vite.config.ts --host 0.0.0.0
) &
FRONTEND_PID=$!
echo "前后端调试服务已启动,按 Ctrl+C 同时停止。"
while true; do
if ! kill -0 "${BACKEND_PID}" 2>/dev/null; then
status=0
wait "${BACKEND_PID}" || status=$?
echo "平台后端已退出(状态码:${status}),正在停止前端。" >&2
exit "${status}"
fi
if ! kill -0 "${FRONTEND_PID}" 2>/dev/null; then
status=0
wait "${FRONTEND_PID}" || status=$?
echo "平台前端已退出(状态码:${status}),正在停止后端。" >&2
exit "${status}"
fi
sleep 1
done