How to create dynamic subdomains in a Flask application

后端 未结 2 750
野性不改
野性不改 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: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('/', 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.

提交回复
热议问题