2025-07-19 09:16:21 +01:00

52 lines
1.6 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 routes.dns import dns_bp
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="cron", hour=10, minute=0)
scheduler.start()
# Register blueprints
app.register_blueprint(api_blueprint)
app.register_blueprint(dns_bp)
@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()
app.run(debug=app.config["DEBUG"], host=app.config["HOST"], port=app.config["PORT"])