Exemple #1
0
def create_app(environment=None):
    app = Flask(__name__)
    load_config(app, environment)
    if app.config['DEBUG'] and app.config.get('LOG_FILENAME'):
        handler = logging.FileHandler(app.config['LOG_FILENAME'])
    else:
        handler = SysLogHandler(
            address=app.config['LOG_ADDRESS'],
            facility=app.config['LOG_FACILITY'],
        )
    handler.setFormatter(logging.Formatter(app.config['LOGGING_FORMATTER']))
    app.logger.addHandler(handler)
    app.logger.setLevel(logging.INFO)

#     def json_error_handler(e):
#         app.logger.error(str(e))
#         return make_response(
#             jsonify({'message': e.description}), e.code
#         )
#     for code in (400, 401, 402, 404, 405, 500):
#         app.error_handler_spec[None][code] = json_error_handler

#     @app.errorhandler(400)
#     def bad_request(error):
#         return make_response(jsonify({'error': 'Bad request'}), 400)

#     @app.errorhandler(404)
#     def not_found(e):
#         return make_response(jsonify({'error': 'Not found'}), 404)

    converters = ConverterFactory()
    converters.register(ImageConverter)

    app.bundles = app.config['BUNDLES']
    app.storages = SoftLinksStorageCollection(
        linkStorageDir=app.config['LINK_STORAGE_DIR'],
        storageConfig=app.config['STORAGES'],
        storagesDir=app.config['STORAGES_DIR'],
        selectStorageStrategy=SelectRandomStorageStrategy(),
        hashStrategy=Xxhash64HashStrategy(),
        genPathStrategy=TwoLevelHierarchyStrategy(),
        genNameStrategy=StaticNameStrategy(),
        converters=converters,
    )

    app.register_blueprint(api_v0_bp)

    return app