140 lines
5.6 KiB
Python
140 lines
5.6 KiB
Python
"""
|
||
NeoZQYY 后端 API 入口
|
||
|
||
基于 FastAPI 构建,为管理后台和微信小程序提供 RESTful API。
|
||
OpenAPI 文档自动生成于 /docs(Swagger UI)和 /redoc(ReDoc)。
|
||
"""
|
||
|
||
from contextlib import asynccontextmanager
|
||
|
||
from fastapi import FastAPI
|
||
from fastapi.middleware.cors import CORSMiddleware
|
||
|
||
from app import config
|
||
# CHANGE 2026-02-19 | 新增 xcx_test 路由(MVP 验证)+ wx_callback 路由(微信消息推送)
|
||
# CHANGE 2026-02-23 | 新增 ops_panel 路由(运维控制面板)
|
||
# CHANGE 2026-02-25 | 新增 xcx_auth 路由(小程序微信登录 + 申请 + 状态查询 + 店铺切换)
|
||
# CHANGE 2026-02-26 | member_birthday 路由替换为 member_retention_clue(维客线索重构)
|
||
# CHANGE 2026-02-26 | 新增 admin_applications 路由(管理端申请审核)
|
||
# CHANGE 2026-02-27 | 新增 xcx_tasks / xcx_notes 路由(小程序核心业务)
|
||
from app.routers import auth, execution, schedules, tasks, env_config, db_viewer, etl_status, xcx_test, wx_callback, member_retention_clue, ops_panel, xcx_auth, admin_applications, business_day, xcx_tasks, xcx_notes
|
||
from app.services.scheduler import scheduler
|
||
from app.services.task_queue import task_queue
|
||
from app.ws.logs import ws_router
|
||
|
||
|
||
@asynccontextmanager
|
||
async def lifespan(app: FastAPI):
|
||
"""应用生命周期:启动时拉起后台服务,关闭时优雅停止。"""
|
||
# CHANGE 2026-03-07 | 启动横幅:打印关键路径,便于诊断连到了哪个实例
|
||
import sys
|
||
_banner = (
|
||
"\n"
|
||
"╔══════════════════════════════════════════════════════╗\n"
|
||
"║ NeoZQYY Backend — 启动诊断 ║\n"
|
||
"╠══════════════════════════════════════════════════════╣\n"
|
||
f"║ Python: {sys.executable}\n"
|
||
f"║ ROOT: {config._project_root}\n"
|
||
f"║ ETL_PATH: {config.ETL_PROJECT_PATH}\n"
|
||
f"║ ETL_PY: {config.ETL_PYTHON_EXECUTABLE}\n"
|
||
f"║ OPS_BASE: {config.OPS_SERVER_BASE}\n"
|
||
f"║ APP_DB: {config.APP_DB_NAME}\n"
|
||
f"║ .env: {config._root_env}\n"
|
||
"╚══════════════════════════════════════════════════════╝\n"
|
||
)
|
||
print(_banner, flush=True)
|
||
|
||
# 启动
|
||
task_queue.start()
|
||
scheduler.start()
|
||
|
||
# CHANGE 2026-02-27 | 注册触发器 job handler(核心业务模块)
|
||
from app.services.trigger_scheduler import register_job
|
||
from app.services import task_generator, task_expiry, recall_detector, note_reclassifier
|
||
|
||
register_job("task_generator", lambda **_kw: task_generator.run())
|
||
register_job("task_expiry_check", lambda **_kw: task_expiry.run())
|
||
register_job("recall_completion_check", recall_detector.run)
|
||
register_job("note_reclassify_backfill", note_reclassifier.run)
|
||
|
||
yield
|
||
# 关闭
|
||
await scheduler.stop()
|
||
await task_queue.stop()
|
||
|
||
|
||
app = FastAPI(
|
||
title="NeoZQYY API",
|
||
description="台球门店运营助手 — 后端 API(管理后台 + 微信小程序)",
|
||
version="0.1.0",
|
||
docs_url="/docs",
|
||
redoc_url="/redoc",
|
||
lifespan=lifespan,
|
||
)
|
||
|
||
# ---- CORS 中间件 ----
|
||
# 允许来源从环境变量 CORS_ORIGINS 读取,缺省允许 Vite 开发服务器 (localhost:5173)
|
||
app.add_middleware(
|
||
CORSMiddleware,
|
||
allow_origins=config.CORS_ORIGINS,
|
||
allow_credentials=True,
|
||
allow_methods=["*"],
|
||
allow_headers=["*"],
|
||
)
|
||
|
||
# ---- 路由注册 ----
|
||
app.include_router(auth.router)
|
||
app.include_router(tasks.router)
|
||
app.include_router(execution.router)
|
||
app.include_router(schedules.router)
|
||
app.include_router(env_config.router)
|
||
app.include_router(db_viewer.router)
|
||
app.include_router(etl_status.router)
|
||
app.include_router(ws_router)
|
||
app.include_router(xcx_test.router)
|
||
app.include_router(wx_callback.router)
|
||
app.include_router(member_retention_clue.router)
|
||
app.include_router(ops_panel.router)
|
||
app.include_router(xcx_auth.router)
|
||
app.include_router(admin_applications.router)
|
||
app.include_router(business_day.router)
|
||
app.include_router(xcx_tasks.router)
|
||
app.include_router(xcx_notes.router)
|
||
|
||
|
||
@app.get("/health", tags=["系统"])
|
||
async def health_check():
|
||
"""健康检查端点,用于探活和监控。"""
|
||
return {"status": "ok"}
|
||
|
||
|
||
# CHANGE 2026-03-07 | 诊断端点:返回关键路径配置,用于确认连到的是哪个实例
|
||
@app.get("/debug/config-paths", tags=["系统"])
|
||
async def debug_config_paths():
|
||
"""返回当前后端实例的关键路径配置(仅开发环境使用)。"""
|
||
import sys
|
||
import os
|
||
import platform
|
||
from app.services.cli_builder import cli_builder as _cb
|
||
from app.schemas.tasks import TaskConfigSchema as _TCS
|
||
|
||
_test_cfg = _TCS(flow="api_ods_dwd", processing_mode="increment_only",
|
||
tasks=["DWD_LOAD_FROM_ODS"], store_id=123)
|
||
_test_cmd = _cb.build_command(
|
||
_test_cfg, config.ETL_PROJECT_PATH,
|
||
python_executable=config.ETL_PYTHON_EXECUTABLE,
|
||
)
|
||
_test_cmd_str = " ".join(_test_cmd)
|
||
|
||
return {
|
||
"hostname": platform.node(),
|
||
"python_executable": sys.executable,
|
||
"project_root": str(config._project_root),
|
||
"env_file": str(config._root_env),
|
||
"etl_python_executable": config.ETL_PYTHON_EXECUTABLE,
|
||
"etl_project_path": config.ETL_PROJECT_PATH,
|
||
"simulated_command": _test_cmd_str,
|
||
"NEOZQYY_ROOT_env": os.environ.get("NEOZQYY_ROOT", "<未设置>"),
|
||
"cwd": os.getcwd(),
|
||
}
|