Skip to content

Commit

Permalink
Merge pull request #440 from mDevv/patch-1
Browse files Browse the repository at this point in the history
Add custom config parameter to aiohttp middleware
  • Loading branch information
wwwjfy committed Feb 14, 2019
2 parents b97a106 + 73ca4e9 commit 5582c95
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions gino/ext/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Gino(_Gino):
db.init_app(app)
By :meth:`init_app` GINO subscribes to a few signals on aiohttp, so that
GINO could use database configuration in aiohttp ``config.gino`` to
GINO could use database configuration in aiohttp ``app['config'].gino`` to
initialize the bound engine. The config includes:
* ``driver`` - the database driver, default is ``asyncpg``.
Expand All @@ -86,6 +86,10 @@ class Gino(_Gino):
* ``kwargs`` - other parameters passed to the specified dialects,
like ``asyncpg``. Unrecognized parameters will cause exceptions.
If you would like to use a custom configuration dictionary, you can do that
by passing it as ``config=`` argument in ``app_init`` call. Remember to use
the keys described above.
If the ``db`` is set as an aiohttp middleware, then a lazy connection is
available at ``request['connection']``. By default, a database connection
is borrowed on the first query, shared in the same execution context, and
Expand All @@ -109,10 +113,13 @@ async def _middleware(self, request, handler):
finally:
request.pop('connection', None)

def init_app(self, app):
def init_app(self, app, config=None):
app['db'] = self

config = app['config'].get('gino', {})
if not isinstance(config, dict):
config = app['config'].get('gino', {})
else:
config = config.copy()

async def before_server_start(app_):
if config.get('dsn'):
Expand Down

0 comments on commit 5582c95

Please sign in to comment.