Esempio n. 1
0
def create_app():  # FACTORY!
    app = Flask(__name__)

    # dynamic configuration based on the FLASK_ENV system env variable

    # this function would load the env file in production as well,
    # when running flask run, it is done automatically
    # we don't need it as this is handled by docker compose
    #dotenv.load_dotenv()

    app.config.from_object('config.Config')

    # initialize plugins
    db.init_app(app)

    from app.routes import api  # do it here to avoid circular dependency with db
    api.init_app(app)  # api is the flask-restplus instance

    migrate = Migrate(app, db)

    with app.app_context():
        # include the routes, models
        # importing routes not required as this is done in the RESTFUL API object,
        # for standard flask applications this should be done here

        # from app.routes import hello_world
        from app.models import Competition, Team, Player

    return app
def create_app(config_name):
    """ app factory """

    # import config options
    from config.config import app_config

    app = Flask(__name__)

    # allow cross-domain requests
    CORS(app)

    # use running config settings on app
    app.config.from_object(app_config[config_name])
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    # register app with the db
    db.init_app(app)
    # initialize api resources
    api.init_app(app)

    # swagger
    app.config['SWAGGER'] = {
        'title': 'Project Cohort backend API',
        'uiversion': 3
    }

    Swagger(app, template_file='api_docs.json')

    # handle default 404 exceptions with a custom response
    @app.errorhandler(404)
    def resource_not_found(exception):
        response = jsonify(
            dict(status='fail',
                 data={
                     'error':
                     'Not found',
                     'message':
                     'The requested URL was'
                     ' not found on the server. If you entered the URL '
                     'manually please check and try again'
                 }))
        response.status_code = 404
        return response

    # both error handlers below handle default 500 exceptions with a custom
    # response
    @app.errorhandler(500)
    def internal_server_error(error):
        response = jsonify(
            dict(status=error,
                 error='Internal Server Error',
                 message='The server encountered an internal error and was'
                 ' unable to complete your request.  Either the server is'
                 ' overloaded or there is an error in the application'))
        response.status_code = 500
        return response

    return app