diff --git a/app.py b/app.py index 1cf151c..059a54e 100644 --- a/app.py +++ b/app.py @@ -11,6 +11,7 @@ import redis import json import mysql.connector import re +import threading from lib.datetime import filter_accounts_next_30_days, filter_accounts_expired from lib.reqs import (get_urls, get_user_accounts, add_user_account, @@ -252,6 +253,9 @@ def add_account() -> Union[Response, str]: base_url, session["auth_credentials"], username, password, stream ): cache.delete_memoized(user_accounts, key_prefix=make_cache_key) + # Run the NPM config update in a background thread + thread = threading.Thread(target=_update_npm_config_in_background) + thread.start() return redirect(url_for("user_accounts")) return render_template( @@ -288,6 +292,9 @@ def validate_account() -> Tuple[Response, int]: response_data = response.json() if response_data.get("message") == "Account is valid and updated": cache.delete_memoized(user_accounts, key_prefix=make_cache_key) + # Run the NPM config update in a background thread + thread = threading.Thread(target=_update_npm_config_in_background) + thread.start() return jsonify(response_data), response.status_code except requests.exceptions.RequestException as e: return jsonify({"error": str(e)}), 500 @@ -492,10 +499,11 @@ def update_config_with_streams(config, streams): return config -@app.route('/update_host_9_config', methods=['POST']) -def update_host_9_config(): +def _update_npm_config(): + """Helper function to update the NPM config.""" if not session.get('user_id') or int(session.get('user_id')) != 1: - return jsonify({'error': 'Unauthorized'}), 401 + print("Unauthorized attempt to update NPM config.") + return npm = NginxProxyManager(app.config['NPM_HOST'], app.config['NPM_EMAIL'], app.config['NPM_PASSWORD']) npm.login() @@ -503,7 +511,6 @@ def update_host_9_config(): if host: current_config = host.get('advanced_config', '') - # Fetch streams from the backend API backend_url = f"{app.config['BACKEND_URL']}/get_all_stream_urls" credentials = base64.b64decode(session["auth_credentials"]).decode() username, password = credentials.split(":", 1) @@ -514,13 +521,29 @@ def update_host_9_config(): response.raise_for_status() streams = response.json() except requests.exceptions.RequestException as e: - return jsonify({"error": f"Failed to fetch streams from backend: {e}"}), 502 + print(f"Failed to fetch streams from backend: {e}") + return if streams: new_config = update_config_with_streams(current_config, streams) npm.update_proxy_host_config(9, new_config) - return jsonify({'message': 'Config updated successfully'}), 200 - return jsonify({'error': 'Failed to update config'}), 500 + print("NPM config updated successfully.") + else: + print("Failed to update NPM config.") + +def _update_npm_config_in_background(): + with app.app_context(): + _update_npm_config() + +@app.route('/update_host_9_config', methods=['POST']) +def update_host_9_config(): + if not session.get('user_id') or int(session.get('user_id')) != 1: + return jsonify({'error': 'Unauthorized'}), 401 + + thread = threading.Thread(target=_update_npm_config_in_background) + thread.start() + + return jsonify({'message': 'NPM config update started in the background.'}), 202 if __name__ == "__main__":