|
21 | 21 | title='FastAPI Tutorial and Coronavirus Tracker API Docs', |
22 | 22 | description='FastAPI教程 新冠病毒疫情跟踪器API接口文档,项目代码:https://github.com/liaogx/fastapi-tutorial', |
23 | 23 | version='1.0.0', |
24 | | - docs_url='/docs', |
25 | | - redoc_url='/redocs', |
| 24 | + docs_url=None, |
| 25 | + redoc_url=None, |
26 | 26 | ) |
27 | 27 |
|
28 | 28 | # mount表示将某个目录下一个完全独立的应用挂载过来,这个不会在API交互文档中显示 |
29 | | -app.mount(path='/static', app=StaticFiles(directory='./coronavirus/static'), name='static') # .mount()不要在分路由APIRouter().mount()调用,模板会报错 |
| 29 | +# app.mount(path='/static', app=StaticFiles(directory='./coronavirus/static'), name='static') # .mount()不要在分路由APIRouter().mount()调用,模板会报错 |
| 30 | + |
| 31 | +# ========设置docs文档为本地文件========= |
| 32 | +from fastapi.staticfiles import StaticFiles |
| 33 | +from fastapi.openapi.docs import ( |
| 34 | + get_redoc_html, |
| 35 | + get_swagger_ui_html, |
| 36 | + get_swagger_ui_oauth2_redirect_html, |
| 37 | +) |
| 38 | +app.mount("/static", StaticFiles(directory="static"), name="static") |
| 39 | + |
| 40 | + |
| 41 | + |
| 42 | +@app.get("/docs", include_in_schema=False) |
| 43 | +async def custom_swagger_ui_html(): |
| 44 | + return get_swagger_ui_html( |
| 45 | + openapi_url=app.openapi_url, |
| 46 | + title=app.title + " - Swagger UI", |
| 47 | + oauth2_redirect_url=app.swagger_ui_oauth2_redirect_url, |
| 48 | + swagger_js_url="/static/swagger-ui-bundle.js", |
| 49 | + swagger_css_url="/static/swagger-ui.css", |
| 50 | + ) |
| 51 | + |
| 52 | + |
| 53 | +@app.get(app.swagger_ui_oauth2_redirect_url, include_in_schema=False) |
| 54 | +async def swagger_ui_redirect(): |
| 55 | + return get_swagger_ui_oauth2_redirect_html() |
30 | 56 |
|
31 | 57 |
|
| 58 | +@app.get("/redoc", include_in_schema=False) |
| 59 | +async def redoc_html(): |
| 60 | + return get_redoc_html( |
| 61 | + openapi_url=app.openapi_url, |
| 62 | + title=app.title + " - ReDoc", |
| 63 | + redoc_js_url="/static/redoc.standalone.js", |
| 64 | + ) |
| 65 | +# ========设置docs文档为本地文件========= |
| 66 | + |
32 | 67 | # @app.exception_handler(StarletteHTTPException) # 重写HTTPException异常处理器 |
33 | 68 | # async def http_exception_handler(request, exc): |
34 | 69 | # """ |
|
41 | 76 | # |
42 | 77 |
|
43 | 78 |
|
44 | | -# @app.exception_handler(ValidationError) # 重写HTTPException异常处理器 |
45 | | -# async def http_exception_handler(request, exc): |
46 | | -# print("&&&"*90) |
47 | | -# """ |
48 | | -# :param request: 这个参数不能省 |
49 | | -# :param exc: |
50 | | -# :return: |
51 | | -# """ |
52 | | -# return PlainTextResponse(str(exc.detail), status_code=exc.status_code) |
53 | | -# 1.用户自定义异常类型,只要该类继承了Exception类即可 |
54 | | -@app.exception_handler(tutorial.chapter03.ValDtoError) |
55 | | -async def request_validation_exception_handler2(request: Request, exc: tutorial.chapter03.ValDtoError): |
56 | | - print(f"===>参数校验异常{request.method} {request.url}") |
57 | | - return JSONResponse({"message":exc.message}) |
58 | 79 | @app.exception_handler(RequestValidationError) # 重写请求验证异常处理器 |
59 | 80 | async def validation_exception_handler(request, exc): |
60 | 81 |
|
|
0 commit comments