aio-libs / aiohttp-devtools

dev tools for aiohttp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cannot inject middlewares

mshahbazi opened this issue · comments

In an attempt to give aiohttp-devtools a try, I ran this command: aiohttp-runserver main.py run
(run is the app_factory)

opening the homepage, I get the following error:

[21:03:09] Starting dev server at http://localhost:8000 ●
[21:03:09] Starting aux server at http://localhost:8001 ◆
Error handling request
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/aiohttp/server.py", line 265, in start
    yield from self.handle_request(message, payload)
  File "/usr/local/lib/python3.5/dist-packages/aiohttp/web.py", line 96, in handle_request
    resp = yield from handler(request)
  File "/usr/local/lib/python3.5/dist-packages/aiohttp_session/__init__.py", line 129, in middleware
    response = yield from handler(request)
  File "/usr/mms/middlewares.py", line 35, in middleware
    request.db = app.db
AttributeError: 'Application' object has no attribute 'db'

the app_factory is as follows:

def run(loop):
	app = aiohttp.web.Application(loop=loop)
	app.on_startup.append(middlewares.init_pg)
	app.on_cleanup.append(middlewares.close_pg)
	return app

and eventually init_pg is:

async def init_pg(app):
	engine = await aiopg.sa.create_engine(
		database=config.DB_NAME,
		user=config.DB_USER,
		password=config.DB_PASS,
		host=config.DB_HOST,
		port=config.DB_PORT,
		loop=app.loop)
	app.db = engine

the problem is that runserver does not call this function (init_pg)

OS: Ubuntu 16.04
Python: 3.5.2

any help would be greatly appreciated

I assume you're using the version released on pypi?

I believe this should be fixed on master. Trying install direct from github with

pip install git+https://github.com/samuelcolvin/aiohttp-devtools.git

and try again.

Otherwise I'm going resolve the conflicts with aiohttp 1.1.5 and release a new version as soon as I can.

it does see here

I'm not sure if this is the problem, but generally you should use

app['db'] = await aiopg.sa.create_engine(...)

rather than

app.db = await aiopg.sa.create_engine(...)

see here for an example.

Does that help?

I suspect you still have the old version of the package installed. let me release an update and they try to install again.

I've let you know when it's released.

I followed your instruction and installed the git package
it is aiohttp-devtools-0.0.2 now
btw, I look forward to your update

thanks

ok, new version released try pip install aiohttp-devtools==0.0.3 and let me know how you get on.

I've just checked now and on_startup is working fine.

Please try this simple example

demo.py

from aiohttp import web

async def hello(request):
    return web.Response(text="Hello, world {}".format(request.app['value']))

async def start_background_tasks(app):
    print('this is the background task starting')
    app['value'] = 42

def app_factory(loop):
    app = web.Application(loop=loop)
    app.router.add_get('/', hello)
    app.on_startup.append(start_background_tasks)
    return app

To run (on ubuntu 16.04):

cd ~
mkdir aiotest && cd aiotest
virtualenv -p /usr/bin/python3.5 env
source env/bin/active
pip install aiohttp-devtools
<your-editor> demo.py # copy and paste the above python and save
adev runserver demo.py

You should see this is the background task starting in the output and Hello, world 42 at localhost:8000.

how can I run it through pycharm?

I've been using pycharm all day, everyday for the last 3 years. I used it's debug tools on day one and never again since. You're on your own with that.

BTW, pleased you got it working in the end. Well done.