from flask import Flask
import os
import logging
from logging.handlers import RotatingFileHandler

from utk_curio.sandbox.config import Config as config_class

from utk_curio.sandbox.extensions import cache


# Flask app
app = Flask(__name__)
app.config.from_object(config_class)

# Flask-Caching
cache.init_app(app)

if not app.debug and not app.testing:
    if app.config['LOG_TO_STDOUT']:
        stream_handler = logging.StreamHandler()
        stream_handler.setLevel(logging.INFO)
        app.logger.addHandler(stream_handler)
    else:
        if not os.path.exists('logs'):
            os.mkdir('logs')
        file_handler = RotatingFileHandler('logs/sandbox.log',
                                           maxBytes=10240, backupCount=10)
        file_handler.setFormatter(logging.Formatter(
            '%(asctime)s %(levelname)s: %(message)s '
            '[in %(pathname)s:%(lineno)d]'))
        file_handler.setLevel(logging.INFO)
        app.logger.addHandler(file_handler)

    app.logger.setLevel(logging.INFO)
    app.logger.info('Sandbox startup')


from utk_curio.sandbox.app import api