- POST/GET /api/schedules/backup 增加 flows 支持,schema_version=2 - 老客户端只推 schedules 时保留已有 flows,避免流程编排被抹掉 - 新增上限:flows 200 条、备份体 2MB(413/400 明确报错) - 同步旧 FastAPI 版 schedules.py(仅兼容维护,内网生产为 controlplane-next)
123 lines
5.7 KiB
Python
123 lines
5.7 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""内网插件控制面(中心机)入口(组装层)。
|
||
|
||
!!! DEPRECATED(2026-10 起)!!!
|
||
这是第一代中心机(FastAPI),已由 desktop/controlplane-next(cool-admin-midway)取代。
|
||
只做必要的问题修复与兼容维护,**不接受新功能**;计划在存量执行机升级完成后下线。
|
||
迁移背景见 docs/架构/服务端迁移方案_cool-admin.md,维护约定见本目录 README.md。
|
||
|
||
业务逻辑已按领域拆到 sibling 模块,本文件仅承担:lifespan 初始化、创建 app、
|
||
装配各领域路由、挂载前端产物、uvicorn 启动。
|
||
- config.py 配置/路径/密钥 - db.py 数据层(建表/种子/审计)
|
||
- crypto.py 口令哈希/Ed25519 - auth.py 鉴权(凭证/票据/防爆破/云端会话)
|
||
- dingtalk.py 钉钉官方接口 - pkgutil.py 插件包校验/解析/版本
|
||
- schemas.py 请求体模型 - apis/ 路由子包(meta/plugins/auth/dingtalk/admin)
|
||
|
||
DB:MySQL(连接参数在 controlplane/.env 的 MYSQL_*,库 37du_center;首次启动自动建库建表+种子)。
|
||
包文件:packages/。可选:CONTROLPLANE_TOKEN 非空时客户端须带
|
||
X-API-Token 请求头。
|
||
|
||
运行(本地 .venv,需在 desktop/ 目录下执行,模块搜索根 = desktop/):
|
||
python -m controlplane.app # 首次自动建表 + 种子 + 生成示例包
|
||
uvicorn controlplane.app:app --port 8800
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import logging
|
||
import os
|
||
import sys
|
||
from contextlib import asynccontextmanager
|
||
|
||
from fastapi import FastAPI
|
||
from fastapi.middleware.cors import CORSMiddleware
|
||
|
||
from controlplane import config
|
||
from controlplane.core.db import _init_db, _seed
|
||
from controlplane.core.logstore import cleanup_expired, start_log_cleanup
|
||
from controlplane.core.monitor import start_fileserver_monitor
|
||
from controlplane.apis import admin as api_admin
|
||
from controlplane.apis import auth as api_auth
|
||
from controlplane.apis import config_push as api_config_push
|
||
from controlplane.apis import dingtalk as api_dingtalk
|
||
from controlplane.apis import file_sources as api_file_sources
|
||
from controlplane.apis import fileserver_status as api_fileserver_status
|
||
from controlplane.apis import meta as api_meta
|
||
from controlplane.apis import notifications as api_notifications
|
||
from controlplane.apis import plugins as api_plugins
|
||
from controlplane.apis import schedules as api_schedules
|
||
|
||
_LOGGER = logging.getLogger(__name__)
|
||
|
||
|
||
@asynccontextmanager
|
||
async def lifespan(app: FastAPI):
|
||
if not config.CONTROLPLANE_TOKEN:
|
||
# 内部凭证为空时客户端接口无鉴权(P0 安全债)。不阻断本地开发,但生产必须配置:
|
||
# 公网部署时请务必在 controlplane/.env 设置 CONTROLPLANE_TOKEN。
|
||
_LOGGER.error(
|
||
"CONTROLPLANE_TOKEN 未配置:全部客户端接口将无鉴权放行,生产环境禁止!")
|
||
_init_db()
|
||
_seed()
|
||
# 集中日志保留期清理:启动即清一次超期日志 + 常驻周期清理(daemon 线程)
|
||
try:
|
||
cleanup_expired()
|
||
except Exception: # noqa: BLE001 启动清理失败不阻断服务(周期线程会继续重试)
|
||
_LOGGER.exception("集中日志启动清理失败")
|
||
start_log_cleanup()
|
||
# 内网文件服务器离线监控:心跳超时判离线并钉钉告警管理员(daemon 线程)
|
||
start_fileserver_monitor()
|
||
yield
|
||
|
||
|
||
app = FastAPI(title="37du 插件管理平台", lifespan=lifespan)
|
||
# CORS 收敛为白名单(原 allow_origins=["*"],控制面走公网时任何站点可跨域调接口)。
|
||
# 默认放行本地控制面前端(8090 静态站);公网/反代部署用 CORS_ALLOW_ORIGINS 配置实际来源。
|
||
_CORS_ORIGINS = [o.strip() for o in os.getenv(
|
||
"CORS_ALLOW_ORIGINS",
|
||
"http://127.0.0.1:8090,http://localhost:8090").split(",") if o.strip()]
|
||
app.add_middleware(CORSMiddleware, allow_origins=_CORS_ORIGINS,
|
||
allow_methods=["*"], allow_headers=["*"])
|
||
|
||
app.include_router(api_meta.router)
|
||
app.include_router(api_plugins.router)
|
||
app.include_router(api_notifications.router)
|
||
app.include_router(api_auth.router)
|
||
app.include_router(api_config_push.router)
|
||
app.include_router(api_file_sources.router)
|
||
app.include_router(api_fileserver_status.router)
|
||
app.include_router(api_schedules.router)
|
||
app.include_router(api_dingtalk.router)
|
||
app.include_router(api_admin.router)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
import uvicorn
|
||
|
||
# PyInstaller --noconsole 下 sys.stdout/stderr 可能为 None,
|
||
# uvicorn 默认 DefaultFormatter 会调用 isatty() 导致启动崩溃,故自定义日志配置绕开颜色检测。
|
||
LOG_CONFIG = {
|
||
"version": 1,
|
||
"disable_existing_loggers": False,
|
||
"formatters": {
|
||
"default": {"format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s"},
|
||
"access": {"format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s"},
|
||
},
|
||
"handlers": {
|
||
"default": {"class": "logging.StreamHandler", "formatter": "default"},
|
||
"access": {"class": "logging.StreamHandler", "formatter": "access"},
|
||
},
|
||
"loggers": {
|
||
"uvicorn": {"handlers": ["default"], "level": "INFO", "propagate": False},
|
||
"uvicorn.error": {"handlers": ["default"], "level": "INFO", "propagate": False},
|
||
"uvicorn.access": {"handlers": ["access"], "level": "INFO", "propagate": False},
|
||
},
|
||
}
|
||
if getattr(sys, "frozen", False):
|
||
if sys.stdout is None:
|
||
sys.stdout = open(os.devnull, "w")
|
||
if sys.stderr is None:
|
||
sys.stderr = open(os.devnull, "w")
|
||
|
||
print(f"[controlplane] http://0.0.0.0:{config.CONTROLPLANE_PORT} mysql={config.MYSQL_DB}@{config.MYSQL_HOST}")
|
||
uvicorn.run(app, host="0.0.0.0", port=config.CONTROLPLANE_PORT,
|
||
log_config=LOG_CONFIG, log_level="info") |