-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #252 from prefeitura-rio/development
Lançamento da Versão 2.2
- Loading branch information
Showing
18 changed files
with
333 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# -*- coding: utf-8 -*- | ||
from contextlib import AbstractAsyncContextManager, asynccontextmanager | ||
from types import ModuleType | ||
from typing import Dict, Iterable, Optional, Union | ||
from contextlib import asynccontextmanager | ||
|
||
import redis.asyncio as redis | ||
from fastapi import FastAPI, Request | ||
from fastapi.responses import JSONResponse | ||
from fastapi_limiter import FastAPILimiter | ||
|
||
from tortoise import Tortoise, connections | ||
from tortoise.exceptions import DoesNotExist, IntegrityError | ||
from tortoise.log import logger | ||
|
||
from app.db import TORTOISE_ORM | ||
from app.config import ( | ||
REDIS_HOST, | ||
REDIS_PASSWORD, | ||
REDIS_PORT, | ||
) | ||
from app.utils import request_limiter_identifier | ||
|
||
|
||
|
||
def register_tortoise( | ||
app: FastAPI, | ||
config: Optional[dict] = None, | ||
config_file: Optional[str] = None, | ||
db_url: Optional[str] = None, | ||
modules: Optional[Dict[str, Iterable[Union[str, ModuleType]]]] = None, | ||
generate_schemas: bool = False, | ||
add_exception_handlers: bool = False, | ||
) -> AbstractAsyncContextManager: | ||
async def init_orm() -> None: # pylint: disable=W0612 | ||
await Tortoise.init(config=config, config_file=config_file, db_url=db_url, modules=modules) | ||
logger.info("Tortoise-ORM started, %s, %s", connections._get_storage(), Tortoise.apps) | ||
if generate_schemas: | ||
logger.info("Tortoise-ORM generating schema") | ||
await Tortoise.generate_schemas() | ||
|
||
async def close_orm() -> None: # pylint: disable=W0612 | ||
await connections.close_all() | ||
logger.info("Tortoise-ORM shutdown") | ||
|
||
class Manager(AbstractAsyncContextManager): | ||
async def __aenter__(self) -> "Manager": | ||
await init_orm() | ||
return self | ||
|
||
async def __aexit__(self, *args, **kwargs) -> None: | ||
await close_orm() | ||
|
||
if add_exception_handlers: | ||
|
||
@app.exception_handler(DoesNotExist) | ||
async def doesnotexist_exception_handler(request: Request, exc: DoesNotExist): | ||
return JSONResponse(status_code=404, content={"detail": str(exc)}) | ||
|
||
@app.exception_handler(IntegrityError) | ||
async def integrityerror_exception_handler(request: Request, exc: IntegrityError): | ||
return JSONResponse( | ||
status_code=422, | ||
content={"detail": [{"loc": [], "msg": str(exc), "type": "IntegrityError"}]}, | ||
) | ||
|
||
return Manager() | ||
|
||
|
||
@asynccontextmanager | ||
async def api_lifespan(app: FastAPI): | ||
# do sth before db inited | ||
redis_connection = redis.from_url( | ||
f"redis://:{REDIS_PASSWORD}@{REDIS_HOST}:{REDIS_PORT}", | ||
encoding="utf8" | ||
) | ||
await FastAPILimiter.init( | ||
redis=redis_connection, | ||
identifier=request_limiter_identifier, | ||
) | ||
|
||
async with register_tortoise( | ||
app, | ||
config=TORTOISE_ORM, | ||
generate_schemas=False, | ||
add_exception_handlers=True, | ||
): | ||
# do sth while db connected | ||
yield | ||
|
||
# do sth after db closed | ||
await FastAPILimiter.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.