-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart_server.py
More file actions
87 lines (75 loc) · 2.48 KB
/
Copy pathstart_server.py
File metadata and controls
87 lines (75 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
WAF API服务器启动脚本
"""
import sys
import os
import logging
import traceback
# 设置日志
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.StreamHandler(),
logging.FileHandler('logs/server_startup.log', encoding='utf-8')
]
)
def main():
"""主函数"""
try:
logging.info("=" * 50)
logging.info("WAF API服务器启动中...")
logging.info("启动时间: %s", logging.Formatter().formatTime(logging.LogRecord('', 0, '', 0, '', (), None)))
logging.info("=" * 50)
# 检查Python版本
logging.info("Python版本: %s", sys.version)
logging.info("当前工作目录: %s", os.getcwd())
logging.info("Python路径: %s", sys.executable)
# 检查必要的模块
required_modules = ['fastapi', 'uvicorn', 'requests']
for module in required_modules:
try:
__import__(module)
logging.info("✓ %s 模块导入成功", module)
except ImportError as e:
logging.error("✗ %s 模块导入失败: %s", module, e)
return False
# 检查配置文件
try:
import config
logging.info("✓ 配置文件导入成功")
logging.info("API配置: host=%s, port=%s", config.API_CONFIG["host"], config.API_CONFIG["port"])
except Exception as e:
logging.error("✗ 配置文件导入失败: %s", e)
return False
# 检查日志目录
log_dir = "logs"
if not os.path.exists(log_dir):
os.makedirs(log_dir)
logging.info("创建日志目录: %s", log_dir)
# 启动服务器
logging.info("开始启动服务器...")
import uvicorn
from api_server import app
uvicorn.run(
app,
host=config.API_CONFIG["host"],
port=config.API_CONFIG["port"],
log_level="info",
reload=False,
workers=1,
loop="asyncio",
http="h11"
)
except Exception as e:
logging.error("服务器启动失败: %s", e)
logging.error("详细错误信息:")
traceback.print_exc()
return False
return True
if __name__ == "__main__":
success = main()
if not success:
sys.exit(1)