Files
37duAutomation/desktop/controlplane/app.py
T
puddy 84d3e103ec refactor: 整体迁移控制面至MySQL + 替换UI框架为Element Plus
1. 移除naive-ui依赖,迁移前端至Element Plus
2. 重构后端数据库层,切换为MySQL替代SQLite
3. 调整项目部署架构,停止exe打包改为源码部署
4. 新增设备上报接口与相关数据库模型
5. 优化配置路径与SQL语法,替换为MySQL占位符格式
6. 调整站点名称与文案,统一为「37du 插件管理平台」
7. 新增SQLite到MySQL迁移脚本
2026-09-16 15:58:45 +08:00

85 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""内网插件控制面(中心机)入口(组装层)。
业务逻辑已按领域拆到 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 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.apis import admin as api_admin
from controlplane.apis import auth as api_auth
from controlplane.apis import dingtalk as api_dingtalk
from controlplane.apis import meta as api_meta
from controlplane.apis import plugins as api_plugins
@asynccontextmanager
async def lifespan(app: FastAPI):
_init_db()
_seed()
yield
app = FastAPI(title="37du 插件管理平台", lifespan=lifespan)
app.add_middleware(CORSMiddleware, allow_origins=["*"],
allow_methods=["*"], allow_headers=["*"])
app.include_router(api_meta.router)
app.include_router(api_plugins.router)
app.include_router(api_auth.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")