add entry points for fittrackee package
This commit is contained in:
@ -24,7 +24,9 @@ def create_app():
|
||||
|
||||
# set config
|
||||
with app.app_context():
|
||||
app_settings = os.getenv('APP_SETTINGS')
|
||||
app_settings = os.getenv(
|
||||
'APP_SETTINGS', 'fittrackee.config.ProductionConfig'
|
||||
)
|
||||
if app_settings == 'fittrackee.config.TestingConfig':
|
||||
# reload config on tests
|
||||
config = import_module('fittrackee.config')
|
||||
|
53
fittrackee/__main__.py
Normal file
53
fittrackee/__main__.py
Normal file
@ -0,0 +1,53 @@
|
||||
# source: http://docs.gunicorn.org/en/stable/custom.html
|
||||
import os
|
||||
|
||||
import gunicorn.app.base
|
||||
from fittrackee import create_app
|
||||
from fittrackee.database_utils import init_database
|
||||
from flask_dramatiq import worker
|
||||
from flask_migrate import upgrade
|
||||
|
||||
HOST = os.getenv('HOST', '0.0.0.0')
|
||||
PORT = os.getenv('API_PORT', '5000')
|
||||
WORKERS = os.getenv('APP_WORKERS', 1)
|
||||
BASEDIR = os.path.abspath(os.path.dirname(__file__))
|
||||
app = create_app()
|
||||
dramatiq_worker = worker
|
||||
|
||||
|
||||
class StandaloneApplication(gunicorn.app.base.BaseApplication):
|
||||
def __init__(self, current_app, options=None):
|
||||
self.options = options or {}
|
||||
self.application = current_app
|
||||
super().__init__()
|
||||
|
||||
def load_config(self):
|
||||
config = {
|
||||
key: value
|
||||
for key, value in self.options.items()
|
||||
if key in self.cfg.settings and value is not None
|
||||
}
|
||||
for key, value in config.items():
|
||||
self.cfg.set(key.lower(), value)
|
||||
|
||||
def load(self):
|
||||
return self.application
|
||||
|
||||
|
||||
def upgrade_db():
|
||||
with app.app_context():
|
||||
upgrade(directory=BASEDIR + '/migrations')
|
||||
|
||||
|
||||
def init_data():
|
||||
with app.app_context():
|
||||
init_database(app)
|
||||
|
||||
|
||||
def main():
|
||||
options = {'bind': f'{HOST}:{PORT}', 'workers': WORKERS}
|
||||
StandaloneApplication(app, options).run()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -3,6 +3,7 @@ import os
|
||||
from dramatiq.brokers.redis import RedisBroker
|
||||
from dramatiq.brokers.stub import StubBroker
|
||||
from flask import current_app
|
||||
from sqlalchemy.pool import NullPool
|
||||
|
||||
if os.getenv('APP_SETTINGS') == 'fittrackee.config.TestingConfig':
|
||||
broker = StubBroker
|
||||
@ -20,7 +21,9 @@ class BaseConfig:
|
||||
TOKEN_EXPIRATION_DAYS = 30
|
||||
TOKEN_EXPIRATION_SECONDS = 0
|
||||
PASSWORD_TOKEN_EXPIRATION_SECONDS = 3600
|
||||
UPLOAD_FOLDER = os.path.join(current_app.root_path, 'uploads')
|
||||
UPLOAD_FOLDER = os.path.join(
|
||||
os.getenv('UPLOAD_FOLDER', current_app.root_path), 'uploads'
|
||||
)
|
||||
PICTURE_ALLOWED_EXTENSIONS = {'jpg', 'png', 'gif'}
|
||||
ACTIVITY_ALLOWED_EXTENSIONS = {'gpx', 'zip'}
|
||||
TEMPLATES_FOLDER = os.path.join(current_app.root_path, 'email/templates')
|
||||
@ -68,3 +71,18 @@ class TestingConfig(BaseConfig):
|
||||
TOKEN_EXPIRATION_SECONDS = 3
|
||||
PASSWORD_TOKEN_EXPIRATION_SECONDS = 3
|
||||
UPLOAD_FOLDER = '/tmp/fitTrackee/uploads'
|
||||
|
||||
|
||||
class ProductionConfig(BaseConfig):
|
||||
"""Production configuration"""
|
||||
|
||||
DEBUG = False
|
||||
# https://docs.sqlalchemy.org/en/13/core/pooling.html#using-connection-pools-with-multiprocessing-or-os-fork # noqa
|
||||
SQLALCHEMY_ENGINE_OPTIONS = (
|
||||
{'poolclass': NullPool}
|
||||
if os.getenv('DATABASE_DISABLE_POOLING', True)
|
||||
else {}
|
||||
)
|
||||
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
|
||||
SECRET_KEY = os.getenv('APP_SECRET_KEY')
|
||||
DRAMATIQ_BROKER_URL = os.getenv('REDIS_URL', 'redis://')
|
||||
|
46
fittrackee/database_utils.py
Normal file
46
fittrackee/database_utils.py
Normal file
@ -0,0 +1,46 @@
|
||||
from fittrackee import db
|
||||
from fittrackee.activities.models import Sport
|
||||
from fittrackee.application.utils import (
|
||||
init_config,
|
||||
update_app_config_from_database,
|
||||
)
|
||||
from fittrackee.users.models import User
|
||||
|
||||
|
||||
def init_database(app):
|
||||
"""Init the database."""
|
||||
admin = User(
|
||||
username='admin', email='admin@example.com', password='mpwoadmin'
|
||||
)
|
||||
admin.admin = True
|
||||
admin.timezone = 'Europe/Paris'
|
||||
db.session.add(admin)
|
||||
sport = Sport(label='Cycling (Sport)')
|
||||
sport.img = '/img/sports/cycling-sport.png'
|
||||
sport.is_default = True
|
||||
db.session.add(sport)
|
||||
sport = Sport(label='Cycling (Transport)')
|
||||
sport.img = '/img/sports/cycling-transport.png'
|
||||
sport.is_default = True
|
||||
db.session.add(sport)
|
||||
sport = Sport(label='Hiking')
|
||||
sport.img = '/img/sports/hiking.png'
|
||||
sport.is_default = True
|
||||
db.session.add(sport)
|
||||
sport = Sport(label='Mountain Biking')
|
||||
sport.img = '/img/sports/mountain-biking.png'
|
||||
sport.is_default = True
|
||||
db.session.add(sport)
|
||||
sport = Sport(label='Running')
|
||||
sport.img = '/img/sports/running.png'
|
||||
sport.is_default = True
|
||||
db.session.add(sport)
|
||||
sport = Sport(label='Walking')
|
||||
sport.img = '/img/sports/walking.png'
|
||||
sport.is_default = True
|
||||
db.session.add(sport)
|
||||
db.session.commit()
|
||||
_, db_app_config = init_config()
|
||||
update_app_config_from_database(app, db_app_config)
|
||||
|
||||
print('Initial data stored in database.')
|
@ -1,15 +1,13 @@
|
||||
import shutil
|
||||
|
||||
from fittrackee import create_app, db
|
||||
from fittrackee.activities.models import Activity, Sport
|
||||
from fittrackee.activities.models import Activity
|
||||
from fittrackee.activities.utils import update_activity
|
||||
from fittrackee.application.utils import (
|
||||
init_config,
|
||||
update_app_config_from_database,
|
||||
)
|
||||
from fittrackee.users.models import User
|
||||
from fittrackee.application.utils import init_config
|
||||
from tqdm import tqdm
|
||||
|
||||
from .database_utils import init_database
|
||||
|
||||
app = create_app()
|
||||
|
||||
|
||||
@ -26,43 +24,8 @@ def drop_db():
|
||||
|
||||
@app.cli.command()
|
||||
def initdata():
|
||||
"""Init the database."""
|
||||
admin = User(
|
||||
username='admin', email='admin@example.com', password='mpwoadmin'
|
||||
)
|
||||
admin.admin = True
|
||||
admin.timezone = 'Europe/Paris'
|
||||
db.session.add(admin)
|
||||
sport = Sport(label='Cycling (Sport)')
|
||||
sport.img = '/img/sports/cycling-sport.png'
|
||||
sport.is_default = True
|
||||
db.session.add(sport)
|
||||
sport = Sport(label='Cycling (Transport)')
|
||||
sport.img = '/img/sports/cycling-transport.png'
|
||||
sport.is_default = True
|
||||
db.session.add(sport)
|
||||
sport = Sport(label='Hiking')
|
||||
sport.img = '/img/sports/hiking.png'
|
||||
sport.is_default = True
|
||||
db.session.add(sport)
|
||||
sport = Sport(label='Mountain Biking')
|
||||
sport.img = '/img/sports/mountain-biking.png'
|
||||
sport.is_default = True
|
||||
db.session.add(sport)
|
||||
sport = Sport(label='Running')
|
||||
sport.img = '/img/sports/running.png'
|
||||
sport.is_default = True
|
||||
db.session.add(sport)
|
||||
sport = Sport(label='Walking')
|
||||
sport.img = '/img/sports/walking.png'
|
||||
sport.is_default = True
|
||||
db.session.add(sport)
|
||||
db.session.commit()
|
||||
# update app config
|
||||
_, db_app_config = init_config()
|
||||
update_app_config_from_database(app, db_app_config)
|
||||
|
||||
print('Initial data stored in database.')
|
||||
"""Init the database and application config."""
|
||||
init_database(app)
|
||||
|
||||
|
||||
@app.cli.command()
|
||||
|
Reference in New Issue
Block a user