diff options
author | syn <isaqtm@gmail.com> | 2020-03-16 22:17:48 +0300 |
---|---|---|
committer | syn <isaqtm@gmail.com> | 2020-03-16 22:17:48 +0300 |
commit | 977c7c38ff4196f43c4784308d81fd7e6a471511 (patch) | |
tree | 3e53843c181a60ec7d2b3b1d4dfdf4868859c3a7 /app/request_routine.py | |
download | blure-977c7c38ff4196f43c4784308d81fd7e6a471511.tar.gz |
Init commit
Diffstat (limited to 'app/request_routine.py')
-rw-r--r-- | app/request_routine.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/app/request_routine.py b/app/request_routine.py new file mode 100644 index 0000000..821272f --- /dev/null +++ b/app/request_routine.py @@ -0,0 +1,26 @@ +from functools import wraps +from collections import namedtuple +from app import blure as app + +Context = namedtuple('Context', [ + 'r', # request + 'pg', # pg connection + 'app' # r.app +]) + + +def db_route(route, pool=True, **route_kwargs): + def proxy_wrapper(f): + @app.route(route, **route_kwargs) + @wraps(f) + async def wrapper(request, *args, **kwargs): + if not pool: + ctx = Context(r=request, pg=None, app=request.app) + response = await f(ctx, *args, **kwargs) + else: + async with app.pool.acquire() as conn: + ctx = Context(r=request, pg=conn, app=request.app) + response = await f(ctx, *args, **kwargs) + return response + return wrapper + return proxy_wrapper |