Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup different subdomains in Flask (using blueprints)?

I have a Flask application running at https://app.mydomain.com.

The blueprints look like this:

app.register_blueprint(main)
app.register_blueprint(account, url_prefix='/account')
app.register_blueprint(users, url_prefix='/users')
app.register_blueprint(boxes, url_prefix='/boxes')
app.register_blueprint(api_1_0, url_prefix='/api/v1.0')

The URLs look like this:

  • https://app.mydomain.com
  • https://app.mydomain.com/account
  • https://app.mydomain.com/users
  • ...

I want to move the api_1_0 route from https://app.mydomain.com/api/v1.0 to https://api.mydomain.com, how should I modify the routes and how should I set app.config['SERVER_NAME']?

example.com (without any subdomain) is another site entirely, otherwise I would get rid of the app subdomain.

So, I want app to be the default subdomain for all blueprints except api_1_0 which should be api.

like image 225
okoboko Avatar asked Jan 13 '15 04:01

okoboko


Video Answer


1 Answers

Since you want your Flask application to handle multiple subdomains, you should set app.config['SERVER_NAME'] to the root domain. Then apply app as the default subdomain and overriding it in api blueprint registration.

The way to do this would be something like that I suppose:

app.config['SERVER_NAME'] = 'mydomain.com'
app.url_map.default_subdomain = "app"
app.register_blueprint(account, url_prefix='/account')
app.register_blueprint(users, url_prefix='/users')
app.register_blueprint(boxes, url_prefix='/boxes')
app.register_blueprint(api_1_0, subdomain='api')
like image 107
Benoît Latinier Avatar answered Oct 17 '22 16:10

Benoît Latinier