forked from antonizdp/mall-2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
46 lines (36 loc) · 1.34 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from fastapi import FastAPI, applications
from tortoise import Tortoise
from apps.goods.search_es import to_es
from mall.conf import settings
from mall.exceptions import exception_handlers
from mall.urls import urlpatterns
# https://github.com/tiangolo/fastapi/issues/4924
def swagger_monkey_patch(*args, **kwargs):
"""
Wrap the function which is generating the HTML for the /docs endpoint and
overwrite the default values for the swagger js and css.
"""
return get_swagger_ui_html(
*args, **kwargs,
swagger_js_url="https://cdn.bootcdn.net/ajax/libs/swagger-ui/4.10.3/swagger-ui-bundle.js",
swagger_css_url="https://cdn.bootcdn.net/ajax/libs/swagger-ui/4.10.3/swagger-ui.css")
# Actual monkey patch
applications.get_swagger_ui_html = swagger_monkey_patch
async def init():
await Tortoise.init(
db_url=settings.MYSQL_DATABASE_URL, modules={"models": settings.APP_MODELS}
)
app = FastAPI(
title="美多商城",
description="""
- 使用FastAPI构建的美多商城项目
- 源项目使用Django, 构建参考视频https://www.bilibili.com/video/BV1ya411A7C8
""",
exception_handlers=exception_handlers,
routes=urlpatterns,
on_startup=[init, to_es],
on_shutdown=[Tortoise.close_connections],
)
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", reload=True)