__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
5

Modules in this package:
    - config -- Manages Fractalis Flask app configuration
"""
6
import logging.config
Sascha Herzinger's avatar
Sascha Herzinger committed
7

8
import os
9
import yaml
Sascha Herzinger's avatar
Sascha Herzinger committed
10
from flask import Flask
Sascha Herzinger's avatar
Sascha Herzinger committed
11
from flask_cors import CORS
12
from flask_request_id import RequestID
13
from redis import StrictRedis
14

15
16
from fractalis.session import RedisSessionInterface

Sascha Herzinger's avatar
a lot    
Sascha Herzinger committed
17
app = Flask(__name__)
18

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

# setup logging
30
with open(os.path.join(os.path.dirname(__file__), 'logging.yaml'), 'rt') as f:
31
32
33
34
35
36
    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:
37
38
    log.warning("Environment Variable FRACTALIS_CONFIG not set. Falling back "
                "to default settings. This is not a good idea in production!")
39

40
41
42
# Plugin that assigns every request an id
RequestID(app)

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

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

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

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

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

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

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

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