reorganize app factory

This commit is contained in:
Lili (Tlapka) 2022-09-26 11:10:58 +02:00
parent 4adac38871
commit 8bbe273df4
1 changed files with 19 additions and 6 deletions

View File

@ -6,11 +6,6 @@ from tlapbot.owncast_helpers import is_stream_live, give_points_to_chat
def create_app(test_config=None): def create_app(test_config=None):
app = Flask(__name__, instance_relative_config=True) app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
DATABASE=os.path.join(app.instance_path, "tlapbot.sqlite")
)
app.config.from_object('tlapbot.default_config')
app.config.from_pyfile('config.py')
# ensure the instance folder exists # ensure the instance folder exists
try: try:
@ -18,18 +13,36 @@ def create_app(test_config=None):
except OSError: except OSError:
pass pass
from . import db # Prepare config: set db to instance folder, then load default, then
# overwrite it with config.py
app.config.from_mapping(
DATABASE=os.path.join(app.instance_path, "tlapbot.sqlite")
)
app.config.from_object('tlapbot.default_config')
app.config.from_pyfile('config.py')
# prepare webhooks and redeem dashboard blueprints
from . import owncast_webhooks from . import owncast_webhooks
from . import owncast_redeem_dashboard from . import owncast_redeem_dashboard
app.register_blueprint(owncast_webhooks.bp) app.register_blueprint(owncast_webhooks.bp)
app.register_blueprint(owncast_redeem_dashboard.bp) app.register_blueprint(owncast_redeem_dashboard.bp)
# add db initialization CLI command
from . import db
db.init_app(app) db.init_app(app)
# add clear queue CLI command
from . import owncast_helpers
app.cli.add_command(owncast_helpers.clear_queue_command)
# scheduler job for giving points to users
def proxy_job(): def proxy_job():
with app.app_context(): with app.app_context():
if is_stream_live(): if is_stream_live():
give_points_to_chat(get_db()) give_points_to_chat(get_db())
# start scheduler that will give points to users
points_giver = BackgroundScheduler() points_giver = BackgroundScheduler()
points_giver.add_job(proxy_job, 'interval', seconds=app.config['POINTS_CYCLE_TIME']) # change to 10 minutes out of testing points_giver.add_job(proxy_job, 'interval', seconds=app.config['POINTS_CYCLE_TIME']) # change to 10 minutes out of testing
points_giver.start() points_giver.start()