|
2 | 2 | # -*- coding:utf-8 -*- |
3 | 3 | # __author__ = '__Jack__' |
4 | 4 |
|
| 5 | +import time |
| 6 | + |
5 | 7 | import uvicorn |
6 | | -from fastapi import FastAPI |
7 | | -from fastapi.exceptions import RequestValidationError |
8 | | -from fastapi.responses import PlainTextResponse |
9 | | -from starlette.exceptions import HTTPException as StarletteHTTPException |
| 8 | +from fastapi import FastAPI, Request |
| 9 | +from fastapi.middleware.cors import CORSMiddleware |
10 | 10 |
|
11 | 11 | from coronavirus import application |
12 | | -from tutorial import app03, app04, app05, app06 |
| 12 | +from tutorial import app03, app04, app05, app06, app08 |
| 13 | + |
| 14 | +# from fastapi.exceptions import RequestValidationError |
| 15 | +# from fastapi.responses import PlainTextResponse |
| 16 | +# from starlette.exceptions import HTTPException as StarletteHTTPException |
13 | 17 |
|
14 | 18 | app = FastAPI( |
15 | 19 | title='FastAPI Tutorial and Coronavirus Tracker API Docs', |
|
40 | 44 | # return PlainTextResponse(str(exc), status_code=400) |
41 | 45 |
|
42 | 46 |
|
43 | | -app.include_router(app03, prefix="/chapter03", tags=['第三章 请求参数和验证']) |
44 | | -app.include_router(app04, prefix="/chapter04", tags=['第四章 响应处理和FastAPI配置']) |
45 | | -app.include_router(app05, prefix="/chapter05", tags=['第五章 FastAPI的依赖注入系统']) |
46 | | -app.include_router(app06, prefix="/chapter06", tags=['第六章 安全、认证和授权']) |
47 | | -app.include_router(application, prefix="/coronavirus", tags=['新冠病毒疫情跟踪器API']) |
| 47 | +@app.middleware('http') |
| 48 | +async def add_process_time_header(request: Request, call_next): # call_next将接收request请求最为参数 |
| 49 | + start_time = time.time() |
| 50 | + response = await call_next(request) |
| 51 | + process_time = time.time() - start_time |
| 52 | + response.headers['X-Process-Time'] = str(process_time) # 添加自定义的以“X-”开头的请求头 |
| 53 | + return response |
| 54 | + |
| 55 | + |
| 56 | +app.add_middleware( |
| 57 | + CORSMiddleware, |
| 58 | + allow_origins=[ |
| 59 | + "http://127.0.0.1.tiangolo.com", |
| 60 | + "https://127.0.0.1.tiangolo.com", |
| 61 | + "http://127.0.0.1", |
| 62 | + "http://127.0.0.1:8080", |
| 63 | + ], |
| 64 | + allow_credentials=True, |
| 65 | + allow_methods=["*"], |
| 66 | + allow_headers=["*"], |
| 67 | +) |
| 68 | + |
| 69 | +app.include_router(app03, prefix='/chapter03', tags=['第三章 请求参数和验证']) |
| 70 | +app.include_router(app04, prefix='/chapter04', tags=['第四章 响应处理和FastAPI配置']) |
| 71 | +app.include_router(app05, prefix='/chapter05', tags=['第五章 FastAPI的依赖注入系统']) |
| 72 | +app.include_router(app06, prefix='/chapter06', tags=['第六章 安全、认证和授权']) |
| 73 | +app.include_router(app08, prefix='/chapter08', tags=['第八章 中间件、CORS、后台任务、测试用例']) |
| 74 | +app.include_router(application, prefix='/coronavirus', tags=['新冠病毒疫情跟踪器API']) |
48 | 75 |
|
49 | 76 | if __name__ == '__main__': |
50 | | - uvicorn.run("run:app", host="0.0.0.0", port=8000, reload=True, debug=True, workers=1) |
| 77 | + uvicorn.run('run:app', host='0.0.0.0', port=8000, reload=True, debug=True, workers=1) |
0 commit comments