3

I have two application factory functions - one creates the "customer" app, and the other creates the "admin" backend app. Both of the factory functions essentially do what is described here - create a flask app and register some extensions to it and then add some blueprints(with a url_prefix). I glue the two apps together via the create_combined_app() from below. It is the return value of that function which I register with my Flask-Script's Manager.

def create_combined_app(config_name):
    customer_app = create_customer_app(config_name)
    admin_app = create_admin_app(config_name)

    from werkzeug.wsgi import DispatcherMiddleware

    customer_app.wsgi_app = DispatcherMiddleware(customer_app.wsgi_app, {
        '/admin': admin_app
    })

    return customer_app

And then this is how I run it.

def make_me_an_app():
    return create_combined_app(config)

manager = Manager(make_me_an_app)
...

if __name__ == '__main__':
    manager.run()

I want to do some testing which involves getting all GET routes of my app and making sure they load. I followed the example from here, but I only see the urls of the customer app, and none of the urls from the admin backend.

@main.route("/site-map")
def site_map():
    from flask import current_app, jsonify
    links = []
    app = current_app
    for rule in app.url_map.iter_rules():
        if "GET" in rule.methods and has_no_empty_params(rule):
            url = url_for(rule.endpoint, **(rule.defaults or {}))
            links.append((url, rule.endpoint))
    return jsonify(links)

The admin backend works when I try to access it from the browser - it all works nicely, except that I don't see the admin's urls when I call /site-map.

Thanks! :)

Community
  • 1
  • 1
Georgi Tenev
  • 338
  • 4
  • 18

1 Answers1

0

I think DispatcherMiddleware create separate apps. Which mean you created customer_app and admin_app. Those 2 live as standalone. They don't know each others, therefor current_app is just the show customer_app.

Here is the describe from Flask http://flask.pocoo.org/docs/0.12/patterns/appdispatch/

enter image description here

Granit
  • 188
  • 1
  • 11