How to create dynamic subdomains in a Flask application

后端 未结 2 723
野性不改
野性不改 2021-01-15 10:36

I am trying to setup variable route handling in a Flask application such as described in this answer: Dynamic Subdomain Handling in a Web App (Flask)

However, I want

相关标签:
2条回答
  • 2021-01-15 10:38

    To keep it simple, I redesigned the logic of my application into two distinct parts.

    This way the Flask application only handles the API endpoint logic. The user profile logic is handled by another application. I can now add multiple Resources to the API application without worry about breaking the routing.

    0 讨论(0)
  • 2021-01-15 10:49
    @app.before_request
    def before_request():
        if 'api' == request.host[:-len(app.config['SERVER_NAME'])].rstrip('.'):
            redirect(url_for('api'))
    
    
    @app.route('/', defaults={'path': ''}, subdomain='api')
    @app.route('/<path:path>', subdomain='api')
    def api(path):
        return "hello"
    

    This should work. Add your api version to the path if needed or that could be processed by your API class.

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