import os

from flask import Flask


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__)
    app.config.from_mapping(
        SECRET_KEY='dev', UPLOAD_FOLDER=os.path.join(
            app.root_path, "articles_files"), ALLOWED_EXTENSIONS={"txt"})

    if test_config is None:
        # load the instance config, if it exists, when not testing
        app.config.from_pyfile('config.py', silent=True)
    else:
        # load the test config if passed in
        app.config.from_mapping(test_config)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass

    from articles_api.articles.articles import articles_api_bp

    app.register_blueprint(articles_api_bp)
    app.add_url_rule('/', endpoint="index")

    return app
