copyninja copyninja - 4 months ago 40
Python Question

Combining 2 Flask apps using DispatcherMiddleware instance results in 404 for second app

Sorry for ambiguous question, I'm not sure how better I can put it. So let me do the explanation of my problem.

I've the Flask application libindic, which has 2 Flask application one is frontend and other is api. So I've as follows

from werkzeug.serving import run_simple
from werkzeug.wsgi import DispatcherMiddleware

from silpa import api, frontend
import os

conffile = os.path.join(os.path.dirname(__file__), "etc", "silpa.conf")

application = DispatcherMiddleware(frontend.create_app(conffile),
{'/api': api.create_app(conffile)})

if __name__ == "__main__":
run_simple('', 5000, application,
use_reloader=True, use_debugger=True)

The front end access works properly but when I hit /api/JSONRPC I get 404 error returned. This rule is defined as follows in silpa/api/

bp = Blueprint('api_jsonrpc', __name__, url_prefix='/api')

@route(bp, '/JSONRPC', methods=['POST'])
def handle_jsonrpc_call():

And when I print value of application and and application.mounts in python interpreter I see folllowing

<Flask 'silpa.frontend'>
>>> application.mounts
{'/api': <Flask 'silpa.api'>}

I'm unable to figure out why /api/JSONRPC results in 404. I'm not sure how can I debug it. I did check the app.url_map for api application and I can see rule for /api/JSONRPC registered there.

If some one can tell me what I might be doing wrong it would be a great help.


OK after debugging and stepping through Flask code I figured out the reason for the problem. The following line actually caused the problem

bp = Blueprint('api_jsonrpc', __name__, url_prefix='/api')

url_prefix should not be present in code because I'm already mounting this app at /api. Adding a /api url_prefix will result in following url instead /api/api/JSONRPC. Removing the above line fixed the issue.

So if you are mouting your app at different mount point than / using DispatcherMiddleware you should not url_prefix in blueprint.