__init__.py 2.35 KB
Newer Older
1
"""Initialize Fractalis Flask app and configure it."""
Sascha Herzinger's avatar
Sascha Herzinger committed
2

3
4
import logging.config
import yaml
Sascha Herzinger's avatar
Sascha Herzinger committed
5
from flask import Flask
Sascha Herzinger's avatar
Sascha Herzinger committed
6
from flask_cors import CORS
7
from flask_request_id import RequestID
8
from flask_compress import Compress
9
from redis import StrictRedis
10

11
12
from fractalis.session import RedisSessionInterface

Sascha Herzinger's avatar
a lot    
Sascha Herzinger committed
13
app = Flask(__name__)
14

15
# Configure app with defaults
Sascha Herzinger's avatar
a lot    
Sascha Herzinger committed
16
app.config.from_object('fractalis.config')
17
# Configure app with manually settings
Sascha Herzinger's avatar
Sascha Herzinger committed
18
19
try:
    app.config.from_envvar('FRACTALIS_CONFIG')
20
    default_config = False
Sascha Herzinger's avatar
Sascha Herzinger committed
21
except RuntimeError:
22
23
24
25
    default_config = True
    pass

# setup logging
26
with open(app.config['FRACTALIS_LOG_CONFIG'], 'rt') as f:
27
28
29
30
31
32
    log_config = yaml.safe_load(f.read())
logging.config.dictConfig(log_config)
log = logging.getLogger(__name__)

# we can't log this earlier because the logger depends on the loaded app config
if default_config:
33
34
    log.warning("Environment Variable FRACTALIS_CONFIG not set. Falling back "
                "to default settings. This is not a good idea in production!")
35

36
37
38
# Plugin that assigns every request an id
RequestID(app)

39
40
41
# Plugin that compresses all responses
Compress(app)

42
# create a redis instance
43
log.info("Creating Redis connection.")
44
redis = StrictRedis(host=app.config['REDIS_HOST'],
45
46
47
                    port=app.config['REDIS_PORT'],
                    charset='utf-8',
                    decode_responses=True)
48

49
# Set new session interface for app
50
log.info("Replacing default session interface.")
51
app.session_interface = RedisSessionInterface(redis, app)
Sascha Herzinger's avatar
Sascha Herzinger committed
52

53
54
55
56
57
58
# allow everyone to submit requests
log.info("Setting up CORS.")
CORS(app, supports_credentials=True)

# create celery app
log.info("Creating celery app.")
59
60
61
62
from fractalis.celeryapp import make_celery, register_tasks  # noqa
celery = make_celery(app)

# register blueprints
Sascha Herzinger's avatar
Sascha Herzinger committed
63
from fractalis.analytics.controller import analytics_blueprint  # noqa
64
from fractalis.data.controller import data_blueprint  # noqa
65
log.info("Registering Flask blueprints.")
Sascha Herzinger's avatar
a lot    
Sascha Herzinger committed
66
app.register_blueprint(analytics_blueprint, url_prefix='/analytics')
67
app.register_blueprint(data_blueprint, url_prefix='/data')
68

69
# registering all application celery tasks
70
log.info("Registering celery tasks.")
71
register_tasks()
72

73
74
log.info("Initialisation of service complete.")

Sascha Herzinger's avatar
a lot    
Sascha Herzinger committed
75
if __name__ == '__main__':
76
    log.info("Starting builtin web server.")
Sascha Herzinger's avatar
Sascha Herzinger committed
77
    app.run(host='0.0.0.0', port=5000)
78
    log.info("Builtin web server started.")