Skip to content

Commit d3f77cd

Browse files
committed
设置docs文件为本地文件,不再访问网络
1 parent bf87b56 commit d3f77cd

File tree

6 files changed

+1830
-21
lines changed

6 files changed

+1830
-21
lines changed

coronavirus/schemas.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class ReadData(CreateData):
2929
created_at: datetime
3030

3131
class Config:
32-
orm_mode = True
32+
from_attributes = True
3333

3434

3535
class ReadCity(CreateCity):
@@ -38,4 +38,4 @@ class ReadCity(CreateCity):
3838
created_at: datetime
3939

4040
class Config:
41-
orm_mode = True
41+
from_attributes = True

run.py

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,49 @@
2121
title='FastAPI Tutorial and Coronavirus Tracker API Docs',
2222
description='FastAPI教程 新冠病毒疫情跟踪器API接口文档,项目代码:https://github.com/liaogx/fastapi-tutorial',
2323
version='1.0.0',
24-
docs_url='/docs',
25-
redoc_url='/redocs',
24+
docs_url=None,
25+
redoc_url=None,
2626
)
2727

2828
# 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()
3056

3157

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+
3267
# @app.exception_handler(StarletteHTTPException) # 重写HTTPException异常处理器
3368
# async def http_exception_handler(request, exc):
3469
# """
@@ -41,20 +76,6 @@
4176
#
4277

4378

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})
5879
@app.exception_handler(RequestValidationError) # 重写请求验证异常处理器
5980
async def validation_exception_handler(request, exc):
6081

static/redoc.standalone.js

Lines changed: 1782 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/swagger-ui-bundle.js

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

static/swagger-ui.css

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tutorial/chapter03.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def __str__(self):
7777

7878
@app03.get("/query/validations") # 长度+正则表达式验证,比如长度8-16位,以a开头。其它校验方法看Query类的源码
7979
def query_params_validate(
80-
value: str = Query(..., min_length=8, max_length=16, regex="^a"), # ...换成None就变成选填的参数
80+
value: str = Query(..., min_length=8, max_length=16, pattern="^a"), # ...换成None就变成选填的参数
8181
values: List[str] = Query(["v1", "v2"], alias="alias_name")
8282
): # 多个查询参数的列表。参数别名
8383
return value, values
@@ -111,7 +111,7 @@ def country(cls, v):
111111
raise ValueError('country必须包含空格')
112112
return v
113113
class Config:
114-
schema_extra = {
114+
json_schema_extra = {
115115
"example": {
116116
"name": "Shanghai",
117117
"country": "China",

0 commit comments

Comments
 (0)