You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
5.3 KiB

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
酒店管理系统 - 系统启动和检查脚本
自动启动后端服务并在浏览器中打开前端
"""
import subprocess
import time
import os
import webbrowser
import sys
from pathlib import Path
def print_header(text):
"""打印标题"""
print("\n" + "=" * 60)
print(f" {text}")
print("=" * 60)
def print_success(text):
"""打印成功信息"""
print(f"[OK] {text}")
def print_error(text):
"""打印错误信息"""
print(f"[ERROR] {text}")
def print_info(text):
"""打印信息"""
print(f"[INFO] {text}")
def check_python():
"""检查 Python 版本"""
print_info(f"Python 版本: {sys.version.split()[0]}")
if sys.version_info < (3, 9):
print_error("需要 Python 3.9 或更高版本")
return False
print_success("Python 版本检查通过")
return True
def check_database():
"""检查数据库连接"""
try:
import psycopg2
conn = psycopg2.connect(
dbname="hotel_management_system",
user="postgres",
password="123456",
host="localhost",
port="5433"
)
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM customer")
count = cursor.fetchone()[0]
conn.close()
print_success(f"数据库连接成功 (客户数: {count})")
return True
except Exception as e:
print_error(f"数据库连接失败: {str(e)[:100]}")
return False
def check_dependencies():
"""检查 Python 依赖"""
required = ["fastapi", "uvicorn", "sqlalchemy", "psycopg2"]
missing = []
for package in required:
try:
__import__(package)
print_success(f"{package}")
except ImportError:
missing.append(package)
print_error(f"{package} 未安装")
if missing:
print_error(f"缺失依赖: {', '.join(missing)}")
print_info("请运行: pip install -r backend/requirements.txt")
return False
print_success("所有依赖检查通过")
return True
def start_backend():
"""启动后端服务"""
print_info("正在启动后端服务...")
backend_dir = Path(__file__).parent / "backend"
try:
proc = subprocess.Popen(
[sys.executable, "main.py"],
cwd=str(backend_dir),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
print_success(f"后端服务已启动 (PID: {proc.pid})")
# 等待服务启动
time.sleep(3)
# 检查服务是否正常运行
try:
import requests
resp = requests.get("http://127.0.0.1:8000/health", timeout=2)
if resp.status_code == 200:
print_success("后端服务健康检查通过")
return proc
else:
print_error(f"后端服务健康检查失败 (状态码: {resp.status_code})")
return proc
except Exception as e:
print_error(f"无法连接到后端服务: {str(e)[:100]}")
return proc
except Exception as e:
print_error(f"启动后端服务失败: {str(e)}")
return None
def main():
"""主函数"""
print_header("酒店管理系统启动脚本")
# 检查 Python 版本
if not check_python():
return False
print_header("依赖检查")
# 检查依赖
if not check_dependencies():
print_info("请先安装依赖: pip install -r backend/requirements.txt")
return False
# 检查数据库
print_header("数据库检查")
if not check_database():
print_error("无法连接到数据库,请确保 PostgreSQL 正在运行")
return False
# 启动后端
print_header("启动后端服务")
backend_proc = start_backend()
if backend_proc is None:
return False
# 打开前端
print_header("打开前端界面")
frontend_path = Path(__file__).parent / "frontend" / "index.html"
frontend_url = frontend_path.as_uri()
print_info(f"前端地址: {frontend_url}")
try:
webbrowser.open(frontend_url)
print_success("前端已在浏览器中打开")
except Exception as e:
print_error(f"无法自动打开浏览器: {str(e)}")
print_info(f"请手动打开: {frontend_url}")
# 显示API文档地址
print_header("系统已启动")
print_success("后端运行地址: http://127.0.0.1:8000")
print_info("API 文档 (Swagger): http://127.0.0.1:8000/docs")
print_info("API 文档 (ReDoc): http://127.0.0.1:8000/redoc")
print_info("健康检查: http://127.0.0.1:8000/health")
print_header("提示信息")
print_info("按 Ctrl+C 停止后端服务")
print_info("不要关闭此窗口,否则后端服务将停止")
# 保持进程运行
try:
if backend_proc:
backend_proc.wait()
except KeyboardInterrupt:
print_info("正在关闭后端服务...")
if backend_proc:
backend_proc.terminate()
backend_proc.wait()
print_success("后端服务已关闭")
return True
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)