summaryrefslogtreecommitdiffstats
path: root/app/request_routine.py
blob: 821272f99700bdd450c3d3449f3b70d1674d154d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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