__init__.py 736 B

12345678910111213141516171819202122232425
  1. from fastapi import FastAPI
  2. from tortoise.contrib.fastapi import register_tortoise
  3. def create_app() -> FastAPI:
  4. app = FastAPI()
  5. register_rotuer(app)
  6. register_db(app)
  7. return app
  8. def register_rotuer(app: FastAPI) -> None:
  9. """注册路由"""
  10. from app.api import routers
  11. app.include_router(routers)
  12. def register_db(app: FastAPI) -> None:
  13. """"初始化数据库并根据数据模型生成对应的表"""
  14. register_tortoise(
  15. app,
  16. db_url="mysql://choozmo:pAssw0rd@db.ptt.cx:3306/test",
  17. modules={"models": ["app.models.models"]},# 这是一个列表用来指明我们的模型文件的路径
  18. generate_schemas=True,
  19. add_exception_handlers=True)