154 lines
4.4 KiB
Python
154 lines
4.4 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
测试后端启动的脚本
|
||
"""
|
||
|
||
import os
|
||
import sys
|
||
import subprocess
|
||
import time
|
||
import requests
|
||
|
||
|
||
def test_backend_startup():
|
||
"""测试后端启动"""
|
||
print("🧪 测试后端启动...")
|
||
|
||
# 切换到backend目录
|
||
backend_dir = "backend"
|
||
if not os.path.exists(backend_dir):
|
||
print("❌ 找不到backend目录")
|
||
return False
|
||
|
||
os.chdir(backend_dir)
|
||
|
||
try:
|
||
# 启动后端服务
|
||
print("📝 启动命令: uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload")
|
||
process = subprocess.Popen([
|
||
sys.executable, "-m", "uvicorn", "app.main:app",
|
||
"--host", "0.0.0.0", "--port", "8000", "--reload"
|
||
], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
|
||
|
||
# 等待服务启动
|
||
print("⏳ 等待服务启动...")
|
||
time.sleep(10)
|
||
|
||
# 检查进程状态
|
||
if process.poll() is not None:
|
||
# 进程已退出
|
||
stdout, stderr = process.communicate()
|
||
print(f"❌ 进程已退出,退出码: {process.returncode}")
|
||
if stderr:
|
||
print(f"错误输出:\n{stderr}")
|
||
if stdout:
|
||
print(f"标准输出:\n{stdout}")
|
||
return False
|
||
|
||
# 尝试连接服务
|
||
try:
|
||
response = requests.get("http://localhost:8000/", timeout=5)
|
||
print(f"✅ 服务响应成功,状态码: {response.status_code}")
|
||
print(f"响应内容: {response.text}")
|
||
|
||
# 测试API文档
|
||
try:
|
||
docs_response = requests.get(
|
||
"http://localhost:8000/docs", timeout=5)
|
||
print(f"✅ API文档可访问,状态码: {docs_response.status_code}")
|
||
except Exception as e:
|
||
print(f"⚠️ API文档访问失败: {e}")
|
||
|
||
return True
|
||
|
||
except requests.exceptions.RequestException as e:
|
||
print(f"❌ 无法连接到服务: {e}")
|
||
|
||
# 检查进程输出
|
||
try:
|
||
stdout, stderr = process.communicate(timeout=2)
|
||
if stderr:
|
||
print(f"错误输出:\n{stderr}")
|
||
if stdout:
|
||
print(f"标准输出:\n{stdout}")
|
||
except subprocess.TimeoutExpired:
|
||
process.kill()
|
||
print("⚠️ 进程被强制终止")
|
||
|
||
return False
|
||
|
||
except Exception as e:
|
||
print(f"❌ 启动过程中出错: {e}")
|
||
return False
|
||
finally:
|
||
# 清理进程
|
||
if 'process' in locals() and process.poll() is None:
|
||
process.terminate()
|
||
try:
|
||
process.wait(timeout=5)
|
||
except subprocess.TimeoutExpired:
|
||
process.kill()
|
||
|
||
|
||
def test_database_connection():
|
||
"""测试数据库连接"""
|
||
print("\n🔍 测试数据库连接...")
|
||
|
||
try:
|
||
# 导入数据库配置
|
||
sys.path.append('backend')
|
||
from app.core.database import SQLALCHEMY_DATABASE_URL
|
||
|
||
print(f"数据库URL: {SQLALCHEMY_DATABASE_URL}")
|
||
|
||
# 尝试创建引擎
|
||
from sqlalchemy import create_engine, text
|
||
engine = create_engine(SQLALCHEMY_DATABASE_URL)
|
||
|
||
# 测试连接
|
||
with engine.connect() as connection:
|
||
result = connection.execute(text("SELECT 1"))
|
||
print("✅ 数据库连接成功")
|
||
return True
|
||
|
||
except Exception as e:
|
||
print(f"❌ 数据库连接失败: {e}")
|
||
return False
|
||
|
||
|
||
def main():
|
||
"""主函数"""
|
||
print("🧪 后端启动测试")
|
||
print("=" * 50)
|
||
|
||
# 测试数据库连接
|
||
db_ok = test_database_connection()
|
||
|
||
if not db_ok:
|
||
print("\n💡 数据库连接失败,可能的原因:")
|
||
print("1. 数据库服务器未运行")
|
||
print("2. 网络连接问题")
|
||
print("3. 数据库凭据错误")
|
||
print("4. 防火墙阻止连接")
|
||
|
||
response = input("\n是否继续测试后端启动?(y/N): ")
|
||
if response.lower() != 'y':
|
||
return
|
||
|
||
# 测试后端启动
|
||
backend_ok = test_backend_startup()
|
||
|
||
if backend_ok:
|
||
print("\n🎉 后端启动测试成功!")
|
||
else:
|
||
print("\n❌ 后端启动测试失败!")
|
||
print("\n💡 建议:")
|
||
print("1. 检查数据库连接")
|
||
print("2. 检查依赖包是否安装完整")
|
||
print("3. 检查端口8000是否被占用")
|
||
print("4. 查看详细的错误输出")
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|