forked from ZhuLinsen/daily_stock_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebui.py
More file actions
143 lines (121 loc) · 4.52 KB
/
Copy pathwebui.py
File metadata and controls
143 lines (121 loc) · 4.52 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# -*- coding: utf-8 -*-
"""
===================================
WebUI 入口文件 (向后兼容)
===================================
本文件保持向后兼容,实际实现已迁移到 web/ 包
结构说明:
web/
├── __init__.py - 包初始化
├── server.py - HTTP 服务器
├── router.py - 路由分发
├── handlers.py - 请求处理器
├── services.py - 业务服务层
└── templates.py - HTML 模板
API Endpoints:
GET / - 配置页面
GET /health - 健康检查
GET /analysis?code=xxx - 触发单只股票异步分析
GET /tasks - 查询任务列表
GET /task?id=xxx - 查询任务状态
POST /update - 更新配置
Usage:
python webui.py
WEBUI_HOST=0.0.0.0 WEBUI_PORT=8000 python webui.py
"""
from __future__ import annotations
import os
import logging
# 从 web 包导入(新架构)
from web.server import WebServer, run_server_in_thread, run_server
from web.router import Router, get_router
from web.services import ConfigService, AnalysisService, get_config_service, get_analysis_service
from web.handlers import PageHandler, ApiHandler
from web.templates import render_config_page, render_error_page
logger = logging.getLogger(__name__)
# 导出所有公共接口(保持向后兼容)
__all__ = [
# 服务器
'WebServer',
'run_server_in_thread',
'run_server',
# 路由
'Router',
'get_router',
# 服务
'ConfigService',
'AnalysisService',
'get_config_service',
'get_analysis_service',
# 处理器
'PageHandler',
'ApiHandler',
# 模板
'render_config_page',
'render_error_page',
]
def _start_bot_stream_clients() -> None:
"""启动 Bot Stream 模式客户端(如果已配置)"""
from src.config import get_config
config = get_config()
# 钉钉 Stream 模式
if config.dingtalk_stream_enabled:
try:
from bot.platforms import start_dingtalk_stream_background, DINGTALK_STREAM_AVAILABLE
if DINGTALK_STREAM_AVAILABLE:
if start_dingtalk_stream_background():
logger.info("[WebUI] 钉钉 Stream 客户端已在后台启动")
else:
logger.warning("[WebUI] 钉钉 Stream 客户端启动失败")
else:
logger.warning("[WebUI] 钉钉 Stream 模式已启用但 SDK 未安装")
logger.warning("[WebUI] 请运行: pip install dingtalk-stream")
except Exception as e:
logger.error(f"[WebUI] 启动钉钉 Stream 客户端失败: {e}")
# 飞书 Stream 模式
if getattr(config, 'feishu_stream_enabled', False):
try:
from bot.platforms import start_feishu_stream_background, FEISHU_SDK_AVAILABLE
if FEISHU_SDK_AVAILABLE:
if start_feishu_stream_background():
logger.info("[WebUI] 飞书 Stream 客户端已在后台启动")
else:
logger.warning("[WebUI] 飞书 Stream 客户端启动失败")
else:
logger.warning("[WebUI] 飞书 Stream 模式已启用但 SDK 未安装")
logger.warning("[WebUI] 请运行: pip install lark-oapi")
except Exception as e:
logger.error(f"[WebUI] 启动飞书 Stream 客户端失败: {e}")
def main() -> int:
"""
主入口函数
支持环境变量配置:
WEBUI_HOST: 监听地址 (默认 127.0.0.1)
WEBUI_PORT: 监听端口 (默认 8000)
"""
host = os.getenv("WEBUI_HOST", "127.0.0.1")
port = int(os.getenv("WEBUI_PORT", "8000"))
print(f"WebUI running: http://{host}:{port}")
print("API Endpoints:")
print(" GET / - 配置页面")
print(" GET /health - 健康检查")
print(" GET /analysis?code=xxx - 触发分析")
print(" GET /tasks - 任务列表")
print(" GET /task?id=xxx - 任务状态")
print(" POST /update - 更新配置")
print()
print("Bot Webhooks:")
print(" POST /bot/feishu - 飞书机器人")
print(" POST /bot/dingtalk - 钉钉机器人")
print(" POST /bot/wecom - 企业微信机器人")
print(" POST /bot/telegram - Telegram 机器人")
print()
# 启动 Bot Stream 客户端(如果配置了)
_start_bot_stream_clients()
try:
run_server(host=host, port=port)
except KeyboardInterrupt:
pass
return 0
if __name__ == "__main__":
raise SystemExit(main())