This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
app.py
130 lines (101 loc) · 3.77 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env python
# -*- coding: utf-8; -*-
#
# This file is part of Superdesk.
#
# Copyright 2013, 2014, 2015 Sourcefabric z.u. and contributors.
#
# For the full copyright and license information, please see the
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license
import os
import logging
import importlib
import jinja2
import eve
import settings
import superdesk
from flask.ext.mail import Mail
from eve.io.mongo import MongoJSONEncoder
from eve.render import send_response
from superdesk.celery_app import init_celery
from eve.auth import TokenAuth
from superdesk.storage.desk_media_storage import SuperdeskGridFSMediaStorage
from superdesk.validator import SuperdeskValidator
from raven.contrib.flask import Sentry
from superdesk.errors import SuperdeskError, SuperdeskApiError
logger = logging.getLogger('superdesk')
sentry = Sentry(register_signal=False, wrap_wsgi=False)
def get_app(config=None):
"""App factory.
:param config: configuration that can override config from `settings.py`
:return: a new SuperdeskEve app instance
"""
if config is None:
config = {}
config['APP_ABSPATH'] = os.path.abspath(os.path.dirname(__file__))
config['CONTENT_STATE'] = 'state'
for key in dir(settings):
if key.isupper():
config.setdefault(key, getattr(settings, key))
media_storage = SuperdeskGridFSMediaStorage
if config['AMAZON_CONTAINER_NAME']:
from superdesk.storage.amazon.amazon_media_storage import AmazonMediaStorage
from superdesk.storage.amazon.import_from_amazon import ImportFromAmazonCommand
media_storage = AmazonMediaStorage
superdesk.command('import:amazon', ImportFromAmazonCommand())
config['DOMAIN'] = {}
app = eve.Eve(
data=superdesk.SuperdeskDataLayer,
auth=TokenAuth,
media=media_storage,
settings=config,
json_encoder=MongoJSONEncoder,
validator=SuperdeskValidator)
superdesk.app = app
custom_loader = jinja2.ChoiceLoader([
app.jinja_loader,
jinja2.FileSystemLoader(['superdesk/templates'])
])
app.jinja_loader = custom_loader
app.mail = Mail(app)
@app.errorhandler(SuperdeskError)
def client_error_handler(error):
"""Return json error response.
:param error: an instance of :attr:`superdesk.SuperdeskError` class
"""
return send_response(None, (error.to_dict(), None, None, error.status_code))
@app.errorhandler(500)
def server_error_handler(error):
"""Log server errors."""
app.sentry.captureException()
logger.exception(error)
return_error = SuperdeskApiError.internalError()
return client_error_handler(return_error)
init_celery(app)
for module_name in app.config['INSTALLED_APPS']:
app_module = importlib.import_module(module_name)
try:
app_module.init_app(app)
except AttributeError:
pass
for resource in superdesk.DOMAIN:
app.register_resource(resource, superdesk.DOMAIN[resource])
for blueprint in superdesk.BLUEPRINTS:
prefix = app.api_prefix or None
app.register_blueprint(blueprint, url_prefix=prefix)
# we can only put mapping when all resources are registered
app.data.elastic.put_mapping(app)
app.sentry = sentry
sentry.init_app(app)
return app
if __name__ == '__main__':
import multiprocessing
debug = True
host = '0.0.0.0'
port = int(os.environ.get('PORT', '5000'))
workers = multiprocessing.cpu_count() * 2 + 1
superdesk.logger.setLevel(logging.INFO)
superdesk.logger.addHandler(logging.StreamHandler())
app = get_app()
app.run(host=host, port=port, debug=debug, use_reloader=debug, processes=workers)