feat: 搭建控制面管理前端并剥离底座管理功能
1. 新增controlplane/frontend Vue3管理前端,实现登录、账号CRUD、插件上传/管理、权限授权 2. 为controlplane/app.py添加CORS中间件并挂载前端静态产物 3. 重构环境变量加载,统一使用app/common/env.load_app_env() 4. 剥离webhost/的管理UI与代理接口,仅保留基础登录能力 5. 新增环境隔离规范文档与架构蓝图说明
This commit is contained in:
+47
-1
@@ -21,18 +21,51 @@ import hashlib
|
||||
import os
|
||||
import secrets
|
||||
import sqlite3
|
||||
import sys
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import List
|
||||
|
||||
from fastapi import FastAPI, File, Header, HTTPException, Query, UploadFile
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from pydantic import BaseModel
|
||||
|
||||
APP_DIR = Path(__file__).resolve().parent
|
||||
# 数据目录定位:打包成 exe(冻结) 时统一放 exe 同目录,保证 controlplane.db / packages/ 持久可写;
|
||||
# 源码运行时沿用 controlplane/ 目录。禁止用 PyInstaller 临时解压目录(重启会丢数据)。
|
||||
if getattr(sys, "frozen", False):
|
||||
_APP_BASE = Path(sys.executable).resolve().parent
|
||||
else:
|
||||
_APP_BASE = Path(__file__).resolve().parent
|
||||
APP_DIR = _APP_BASE
|
||||
DB_PATH = APP_DIR / "controlplane.db"
|
||||
PKG_DIR = APP_DIR / "packages"
|
||||
|
||||
|
||||
# ==================== 配置固化:加载本模块 env(controlplane/.env;免命令行传参;环境变量优先) ====================
|
||||
def _load_dotenv() -> None:
|
||||
"""源码模式读 controlplane/.env;exe 模式读 exe 同目录 .env。UTF-8、'#'/'空行'跳过。"""
|
||||
env_path = APP_DIR / ".env"
|
||||
# APP_DIR 源码态为 controlplane/,exe 态为 exe 同目录,均为本模块专属 env
|
||||
if not env_path.is_file():
|
||||
return
|
||||
try:
|
||||
for _ln in env_path.read_text(encoding="utf-8").splitlines():
|
||||
_line = _ln.strip()
|
||||
if not _line or _line.startswith("#") or "=" not in _line:
|
||||
continue
|
||||
_k, _v = _line.split("=", 1)
|
||||
_k, _v = _k.strip(), _v.strip()
|
||||
if _k and _k not in os.environ:
|
||||
os.environ[_k] = _v
|
||||
except OSError as _exc: # noqa: BLE001
|
||||
pass
|
||||
|
||||
|
||||
_load_dotenv()
|
||||
|
||||
CONTROLPLANE_TOKEN = os.getenv("CONTROLPLANE_TOKEN", "")
|
||||
|
||||
|
||||
@@ -162,6 +195,8 @@ async def lifespan(app: FastAPI):
|
||||
|
||||
|
||||
app = FastAPI(title="37du 内网插件控制面", lifespan=lifespan)
|
||||
app.add_middleware(CORSMiddleware, allow_origins=["*"],
|
||||
allow_methods=["*"], allow_headers=["*"])
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
@@ -546,6 +581,17 @@ def admin_plugin_delete(d: PluginDelIn,
|
||||
return {"ok": True, "plugin_id": plugin_id}
|
||||
|
||||
|
||||
# ==================== 挂载控制面前端产物 ====================
|
||||
# 源码模式 => controlplane/frontend/dist;exe 模式 => sys._MEIPASS/frontend/dist(打进 exe)
|
||||
if getattr(sys, "frozen", False):
|
||||
DIST = Path(getattr(sys, "_MEIPASS", APP_DIR)) / "frontend" / "dist"
|
||||
else:
|
||||
DIST = APP_DIR / "frontend" / "dist"
|
||||
|
||||
if DIST.is_dir():
|
||||
app.mount("/", StaticFiles(directory=str(DIST), html=True), name="web")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
|
||||
Reference in New Issue
Block a user