19.FastAPI中间件
在FastAPI中,可以通过中间件在每个请求被特定的路径操作处理之前,以及在每个响应返回之前进行处理。FastAPI中间件是一个函数,其作用为:
19.1定义中间件
可以使用装饰器 @app.middleware("http") 来定义中间件,中间件函数的参数:
示例代码:
import time
from fastapi import FastAPI
from fastapi import Request
?
app = FastAPI()
?
@app.middleware("http")
async def middle(request: Request, call_next):
? ?start_time = time.time()
? ?response = await call_next(request)
? ?m = time.time() - start_time
? ?response.headers["X-Run-Time"] = str(m)
? ?return response
?
@app.get(path='/test')
async def test():
? ?return "Hello world."
执行请求:
curl http://127.0.0.1:8000/test -i
HTTP/1.1 200 OK
date: Tue, 01 Feb 2022 11:55:52 GMT
server: uvicorn
content-length: 14
content-type: application/json
x-run-time: 0.001463174819946289
?
"Hello world."
上面的代码中,通过中间件来计算请求的实际执行时间。
19.2请求及响应顺序
在使用中间件的情况下,FastAPI请求和响应的处理顺序为:中间件处理请求 -> 路由处理请求 -> 路由处理响应 ->中间件处理响应。代码示例:
# coding: utf-8
import time
from fastapi import FastAPI
from fastapi import Request
?
app = FastAPI()
?
@app.middleware("http")
async def middle(request: Request, call_next):
? ?print("middleware start")
? ?start_time = time.time()
? ?response = await call_next(request)
? ?m = time.time() - start_time
? ?response.headers["X-Run-Time"] = str(m)
? ?print("middleware end")
? ?return response
?
@app.get(path='/test')
async def test():
? ?print("test start")
? ?hello = "Hello world."
? ?print("test end")
? ?return hello
执行请求:
curl http://127.0.0.1:8000/test -i
HTTP/1.1 200 OK
date: Tue, 01 Feb 2022 12:10:19 GMT
server: uvicorn
content-length: 14
content-type: application/json
x-run-time: 0.002346038818359375
?
"Hello world."
后台print输出:
middleware start
test start
test end
middleware end
19.3中间件的用途
FastAPI的中间件可以拦截所有请求,同时也可以拦截所有响应,所以,一般情况下,可以针对请求和响应做一些统一的公共处理,比如:拦截请求后,记录用户的行为,或者对于特殊的处理进行鉴权等。
|