156 lines
4.7 KiB
Python
156 lines
4.7 KiB
Python
"""
|
||
API服务器启动脚本
|
||
自动从 db_config.json 和 api_config.json 读取配置并启动服务器
|
||
"""
|
||
import os
|
||
import sys
|
||
import json
|
||
|
||
def load_config():
|
||
"""加载配置文件"""
|
||
print("=" * 60)
|
||
print("加载配置文件...")
|
||
print("=" * 60)
|
||
|
||
# 读取数据库配置
|
||
if os.path.exists('db_config.json'):
|
||
with open('db_config.json', 'r', encoding='utf-8') as f:
|
||
db_config = json.load(f)
|
||
print("✓ 数据库配置:")
|
||
print(f" - 主机: {db_config.get('host', 'localhost')}")
|
||
print(f" - 数据库: {db_config.get('database', 'exeprotector')}")
|
||
print(f" - 用户: {db_config.get('user', 'root')}")
|
||
|
||
# 设置环境变量
|
||
os.environ['DB_HOST'] = db_config.get('host', 'localhost')
|
||
os.environ['DB_USER'] = db_config.get('user', 'root')
|
||
os.environ['DB_PASSWORD'] = db_config.get('password', 'taiyi1224')
|
||
os.environ['DB_NAME'] = db_config.get('database', 'exeprotector')
|
||
else:
|
||
print("⚠️ 未找到 db_config.json,使用默认配置")
|
||
|
||
# 读取API配置
|
||
if os.path.exists('api_config.json'):
|
||
with open('api_config.json', 'r', encoding='utf-8') as f:
|
||
api_config = json.load(f)
|
||
api_key = api_config.get('api_key', '')
|
||
if api_key:
|
||
print("✓ API密钥已加载")
|
||
os.environ['API_KEY'] = api_key
|
||
else:
|
||
print("⚠️ API密钥为空")
|
||
else:
|
||
print("⚠️ 未找到 api_config.json")
|
||
|
||
print("=" * 60)
|
||
print()
|
||
|
||
def test_db_connection():
|
||
"""测试数据库连接"""
|
||
print("测试数据库连接...")
|
||
|
||
try:
|
||
import mysql.connector
|
||
|
||
db_config = {
|
||
'host': os.environ.get('DB_HOST', 'localhost'),
|
||
'user': os.environ.get('DB_USER', 'root'),
|
||
'password': os.environ.get('DB_PASSWORD', 'taiyi1224'),
|
||
'database': os.environ.get('DB_NAME', 'exeprotector'),
|
||
'connect_timeout': 10
|
||
}
|
||
|
||
conn = mysql.connector.connect(**db_config)
|
||
cursor = conn.cursor()
|
||
|
||
# 检查表
|
||
cursor.execute("SHOW TABLES LIKE 'software_products'")
|
||
if cursor.fetchone():
|
||
print("✓ 数据库连接正常")
|
||
|
||
# 显示软件产品
|
||
cursor.execute("SELECT id, name FROM software_products")
|
||
products = cursor.fetchall()
|
||
if products:
|
||
print(f"✓ 找到 {len(products)} 个软件产品:")
|
||
for pid, name in products:
|
||
print(f" - {name} (ID:{pid})")
|
||
else:
|
||
print("⚠️ 数据库中没有软件产品,请先注册")
|
||
else:
|
||
print("✗ 数据库表不存在,请先初始化数据库")
|
||
cursor.close()
|
||
conn.close()
|
||
return False
|
||
|
||
cursor.close()
|
||
conn.close()
|
||
print()
|
||
return True
|
||
|
||
except mysql.connector.Error as e:
|
||
print(f"✗ 数据库连接失败: {e}")
|
||
print()
|
||
print("请检查:")
|
||
print(" 1. MySQL服务是否运行")
|
||
print(" 2. db_config.json 中的配置是否正确")
|
||
print(" 3. 数据库是否已创建")
|
||
return False
|
||
except ImportError:
|
||
print("✗ 未安装 mysql-connector-python")
|
||
print(" 安装: pip install mysql-connector-python")
|
||
return False
|
||
|
||
def start_server():
|
||
"""启动API服务器"""
|
||
print("=" * 60)
|
||
print("启动API服务器...")
|
||
print("=" * 60)
|
||
print()
|
||
print("监听地址: 0.0.0.0:5100")
|
||
print("健康检查: http://localhost:5100/api/health")
|
||
print("验证接口: http://localhost:5100/api/validate")
|
||
print()
|
||
print("按 Ctrl+C 停止服务器")
|
||
print("=" * 60)
|
||
print()
|
||
|
||
# 导入并运行服务器
|
||
from api_server_lite import app
|
||
app.run(host='0.0.0.0', port=5100, debug=False)
|
||
|
||
if __name__ == '__main__':
|
||
# 设置UTF-8编码
|
||
if sys.platform == 'win32':
|
||
try:
|
||
sys.stdout.reconfigure(encoding='utf-8')
|
||
except:
|
||
pass
|
||
|
||
print("\n" + "=" * 60)
|
||
print("API服务器启动工具")
|
||
print("=" * 60)
|
||
print()
|
||
|
||
# 加载配置
|
||
load_config()
|
||
|
||
# 测试数据库
|
||
if not test_db_connection():
|
||
print("=" * 60)
|
||
print("数据库连接失败,无法启动服务器")
|
||
print("=" * 60)
|
||
sys.exit(1)
|
||
|
||
# 启动服务器
|
||
try:
|
||
start_server()
|
||
except KeyboardInterrupt:
|
||
print("\n\n服务器已停止")
|
||
except Exception as e:
|
||
print(f"\n✗ 启动失败: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
sys.exit(1)
|
||
|