feat(backend): 添加中间件和日志功能
- 新增跨域、gzip压缩和trace中间件 - 实现请求ID生成和日志记录功能 - 优化环境变量加载和数据库连接逻辑- 重构部分代码以提高可维护性main
parent
f0f777dcc7
commit
560887d306
@ -0,0 +1,25 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
|
||||
def add_cors_middleware(app: FastAPI):
|
||||
"""
|
||||
添加跨域中间件
|
||||
|
||||
:param app: FastAPI对象
|
||||
:return:
|
||||
"""
|
||||
# 前端页面url
|
||||
origins = [
|
||||
'http://localhost:80',
|
||||
'http://127.0.0.1:80',
|
||||
]
|
||||
|
||||
# 后台api允许跨域
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=origins,
|
||||
allow_credentials=True,
|
||||
allow_methods=['*'],
|
||||
allow_headers=['*'],
|
||||
)
|
||||
@ -0,0 +1,12 @@
|
||||
from fastapi import FastAPI
|
||||
from starlette.middleware.gzip import GZipMiddleware
|
||||
|
||||
|
||||
def add_gzip_middleware(app: FastAPI):
|
||||
"""
|
||||
添加gzip压缩中间件
|
||||
|
||||
:param app: FastAPI对象
|
||||
:return:
|
||||
"""
|
||||
app.add_middleware(GZipMiddleware, minimum_size=1000, compresslevel=9)
|
||||
@ -0,0 +1,16 @@
|
||||
from fastapi import FastAPI
|
||||
from middlewares.cors_middleware import add_cors_middleware
|
||||
from middlewares.gzip_middleware import add_gzip_middleware
|
||||
from middlewares.trace_middleware import add_trace_middleware
|
||||
|
||||
def handle_middleware(app: FastAPI):
|
||||
"""
|
||||
全局中间件处理
|
||||
"""
|
||||
# 加载跨域中间件
|
||||
add_cors_middleware(app)
|
||||
# 加载gzip压缩中间件
|
||||
add_gzip_middleware(app)
|
||||
# 加载trace中间件
|
||||
add_trace_middleware(app)
|
||||
|
||||
@ -0,0 +1,17 @@
|
||||
from fastapi import FastAPI
|
||||
from .ctx import TraceCtx
|
||||
from .middle import TraceASGIMiddleware
|
||||
|
||||
__all__ = ('TraceASGIMiddleware', 'TraceCtx')
|
||||
|
||||
__version__ = '0.1.0'
|
||||
|
||||
|
||||
def add_trace_middleware(app: FastAPI):
|
||||
"""
|
||||
添加trace中间件
|
||||
|
||||
:param app: FastAPI对象
|
||||
:return:
|
||||
"""
|
||||
app.add_middleware(TraceASGIMiddleware)
|
||||
@ -0,0 +1,23 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@author: peng
|
||||
@file: ctx.py
|
||||
@time: 2025/1/17 16:57
|
||||
"""
|
||||
|
||||
import contextvars
|
||||
from uuid import uuid4
|
||||
|
||||
CTX_REQUEST_ID: contextvars.ContextVar[str] = contextvars.ContextVar('request-id', default='')
|
||||
|
||||
|
||||
class TraceCtx:
|
||||
@staticmethod
|
||||
def set_id():
|
||||
_id = uuid4().hex
|
||||
CTX_REQUEST_ID.set(_id)
|
||||
return _id
|
||||
|
||||
@staticmethod
|
||||
def get_id():
|
||||
return CTX_REQUEST_ID.get()
|
||||
@ -0,0 +1,47 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
@author: peng
|
||||
@file: middle.py
|
||||
@time: 2025/1/17 16:57
|
||||
"""
|
||||
|
||||
from functools import wraps
|
||||
from starlette.types import ASGIApp, Message, Receive, Scope, Send
|
||||
from .span import get_current_span, Span
|
||||
|
||||
|
||||
class TraceASGIMiddleware:
|
||||
"""
|
||||
fastapi-example:
|
||||
app = FastAPI()
|
||||
app.add_middleware(TraceASGIMiddleware)
|
||||
"""
|
||||
|
||||
def __init__(self, app: ASGIApp) -> None:
|
||||
self.app = app
|
||||
|
||||
@staticmethod
|
||||
async def my_receive(receive: Receive, span: Span):
|
||||
await span.request_before()
|
||||
|
||||
@wraps(receive)
|
||||
async def my_receive():
|
||||
message = await receive()
|
||||
await span.request_after(message)
|
||||
return message
|
||||
|
||||
return my_receive
|
||||
|
||||
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
|
||||
if scope['type'] != 'http':
|
||||
await self.app(scope, receive, send)
|
||||
return
|
||||
|
||||
async with get_current_span(scope) as span:
|
||||
handle_outgoing_receive = await self.my_receive(receive, span)
|
||||
|
||||
async def handle_outgoing_request(message: 'Message') -> None:
|
||||
await span.response(message)
|
||||
await send(message)
|
||||
|
||||
await self.app(scope, handle_outgoing_receive, handle_outgoing_request)
|
||||
Loading…
Reference in New Issue