Files
37duAutomation/controlplane/app.py
T
puddy cb04790d28 refactor: 重构项目目录结构与代码组织
1.  重构controlplane与webhost后端的包结构,按领域拆分模块到core/apis子包
2.  移动并整理工具脚本到tools子目录,统一管理运维脚本
3.  重构插件运行时、调度、作业管理等核心模块到core子包
4.  更新AGENTS.md文档,明确各代码形态的组织规范
5.  清理重复的旧文件与冗余代码
2026-09-14 12:34:54 +08:00

91 lines
3.6 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:controlplane.db(同目录)。包文件:packages/。可选:CONTROLPLANE_TOKEN 非空时客户端须带
X-API-Token 请求头。
运行(本地 .venv):
python controlplane/app.py # 首次自动建表 + 种子 + 生成示例包
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 fastapi.staticfiles import StaticFiles
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)
# ==================== 挂载控制面前端产物 ====================
# 源码模式 => config.DIST(controlplane/frontend/dist);exe 模式 => _MEIPASS/frontend/dist(打进 exe)
if config.DIST.is_dir():
app.mount("/", StaticFiles(directory=str(config.DIST), html=True), name="web")
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} sqlite={config.DB_PATH.name}")
uvicorn.run(app, host="0.0.0.0", port=config.CONTROLPLANE_PORT,
log_config=LOG_CONFIG, log_level="info")