0

In flask I know it can use subdomain, but I want to set some different subdomain in same routing. Is there a way we can use regular expressions in subdomain just like in its URL routing?like:

@app.route('/', subdomain='<regex("www|test"):subdomain>')
def example(subdomain):
    return "{}".format(subdomain)

if __name__ == '__main__':
    app.run(debug=True)

But there is new question, if I don't want the argument like this one, what should I do?

@app.route('/', subdomain='<regex("www|test"):subdomain>')
def example():
    return "test"

if __name__ == '__main__':
    app.run(debug=True)
L.GuangTian
  • 51
  • 1
  • 4

1 Answers1

0

I found the werkzeug use the same way to parse domain_url and URL path in here!

so I can implement it by this:

from flask import Flask
from werkzeug.routing import BaseConverter

app = Flask(__name__)


class Config(object):
    DEBUG = False
    TESTING = False
    SQLALCHEMY_TRACK_MODIFICATIONS = True


class DevelopmentConfig(Config):
    DEBUG = True
    SERVER_NAME = 'local.dev:9090'


class RegexConverter(BaseConverter):
    def __init__(self, url_map, *items):
        super(RegexConverter, self).__init__(url_map)
        self.regex = items[0]


app.url_map.converters['regex'] = RegexConverter
app.config.from_object(DevelopmentConfig)

@app.route('/', subdomain='<regex("www|test"):subdomain>')
def example(subdomain):
    return "{}".format(subdomain)

if __name__ == '__main__':
    app.run(debug=True)

and if you want to test in local, you maybe edit your host like:

127.0.0.1       www.local.dev
127.0.0.1       local.dev
127.0.0.1       test.local.dev
L.GuangTian
  • 51
  • 1
  • 4