How to implement Flask Application Dispatching by Path with WSGI?

后端 未结 2 1048
一整个雨季
一整个雨季 2021-02-03 13:43

I would like to use a single domain as a Staging Environment for multiple flask applications that will eventually run on their own domains.

Something like:

相关标签:
2条回答
  • 2021-02-03 14:09

    This worked for me:

    Folder structure

    DISPATCHER (folder)
       dispatcher.py
    
       app1 (folder)
           __init__.py
    
       app2 (folder)
           __init__.py
    
       app3 (folder)
           __init__.py
    

    dispatcher.py

    from flask import Flask
    from werkzeug.wsgi import DispatcherMiddleware
    from werkzeug.exceptions import NotFound
    
    from app1 import app as app1
    from app2 import app as app2
    from app3 import app as app3
    
    app = Flask(__name__)
    
    app.wsgi_app = DispatcherMiddleware(NotFound(), {
        "/app1": app1,
        '/app2': app2,
        '/app3': app3
    })
    
    if __name__ == "__main__":
        app.run()
    

    app1 to app3 __init__.py

    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route("/")
    def index_one():
        return "Hi im 1 or 2 or 3"
    
    if __name__ == "__main__":
        app.run()
    

    Working

    python app.py
    
    localhost:5000/app1 "Hi im one"
    localhost:5000/app2 "Hi im two"
    localhost:5000/app3 "Hi im three"
    

    Another configuratiom

    You can import another app, like app0 and add a menu to the apps, changing this with NotFound()

    This helped

    Application Dispatching

    0 讨论(0)
  • 2021-02-03 14:18

    The key thing to note, here, is that you'll actually have 4 apps (3 individual apps and one combined app). This is ignoring the staging/live distinction because staging and live are just copies of each other in different directories.

    Create each of the individual apps and get them responding on their individual domains. Then create a new app that imports the application variables from the individual apps and combines them using the DispatcherMiddleware like the example under the heading "Combining Applications" on the doc page you link to.

    0 讨论(0)
提交回复
热议问题