Files
YG_FT_Platform/request/start.sh
DESKTOP-72TV0V4\caoxiaozhu bda8f13446 1. 增加了请求框架
2. 增加了删除虚拟环境的脚本
2026-01-12 14:20:44 +08:00

100 lines
3.0 KiB
Bash
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 -euo pipefail
echo "🚀 启动 X-Request 高性能 FastAPI 框架"
# 永远从脚本所在目录运行(避免在别的目录执行导致找不到 venv / requirements / .env
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
mkdir -p logs
PID_FILE="logs/xrequest.pid"
LOG_FILE="logs/xrequest.server.log"
# 检查虚拟环境是否存在
if [ ! -d "xrequest" ]; then
echo "❌ 虚拟环境不存在,请先运行:"
echo " ./setup.sh"
exit 1
fi
# 选择虚拟环境里的 Python不要依赖 source activate也不要回退系统 Python
VENV_PY=""
if [ -f "xrequest/Scripts/python.exe" ]; then
VENV_PY="xrequest/Scripts/python.exe"
elif [ -f "xrequest/bin/python" ]; then
VENV_PY="xrequest/bin/python"
fi
if [ -z "$VENV_PY" ]; then
echo "❌ 未找到虚拟环境 Python可尝试重新创建虚拟环境"
echo " rm -rf xrequest && ./setup.sh"
exit 1
fi
echo "✅ 使用虚拟环境 Python: $VENV_PY"
# 检查 .env 文件是否存在
if [ ! -f ".env" ]; then
echo "❌ 环境配置文件 .env 不存在,请先运行:"
echo " ./setup.sh"
exit 1
fi
# 读取 .env 文件中的配置
PORT="8000" # 默认端口
HOST="0.0.0.0" # 默认主机
# 解析 .env 文件(简单解析 PORT 和 HOST
if [ -f ".env" ]; then
# 使用 grep 和 sed 来提取 PORT 和 HOST忽略注释
PORT=$(grep -v '^#' .env | grep '^PORT=' | head -1 | cut -d= -f2 | tr -d '"' || echo "8000")
HOST=$(grep -v '^#' .env | grep '^HOST=' | head -1 | cut -d= -f2 | tr -d '"' || echo "0.0.0.0")
fi
echo "📋 使用配置: HOST=$HOST, PORT=$PORT"
# 快速校验关键依赖是否可导入(避免跑到一半才 ModuleNotFoundError
if ! "$VENV_PY" -c "import uvicorn" >/dev/null 2>&1; then
echo "❌ 虚拟环境缺少 uvicorn或未正确安装依赖。请运行"
echo " ./setup.sh"
echo " 或手动安装:$VENV_PY -m pip install -r requirements.txt"
exit 1
fi
if [ -f "$PID_FILE" ]; then
OLD_PID="$(cat "$PID_FILE" 2>/dev/null || true)"
if [ -n "$OLD_PID" ] && kill -0 "$OLD_PID" >/dev/null 2>&1; then
echo "✅ X-Request 已在后台运行 (pid=$OLD_PID)"
echo "📄 日志: $LOG_FILE"
exit 0
fi
rm -f "$PID_FILE" || true
fi
echo "🎯 启动应用服务..."
echo "📚 API文档地址: http://localhost:$PORT/docs"
echo "🏥 健康检查: http://localhost:$PORT/health"
echo "📊 应用信息: http://localhost:$PORT/info"
echo "⏹️ 停止服务: ./stop.sh"
echo "📄 日志: $LOG_FILE"
echo ""
nohup "$VENV_PY" "main.py" >>"$LOG_FILE" 2>&1 &
PID_NUM=$!
echo $PID_NUM > "$PID_FILE"
sleep 0.5 # 等待PID文件写入
if [ -f "$PID_FILE" ]; then
SAVED_PID=$(cat "$PID_FILE")
echo "✅ 已在后台启动 (pid=$SAVED_PID)"
else
echo "⚠️ 启动成功但PID文件创建失败使用进程号: $PID_NUM"
echo $PID_NUM > "$PID_FILE"
echo "✅ 已在后台启动 (pid=$PID_NUM)"
fi
echo " 查看日志: tail -f $LOG_FILE"
echo " 停止服务: ./stop.sh"