ashleysommer/sanic-dispatcher

Can't dispatch a CORS-enabled Sanic application

Closed this issue · 3 comments

Hi,

When using the sanic-cors package (sanic-cors==0.9.8, sanic_dispatcher==0.6.0.0, Sanic==19.3.1, python 3.6) and this piece of code:

from sanic import Sanic
from sanic.response import text
from sanic_cors import CORS
from sanic_dispatcher import SanicDispatcherMiddlewareController

main_app = Sanic(__name__)
dispatcher = SanicDispatcherMiddlewareController(main_app)


@main_app.get('/')
def hello_main(request):
    """Route main."""
    return text('hello, main world !')


child_app = Sanic(__name__+'__')
CORS(child_app)


@child_app.route("/", methods=['GET', 'OPTIONS'])
def hello_world(request):
    """Cors route."""
    return text("Hello, cross-origin-world!")


dispatcher.register_sanic_application(child_app, '/cors', apply_middleware=True)

if __name__ == '__main__':
    main_app.run(port=8001, debug=True)

When querying /cors, I get an error message :

[2019-06-17 14:52:30 +0200] - (sanic.access)[INFO][127.0.0.1:38858]: GET http://localhost:8001/favicon.ico  404 43
[2019-06-17 14:52:35 +0200] [30223] [ERROR] Exception occurred while handling uri: 'http://localhost:8001'
Traceback (most recent call last):
  File "/home/.../.virtualenvs/.../lib/python3.6/site-packages/sanic/app.py", line 885, in handle_request
    response = await self._run_request_middleware(request)
  File "/usr/lib/python3.6/asyncio/coroutines.py", line 110, in __next__
    return self.gen.send(None)
  File "/home/.../.virtualenvs/.../lib/python3.6/site-packages/spf/framework.py", line 497, in _run_request_middleware
    "App must be running before you can run middleware!"
AssertionError: App must be running before you can run middleware!
[2019-06-17 14:52:35 +0200] [30223] [DEBUG] CORS: Request to '' matches CORS resource '/*'. Using options: {'origins': ['.*'], 'methods': 'DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT', 'allow_headers': ['.*'], 'expose_headers': None, 'supports_credentials': False, 'max_age': None, 'send_wildcard': False, 'automatic_options': False, 'vary_header': True, 'resources': '/*', 'intercept_exceptions': True, 'always_send': True}
[2019-06-17 14:52:35 +0200] [30223] [DEBUG] CORS: Cannot find the request context. Is request already finished?

Any way to fix this ? I know that this package hasn't been maintained for a long time - do you have any other option to dispatch multiple sanic apps or fix this issue ?

Thanks a lot !

I looked a bit into the code and I guess it could be fixed by triggering the before_start & after_start events (at least). It works on that simple example for me at least :)
I edited a forked version here : https://github.com/amaurymercier/sanic-dispatcher/pull/1

Hi @amaurymercier
Thanks for this issue.
Looks like the same problem as in #8

The current version of Sanic-Dispatcher hasn't been updated in quite a while, and probably needs some much overdue fixes.

@amaurymercier
I've pushed a new version of Sanic-Dispatcher, updated with better compatibility with newer sanic versions, as well as adding in the event listener compatibility hooks.
It is v 0.7.0.0 and should be out on PyPI soon.