app.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env python
  2. """
  3. @Contact : liuyuqi.gov@msn.cn
  4. @Time : 2024/03/19 16:50:59
  5. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  6. @Desc :
  7. """
  8. from app.config import settings
  9. from app.extensions.hook import (init_banner, init_middlewares, register_db,
  10. register_exception_handlers, register_routes)
  11. from fastapi import FastAPI
  12. from fastapi.routing import APIRoute
  13. def custom_generate_unique_id(route: APIRoute) -> str:
  14. return f"{route.tags[0]}-{route.name}"
  15. def create_app(config_name="dev") -> FastAPI:
  16. """
  17. return FastAPI instance
  18. """
  19. fastapp = FastAPI(
  20. title=settings.PROJECT_NAME,
  21. description=settings.PROJECT_NAME,
  22. # version= settings.API_VERSION,
  23. openapi_url=f"{settings.API_V1_STR}/openapi.json",
  24. docs_url="/docs",
  25. redoc_url="/redoc",
  26. generate_unique_id_function=custom_generate_unique_id,
  27. )
  28. init_banner()
  29. init_middlewares(app=fastapp)
  30. register_db(app=fastapp)
  31. register_exception_handlers(app=fastapp)
  32. register_routes(app=fastapp)
  33. return fastapp
  34. fastapp = create_app()