2025-07-18 17:51:48 +01:00

44 lines
1.3 KiB
Python

import os
from flask import Flask, jsonify
from dotenv import load_dotenv
from ktvmanager.config import DevelopmentConfig, ProductionConfig
from routes.api import api_blueprint
from ktvmanager.lib.database import initialize_db_pool
from ktvmanager.account_checker import send_expiry_notifications
from apscheduler.schedulers.background import BackgroundScheduler
def create_app():
app = Flask(__name__)
load_dotenv()
if os.environ.get("FLASK_ENV") == "production":
app.config.from_object(ProductionConfig)
else:
app.config.from_object(DevelopmentConfig)
with app.app_context():
initialize_db_pool()
# Schedule the daily account check
scheduler = BackgroundScheduler()
# Pass the app instance to the job
scheduler.add_job(func=lambda: send_expiry_notifications(app), trigger="interval", days=1)
scheduler.start()
# Register blueprints
app.register_blueprint(api_blueprint)
# 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()
app.run(debug=app.config["DEBUG"], host=app.config["HOST"], port=app.config["PORT"])