title: Flask multi-tenant and app feature configuration
body: <p>I have an application that, with the same codebase, can be configured for two types of functionality. Depending on the configuration, some features are enabled or disabled. One such feature being if login is required for all routes. For example:</p>
  
  <pre><code>if app.config['IS_PRIVATE_INSTANCE'] :
      @app.before_request
      def check_logged_in():
          if request.endpoint and not request.endpoint == 'security.login' and not request.endpoint == 'security.logout' and not request.endpoint == 'security.forgot_password' and not request.endpoint == 'security.reset_password':
          return app.login_manager.unauthorized()
  </code></pre>
  
  <p>One version is intended for the general public that does not require login and has no use for some features. The other is a per-client private version with added functionality and access restricted to registered users.</p>
  
  <p>The general public version is intended to always be a single instance, but we currently have several instances of the private version (with their own DB's) running for different clients.</p>
  
  <p>The task is now to consolidate everything into a single multi-tenant application using subdomains. The wrinkle is the slightly different versions of the app.</p>
  
  <p>Whereas currently the config options (for public vs private, as well as a few per-instance ones) were being loaded from environment variables, now they need to be in a DB table and loaded according to the subdomain.</p>
  
  <p>I figure I could "flatten" the app config requirement into the subdomain multi-tenancy by having the <code>www</code> subdomain use the public configuration and any other subdomain use the private configuration, but I am just not sure how to do so so that I can freely know at any moment (without the need of a request) whether it is one or the other like I currently do using app config variables which exist outside app or request contexts.</p>
  
  <p>I've seen multiple strategies for achieving multi-tenancy in Flask using App Dispatching, Blueprints, or, in simpler cases, just setting the <code>SERVER_NAME</code> and using the <code>subdomain</code> route param, but they all seem to assume that the application is identical across all tenants. The latter works just fine for when this application is already configured and running and using the subdomain to only use the data of that subdomain, but for app level configuration it doesn't seem enough.</p>
  
  <p>Any ideas how to achieve multi-tenancy with different app config depending on the subdomain?</p>