50 lines
1.6 KiB
Python
Raw Permalink Normal View History

2025-07-14 13:42:58 +01:00
import os
from flask import Flask, jsonify
2025-07-14 13:42:58 +01:00
from dotenv import load_dotenv
from ktvmanager.config import DevelopmentConfig, ProductionConfig
from routes.api import api_blueprint
2025-07-14 18:32:48 +01:00
from ktvmanager.lib.database import initialize_db_pool
2025-07-18 17:51:48 +01:00
from ktvmanager.account_checker import send_expiry_notifications
from apscheduler.schedulers.background import BackgroundScheduler
def create_app():
app = Flask(__name__)
2025-07-14 13:42:58 +01:00
load_dotenv()
if os.environ.get("FLASK_ENV") == "production":
app.config.from_object(ProductionConfig)
else:
app.config.from_object(DevelopmentConfig)
2025-07-14 18:32:48 +01:00
with app.app_context():
initialize_db_pool()
2025-07-18 17:51:48 +01:00
# Schedule the daily account check
scheduler = BackgroundScheduler()
# Pass the app instance to the job
2025-07-18 18:03:43 +01:00
scheduler.add_job(func=lambda: send_expiry_notifications(app), trigger="cron", hour=10, minute=0)
2025-07-18 17:51:48 +01:00
scheduler.start()
# Register blueprints
app.register_blueprint(api_blueprint)
2025-07-19 09:05:25 +01:00
@app.route('/check-expiry', methods=['POST'])
def check_expiry():
"""Manually triggers the expiry notification check."""
send_expiry_notifications(app)
return jsonify({"message": "Expiry notification check triggered."})
# Error handlers
@app.errorhandler(404)
def not_found(error):
return jsonify({"error": "Not found"}), 404
@app.errorhandler(500)
def server_error(error):
return jsonify({"error": "Server error"}), 500
return app
if __name__ == "__main__":
app = create_app()
2025-07-14 13:42:58 +01:00
app.run(debug=app.config["DEBUG"], host=app.config["HOST"], port=app.config["PORT"])