title: Flask and SubdomainDispatcher, how the application is run at first?
body: <p>I'm building a website using Flask, and I'd like to use the indicated <a href="http://flask.pocoo.org/docs/patterns/appdispatch/" rel="nofollow noreferrer">SubdomainDispatcher</a>, but I don't know how to make it work (it's missing the initial <code>run</code> to start the server).</p>
  
  <p>Here's the code :</p>
  
  <p><strong>The create_app function given <a href="http://flask.pocoo.org/snippets/20/" rel="nofollow noreferrer">here</a> :</strong></p>
  
  <pre><code>def create_app(database_uri, debug=False):
      app = Flask(__name__)
      app.debug = debug
  
      # set up your database
      app.engine = create_engine(database_uri)
  
      # add your modules
      app.register_module(frontend)
  
      # other setup tasks
  
      return app
  </code></pre>
  
  <p><strong>The SubdomainDispatcher :</strong></p>
  
  <pre><code>class SubdomainDispatcher(object):
  
      def __init__(self, domain, create_app):
          self.domain = domain
          self.create_app = create_app
          self.lock = Lock()
          self.instances = {}
  
      def get_application(self, host):
          host = host.split(':')[0]
          assert host.endswith(self.domain), 'Configuration error'
          subdomain = host[:-len(self.domain)].rstrip('.')
          with self.lock:
              app = self.instances.get(subdomain)
              if app is None:
                  app = self.create_app(subdomain)
                  self.instances[subdomain] = app
              return app
  
      def __call__(self, environ, start_response):
          app = self.get_application(environ['HTTP_HOST'])
          return app(environ, start_response)
  </code></pre>
  
  <p><strong>The main app :</strong></p>
  
  <pre><code>def make_app(subdomain):
      user = get_user_for_subdomain(subdomain)
      if user is None:
          # if there is no user for that subdomain we still have
          # to return a WSGI application that handles that request.
          # We can then just return the NotFound() exception as
          # application which will render a default 404 page.
          # You might also redirect the user to the main page then
          return NotFound()
  
      # otherwise create the application for the specific user
      return create_app(user)
  
  application = SubdomainDispatcher('example.com', make_app)
  </code></pre>
  
  <p>So far, my code works without errors, but then, it stops. This is normal because no where, there is the :</p>
  
  <pre><code>if __name__ == "__main__":
      application = create_app(config.DATABASE_URI, debug=True)
      application.run()
  </code></pre>
  
  <p>code that make the initial server to be run.</p>
  
  <p>I tried this :</p>
  
  <pre><code>if __name__ == "__main__":
      application = SubdomainDispatcher('example.com', make_app)
      application.run()
  </code></pre>
  
  <p>But it fails with :</p>
  
  <blockquote>
    <p>AttributeError: 'SubdomainDispatcher' object has no attribute 'run'</p>
  </blockquote>
  
  <p>How can I run it using the SubdomainDispatcher ?</p>