def build_app(sub_port=5558, **kwargs): """Build the flask app.""" global app global message_data global cache logger.info("Starting threedi-wms...") logger.info("Subscription port: %d (server should publish on this port)." % sub_port) # use the correct subgrid_id subgrid_id = utils.fetch_subgrid_id() # add a ":" to the subgrid_id for uniformity cache_key_prefix = "{0}:".format(subgrid_id) logger.info( "Got subgrid_id: %s." % subgrid_id, extra={'subgrid_id': subgrid_id}) app = flask.Flask(__name__) if config.USE_CACHE: cache_config = { 'CACHE_TYPE': 'redis', 'CACHE_KEY_PREFIX': cache_key_prefix, 'CACHE_REDIS_HOST': config.REDIS_HOST_CACHE, 'CACHE_REDIS_PORT': config.REDIS_PORT, 'CACHE_REDIS_DB': config.REDIS_DB_THREEDI_WMS_CACHE, } else: cache_config = {'CACHE_TYPE': 'null', } cache = Cache(app, config=cache_config) # reset state variables logger.info("Reset wms state variables in redis.") reporter.reset_all() # setup sentry if hasattr(config, 'SENTRY_DSN'): app.config['SENTRY_DSN'] = config.SENTRY_DSN Sentry(app, dsn=config.SENTRY_DSN, logging=True, level=logging.ERROR) # this one is global because we only have one event loop that receives # messages message_data = MessageData(sub_port=sub_port) # register the blueprints for blueprint in blueprints.get_blueprints(): url_prefix = '/' + blueprint.name app.register_blueprint(blueprint, url_prefix=url_prefix) logger.info("Ready to rock and roll!", extra={'subgrid_id': subgrid_id}) return app
# -*- coding: utf-8 -*- # (c) Nelen & Schuurmans. GPL licensed, see LICENSE.rst. from __future__ import print_function from __future__ import unicode_literals from __future__ import absolute_import from __future__ import division from server import blueprints from server import loghelper import flask # App app = flask.Flask(__name__) # Register the blueprints for blueprint in blueprints.get_blueprints(): url_prefix = '/' + blueprint.name app.register_blueprint(blueprint, url_prefix=url_prefix) # Setup logging loghelper.setup_logging(logfile_name='server.log') # Main def run(): app.run(host='0.0.0.0', debug=True)
import celery from server import blueprints from server import config """ This module is only used by the celery worker, and not by the server. Every blueprint can have it's own celery app, which must connect to the same broker as this worker module for it's tasks to be executed by the servers worker. """ # vvv Fix for celery forking problem os.environ['PYTHONPATH'] = ':'.join(sys.path) # Autocreate celery db dir try: os.makedirs(os.path.dirname(config.CELERY_DB)) except OSError: pass # No problem. # Configure celery app = celery.Celery() app.conf.update( BROKER_URL='sqla+sqlite:///{}'.format(config.CELERY_DB), ) # Import the blueprints, any tasks in them get registered with celery. blueprints.get_blueprints()