2017-12-16 21:00:46 +01:00
|
|
|
import logging
|
2018-04-10 21:53:18 +02:00
|
|
|
import os
|
2020-05-17 16:42:44 +02:00
|
|
|
from importlib import import_module, reload
|
2017-12-16 21:00:46 +01:00
|
|
|
|
2020-09-16 13:48:15 +02:00
|
|
|
from flask import Flask, render_template
|
2017-12-16 21:00:46 +01:00
|
|
|
from flask_bcrypt import Bcrypt
|
2020-07-14 22:03:56 +02:00
|
|
|
from flask_dramatiq import Dramatiq
|
2018-01-20 19:12:34 +01:00
|
|
|
from flask_migrate import Migrate
|
2017-12-16 21:00:46 +01:00
|
|
|
from flask_sqlalchemy import SQLAlchemy
|
|
|
|
|
2020-05-17 16:42:44 +02:00
|
|
|
from .email.email import Email
|
|
|
|
|
2017-12-16 21:00:46 +01:00
|
|
|
db = SQLAlchemy()
|
|
|
|
bcrypt = Bcrypt()
|
2018-01-20 19:12:34 +01:00
|
|
|
migrate = Migrate()
|
2020-05-17 16:42:44 +02:00
|
|
|
email_service = Email()
|
2020-07-14 22:03:56 +02:00
|
|
|
dramatiq = Dramatiq()
|
2020-09-16 15:41:02 +02:00
|
|
|
appLog = logging.getLogger('fittrackee')
|
2017-12-16 21:00:46 +01:00
|
|
|
|
2018-04-09 22:09:58 +02:00
|
|
|
|
|
|
|
def create_app():
|
|
|
|
# instantiate the app
|
2020-09-16 15:41:02 +02:00
|
|
|
app = Flask(__name__, static_folder='dist/static', template_folder='dist')
|
2018-04-09 22:09:58 +02:00
|
|
|
|
|
|
|
# set config
|
|
|
|
with app.app_context():
|
2018-04-10 21:53:18 +02:00
|
|
|
app_settings = os.getenv('APP_SETTINGS')
|
2020-09-16 15:41:02 +02:00
|
|
|
if app_settings == 'fittrackee.config.TestingConfig':
|
2020-05-17 16:42:44 +02:00
|
|
|
# reload config on tests
|
2020-09-16 15:41:02 +02:00
|
|
|
config = import_module('fittrackee.config')
|
2020-05-17 16:42:44 +02:00
|
|
|
reload(config)
|
2018-04-10 21:53:18 +02:00
|
|
|
app.config.from_object(app_settings)
|
2018-04-09 22:09:58 +02:00
|
|
|
|
|
|
|
# set up extensions
|
|
|
|
db.init_app(app)
|
|
|
|
bcrypt.init_app(app)
|
|
|
|
migrate.init_app(app, db)
|
2020-07-14 22:03:56 +02:00
|
|
|
dramatiq.init_app(app)
|
2018-04-09 22:09:58 +02:00
|
|
|
|
2020-05-17 16:42:44 +02:00
|
|
|
# set up email
|
|
|
|
email_service.init_email(app)
|
|
|
|
|
2019-11-13 18:40:01 +01:00
|
|
|
# get configuration from database
|
|
|
|
from .application.models import AppConfig
|
|
|
|
from .application.utils import init_config, update_app_config_from_database
|
|
|
|
|
|
|
|
with app.app_context():
|
|
|
|
# Note: check if "app_config" table exist to avoid errors when
|
|
|
|
# dropping tables on dev environments
|
|
|
|
if db.engine.dialect.has_table(db.engine, 'app_config'):
|
|
|
|
db_app_config = AppConfig.query.one_or_none()
|
|
|
|
if not db_app_config:
|
|
|
|
_, db_app_config = init_config()
|
|
|
|
update_app_config_from_database(app, db_app_config)
|
|
|
|
|
2018-04-09 22:09:58 +02:00
|
|
|
from .activities.activities import activities_blueprint # noqa
|
2018-05-11 23:12:25 +02:00
|
|
|
from .activities.records import records_blueprint # noqa
|
2018-05-01 16:17:12 +02:00
|
|
|
from .activities.sports import sports_blueprint # noqa
|
2018-06-06 00:22:24 +02:00
|
|
|
from .activities.stats import stats_blueprint # noqa
|
2019-11-13 19:14:13 +01:00
|
|
|
from .application.app_config import config_blueprint # noqa
|
2020-07-14 16:23:48 +02:00
|
|
|
from .users.auth import auth_blueprint # noqa
|
|
|
|
from .users.users import users_blueprint # noqa
|
2018-04-09 22:09:58 +02:00
|
|
|
|
|
|
|
app.register_blueprint(users_blueprint, url_prefix='/api')
|
|
|
|
app.register_blueprint(auth_blueprint, url_prefix='/api')
|
|
|
|
app.register_blueprint(activities_blueprint, url_prefix='/api')
|
2018-05-11 23:12:25 +02:00
|
|
|
app.register_blueprint(records_blueprint, url_prefix='/api')
|
2018-05-01 16:17:12 +02:00
|
|
|
app.register_blueprint(sports_blueprint, url_prefix='/api')
|
2018-06-06 00:22:24 +02:00
|
|
|
app.register_blueprint(stats_blueprint, url_prefix='/api')
|
2019-11-13 18:40:01 +01:00
|
|
|
app.register_blueprint(config_blueprint, url_prefix='/api')
|
2018-04-09 22:09:58 +02:00
|
|
|
|
|
|
|
if app.debug:
|
|
|
|
logging.getLogger('sqlalchemy').setLevel(logging.WARNING)
|
2019-08-28 13:25:39 +02:00
|
|
|
logging.getLogger('sqlalchemy').handlers = logging.getLogger(
|
|
|
|
'werkzeug'
|
|
|
|
).handlers
|
2018-04-09 22:09:58 +02:00
|
|
|
logging.getLogger('sqlalchemy.orm').setLevel(logging.WARNING)
|
|
|
|
logging.getLogger('flake8').propagate = False
|
|
|
|
appLog.setLevel(logging.DEBUG)
|
|
|
|
|
|
|
|
# Enable CORS
|
|
|
|
@app.after_request
|
|
|
|
def after_request(response):
|
|
|
|
response.headers.add('Access-Control-Allow-Origin', '*')
|
|
|
|
response.headers.add(
|
|
|
|
'Access-Control-Allow-Headers', 'Content-Type,Authorization'
|
|
|
|
)
|
|
|
|
response.headers.add(
|
|
|
|
'Access-Control-Allow-Methods',
|
2019-08-28 13:25:39 +02:00
|
|
|
'GET,PUT,POST,DELETE,PATCH,OPTIONS',
|
2018-04-09 22:09:58 +02:00
|
|
|
)
|
|
|
|
return response
|
|
|
|
|
2020-09-16 13:48:15 +02:00
|
|
|
@app.route('/', defaults={'path': ''})
|
|
|
|
@app.route('/<path:path>')
|
|
|
|
def catch_all(path):
|
|
|
|
return render_template('index.html')
|
|
|
|
|
2018-04-09 22:09:58 +02:00
|
|
|
return app
|