nex_math/scripts/start.sh

37 lines
1.1 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env bash
# 同时启动后端(FastAPI :8000)与前端(Vite :5173)。
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
if [[ ! -x "$ROOT_DIR/backend/.venv/bin/uvicorn" ]]; then
echo "尚未初始化,请先运行 ./scripts/init.sh" >&2
exit 1
fi
if [[ ! -f "$ROOT_DIR/data/math.db" ]]; then
echo "数据库不存在,请先运行 ./scripts/init.sh" >&2
exit 1
fi
if [[ ! -d "$ROOT_DIR/frontend/node_modules" ]]; then
echo "[start] 安装前端依赖…"
(cd "$ROOT_DIR/frontend" && npm install)
fi
BACKEND_PID=""
cleanup() {
if [[ -n "$BACKEND_PID" ]]; then
kill "$BACKEND_PID" 2>/dev/null || true
fi
}
trap cleanup EXIT INT TERM
echo "[start] 后端:http://localhost:${BACKEND_PORT:-8000}/api/health"
(cd "$ROOT_DIR/backend" && exec .venv/bin/uvicorn main:app \
--host 0.0.0.0 --port "${BACKEND_PORT:-8000}" --reload) &
BACKEND_PID=$!
echo "[start] 前端:http://localhost:${FRONTEND_PORT:-5173}"
(cd "$ROOT_DIR/frontend" && exec npm run dev -- \
--host 0.0.0.0 --port "${FRONTEND_PORT:-5173}")