from flask import Flask from datetime import datetime def create_app(): app = Flask(__name__) app.config['SECRET_KEY'] = 'your_secret_key' # Set max upload size to 10GB for large database backups app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024 * 1024 # 10GB # Application uses direct MariaDB connections via external_server.conf # No SQLAlchemy ORM needed - all database operations use raw SQL from app.routes import bp as main_bp, warehouse_bp from app.daily_mirror import daily_mirror_bp app.register_blueprint(main_bp, url_prefix='/') app.register_blueprint(warehouse_bp, url_prefix='/warehouse') app.register_blueprint(daily_mirror_bp) # Add 'now' function to Jinja2 globals app.jinja_env.globals['now'] = datetime.now # Add license check middleware @app.before_request def check_license_middleware(): from flask import session, request, redirect, url_for, flash, render_template import os import json from datetime import datetime # Skip license check for static files, login page, and superadmin users if request.endpoint and ( request.endpoint == 'static' or request.endpoint == 'main.login' or request.path.startswith('/static/') ): return None # Skip if user is not logged in (will be redirected to login by other means) if 'user' not in session: return None # Skip license check for superadmin if session.get('role') == 'superadmin': return None # Check license validity license_path = os.path.join(app.instance_path, 'app_license.json') if not os.path.exists(license_path): session.clear() flash('⚠️ Application License Missing - Please contact your superadmin to generate a license key.', 'danger') return redirect(url_for('main.login')) try: with open(license_path, 'r') as f: license_data = json.load(f) valid_until = datetime.strptime(license_data['valid_until'], '%Y-%m-%d') if datetime.utcnow().date() > valid_until.date(): session.clear() flash(f'⚠️ Application License Expired on {license_data["valid_until"]} - Please contact your superadmin to renew the license.', 'danger') return redirect(url_for('main.login')) except Exception as e: session.clear() flash('⚠️ License Validation Error - Please contact your superadmin.', 'danger') return redirect(url_for('main.login')) return None # Initialize automatic backup scheduler from app.backup_scheduler import init_backup_scheduler init_backup_scheduler(app) print("✅ Automatic backup scheduler initialized") return app