From 977c7c38ff4196f43c4784308d81fd7e6a471511 Mon Sep 17 00:00:00 2001 From: syn Date: Mon, 16 Mar 2020 22:17:48 +0300 Subject: Init commit --- app/request_routine.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 app/request_routine.py (limited to 'app/request_routine.py') 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 -- cgit v1.2.1-18-gbd029