From b44a2cef530865ddb766b12311bbb699b857ece5 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 17 Sep 2020 16:08:57 +0200 Subject: [PATCH] API - minor refactor --- .gitlab-ci.yml | 4 +-- Makefile | 4 +-- Makefile.config | 2 +- fittrackee/__main__.py | 60 +++++++++++++++++++++++++++++++++++---- fittrackee/server.py | 64 ------------------------------------------ 5 files changed, 60 insertions(+), 74 deletions(-) delete mode 100644 fittrackee/server.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a78bd86b..63dbb863 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -8,7 +8,7 @@ variables: APP_SETTINGS: fittrackee.config.TestingConfig DATABASE_TEST_URL: postgres://fittrackee:fittrackee@postgres:5432/fittrackee_test EMAIL_URL: smtp://none:none@0.0.0.0:1025 - FLASK_APP: fittrackee/server.py + FLASK_APP: fittrackee/__main__.py SENDER_EMAIL: fittrackee@example.com services: @@ -57,7 +57,7 @@ firefox: - poetry config virtualenvs.create false - poetry install --no-interaction --quiet - flask db upgrade --directory fittrackee/migrations - - flask initdata + - flask init-data - setsid nohup flask run --with-threads -h 0.0.0.0 -p 5000 >> nohup.out 2>&1 & - export TEST_APP_URL=http://$(hostname --ip-address):5000 - sleep 5 diff --git a/Makefile b/Makefile index 593ccb1d..c24e3cdf 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ html: install-db: psql -U postgres -f db/create.sql $(FLASK) db upgrade --directory $(MIGRATIONS) - $(FLASK) initdata + $(FLASK) init-data init-app-config: $(FLASK) init-app-config @@ -35,7 +35,7 @@ init-app-config: init-db: $(FLASK) drop-db $(FLASK) db upgrade --directory $(MIGRATIONS) - $(FLASK) initdata + $(FLASK) init-data install: install-client install-python diff --git a/Makefile.config b/Makefile.config index 80bfcd34..600747ca 100644 --- a/Makefile.config +++ b/Makefile.config @@ -2,7 +2,7 @@ HOST = 0.0.0.0 API_PORT = 5000 CLIENT_PORT = 3000 -export FLASK_APP = $(PWD)/fittrackee/server.py +export FLASK_APP = $(PWD)/fittrackee/__main__.py export FLASK_ENV=development export TEST_APP_URL = http://$(HOST):$(API_PORT) export TEST_CLIENT_URL = http://$(HOST):$(CLIENT_PORT) diff --git a/fittrackee/__main__.py b/fittrackee/__main__.py index 4ce12a68..865420a3 100644 --- a/fittrackee/__main__.py +++ b/fittrackee/__main__.py @@ -1,11 +1,17 @@ -# source: http://docs.gunicorn.org/en/stable/custom.html +# source for StandaloneApplication class: +# http://docs.gunicorn.org/en/stable/custom.html import os +import shutil import gunicorn.app.base -from fittrackee import create_app +from fittrackee import create_app, db +from fittrackee.activities.models import Activity +from fittrackee.activities.utils import update_activity +from fittrackee.application.utils import init_config from fittrackee.database_utils import init_database from flask_dramatiq import worker from flask_migrate import upgrade +from tqdm import tqdm HOST = os.getenv('HOST', '0.0.0.0') PORT = os.getenv('API_PORT', '5000') @@ -39,9 +45,53 @@ def upgrade_db(): upgrade(directory=BASEDIR + '/migrations') +@app.cli.command('drop-db') +def drop_db(): + """Empty database for dev environments.""" + db.engine.execute("DROP TABLE IF EXISTS alembic_version;") + db.drop_all() + db.session.commit() + print('Database dropped.') + shutil.rmtree(app.config['UPLOAD_FOLDER'], ignore_errors=True) + print('Uploaded files deleted.') + + +@app.cli.command('init-data') def init_data(): - with app.app_context(): - init_database(app) + """Init the database and application config.""" + init_database(app) + + +@app.cli.command() +def recalculate(): + print("Starting activities data refresh") + activities = ( + Activity.query.filter(Activity.gpx != None) # noqa + .order_by(Activity.activity_date.asc()) # noqa + .all() + ) + if len(activities) == 0: + print('➡️ no activities to upgrade.') + return None + pbar = tqdm(activities) + for activity in pbar: + update_activity(activity) + pbar.set_postfix(activitiy_id=activity.id) + db.session.commit() + + +@app.cli.command('init-app-config') +def init_app_config(): + """Init application configuration.""" + print("Init application configuration") + config_created, _ = init_config() + if config_created: + print("Creation done!") + else: + print( + "Application configuration already existing in database. " + "Please use web application to update it." + ) def main(): @@ -50,4 +100,4 @@ def main(): if __name__ == '__main__': - main() + app.run() diff --git a/fittrackee/server.py b/fittrackee/server.py deleted file mode 100644 index af9d4dd3..00000000 --- a/fittrackee/server.py +++ /dev/null @@ -1,64 +0,0 @@ -import shutil - -from fittrackee import create_app, db -from fittrackee.activities.models import Activity -from fittrackee.activities.utils import update_activity -from fittrackee.application.utils import init_config -from tqdm import tqdm - -from .database_utils import init_database - -app = create_app() - - -@app.cli.command() -def drop_db(): - """Empty database for dev environments.""" - db.engine.execute("DROP TABLE IF EXISTS alembic_version;") - db.drop_all() - db.session.commit() - print('Database dropped.') - shutil.rmtree(app.config['UPLOAD_FOLDER'], ignore_errors=True) - print('Uploaded files deleted.') - - -@app.cli.command() -def initdata(): - """Init the database and application config.""" - init_database(app) - - -@app.cli.command() -def recalculate(): - print("Starting activities data refresh") - activities = ( - Activity.query.filter(Activity.gpx != None) # noqa - .order_by(Activity.activity_date.asc()) # noqa - .all() - ) - if len(activities) == 0: - print('➡️ no activities to upgrade.') - return None - pbar = tqdm(activities) - for activity in pbar: - update_activity(activity) - pbar.set_postfix(activitiy_id=activity.id) - db.session.commit() - - -@app.cli.command('init-app-config') -def init_app_config(): - """Init application configuration.""" - print("Init application configuration") - config_created, _ = init_config() - if config_created: - print("Creation done!") - else: - print( - "Application configuration already existing in database. " - "Please use web application to update it." - ) - - -if __name__ == '__main__': - app.run()