feat: FastAPI 挂载前端静态文件
- GET / -> frontend/index.html 客户充值页 - GET /admin -> frontend/admin.html 管理后台 - /static/* -> frontend/ 目录静态资源 - requirements.txt 补充 aiofiles 依赖 Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -21,10 +21,13 @@ import json
|
||||
import logging
|
||||
import time
|
||||
from contextlib import asynccontextmanager
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import FastAPI, HTTPException, Header, Depends
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import FileResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from pydantic import BaseModel
|
||||
|
||||
import config as cfg_mod
|
||||
@@ -33,6 +36,8 @@ import upstream as up
|
||||
import poller
|
||||
from crypto import encode_our_cdk, decode_our_cdk, key_from_hex
|
||||
|
||||
FRONTEND_DIR = Path(__file__).parent.parent / "frontend"
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger("app")
|
||||
|
||||
@@ -59,6 +64,18 @@ app.add_middleware(
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
# 前端静态文件(index.html / admin.html 等)
|
||||
if FRONTEND_DIR.exists():
|
||||
app.mount("/static", StaticFiles(directory=str(FRONTEND_DIR)), name="static")
|
||||
|
||||
@app.get("/")
|
||||
def serve_index():
|
||||
return FileResponse(str(FRONTEND_DIR / "index.html"))
|
||||
|
||||
@app.get("/admin")
|
||||
def serve_admin():
|
||||
return FileResponse(str(FRONTEND_DIR / "admin.html"))
|
||||
|
||||
|
||||
# ─── 鉴权依赖 ─────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
@@ -3,3 +3,4 @@ uvicorn[standard]>=0.29.0
|
||||
pydantic>=2.0.0
|
||||
requests>=2.31.0
|
||||
pycryptodome>=3.20.0
|
||||
aiofiles>=23.0.0
|
||||
|
||||
Reference in New Issue
Block a user