feat: 资产 CRUD API + 统计接口 + Vue3 管理前端(可选 API Key 鉴权)
This commit is contained in:
+42
-2
@@ -1,10 +1,31 @@
|
||||
"""vps-manager 后端入口"""
|
||||
"""vps-manager 后端入口
|
||||
|
||||
- 注册资产 CRUD 与统计路由
|
||||
- 挂载本地静态资源(Vue/Tailwind/前端逻辑)
|
||||
- 渲染前端 SPA 入口页面
|
||||
- 启用 CORS(便于外部程序/其他 AI 跨域调用写入接口)
|
||||
"""
|
||||
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import HTMLResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
|
||||
from app.core.config import settings
|
||||
from app.database import init_db
|
||||
from app.routers import assets, stats
|
||||
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
STATIC_DIR = BASE_DIR / "static"
|
||||
TEMPLATE_DIR = BASE_DIR / "app" / "templates"
|
||||
STATIC_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 直接使用 Jinja2 Environment 渲染(规避 Starlette Jinja2Templates 在 Python 3.14 下的缓存兼容问题)
|
||||
jinja_env = Environment(loader=FileSystemLoader(TEMPLATE_DIR), autoescape=True)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
@@ -14,7 +35,26 @@ async def lifespan(_: FastAPI):
|
||||
yield
|
||||
|
||||
|
||||
app = FastAPI(title="VPS 资产管理系统", version="0.1.2", lifespan=lifespan)
|
||||
app = FastAPI(title=settings.APP_NAME, version="0.2.0", lifespan=lifespan)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
app.include_router(assets.router)
|
||||
app.include_router(stats.router)
|
||||
|
||||
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
||||
|
||||
|
||||
@app.get("/", include_in_schema=False)
|
||||
def index() -> HTMLResponse:
|
||||
"""前端 SPA 入口"""
|
||||
html = jinja_env.get_template("index.html").render(app_name=settings.APP_NAME)
|
||||
return HTMLResponse(html)
|
||||
|
||||
|
||||
@app.get("/health")
|
||||
|
||||
Reference in New Issue
Block a user