x
This commit is contained in:
+35
-2
@@ -34,17 +34,21 @@ except ImportError as e:
|
||||
print(f" - {item}")
|
||||
raise
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.templating import Jinja2Templates
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
from api.auth_routes import router as auth_router
|
||||
from api.inventory import inventory_router
|
||||
from utils.logger import setup_logging
|
||||
from utils.logger import setup_logging, get_logger
|
||||
from database.init_db import init_database
|
||||
|
||||
setup_logging()
|
||||
logger = get_logger(__name__)
|
||||
|
||||
app = FastAPI(
|
||||
title="Gemold - 模具制造管理系统",
|
||||
@@ -52,6 +56,35 @@ app = FastAPI(
|
||||
version="4.0.0"
|
||||
)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=["*"],
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"],
|
||||
allow_headers=["*"],
|
||||
)
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def log_requests(request: Request, call_next):
|
||||
start_time = time.time()
|
||||
response = await call_next(request)
|
||||
duration = time.time() - start_time
|
||||
status = response.status_code
|
||||
|
||||
if status >= 400:
|
||||
auth_header = request.headers.get("authorization", "")
|
||||
token_preview = ""
|
||||
if auth_header.startswith("Bearer "):
|
||||
token_raw = auth_header[7:]
|
||||
token_preview = token_raw[:20] + "..." if len(token_raw) > 20 else token_raw
|
||||
logger.warning(
|
||||
f"[HTTP] {request.method} {request.url.path} -> {status} "
|
||||
f"({duration:.2f}s) "
|
||||
f"token={token_preview or 'none'}"
|
||||
)
|
||||
return response
|
||||
|
||||
# 启动时初始化数据库和RustFS
|
||||
@app.on_event("startup")
|
||||
async def startup_event():
|
||||
|
||||
Reference in New Issue
Block a user