execute on url update
This commit is contained in:
parent
dbf0161133
commit
6462fc6009
37
app.py
37
app.py
@ -11,6 +11,7 @@ import redis
|
|||||||
import json
|
import json
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
import re
|
import re
|
||||||
|
import threading
|
||||||
|
|
||||||
from lib.datetime import filter_accounts_next_30_days, filter_accounts_expired
|
from lib.datetime import filter_accounts_next_30_days, filter_accounts_expired
|
||||||
from lib.reqs import (get_urls, get_user_accounts, add_user_account,
|
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
|
base_url, session["auth_credentials"], username, password, stream
|
||||||
):
|
):
|
||||||
cache.delete_memoized(user_accounts, key_prefix=make_cache_key)
|
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 redirect(url_for("user_accounts"))
|
||||||
|
|
||||||
return render_template(
|
return render_template(
|
||||||
@ -288,6 +292,9 @@ def validate_account() -> Tuple[Response, int]:
|
|||||||
response_data = response.json()
|
response_data = response.json()
|
||||||
if response_data.get("message") == "Account is valid and updated":
|
if response_data.get("message") == "Account is valid and updated":
|
||||||
cache.delete_memoized(user_accounts, key_prefix=make_cache_key)
|
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
|
return jsonify(response_data), response.status_code
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
return jsonify({"error": str(e)}), 500
|
return jsonify({"error": str(e)}), 500
|
||||||
@ -492,10 +499,11 @@ def update_config_with_streams(config, streams):
|
|||||||
|
|
||||||
return config
|
return config
|
||||||
|
|
||||||
@app.route('/update_host_9_config', methods=['POST'])
|
def _update_npm_config():
|
||||||
def update_host_9_config():
|
"""Helper function to update the NPM config."""
|
||||||
if not session.get('user_id') or int(session.get('user_id')) != 1:
|
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 = NginxProxyManager(app.config['NPM_HOST'], app.config['NPM_EMAIL'], app.config['NPM_PASSWORD'])
|
||||||
npm.login()
|
npm.login()
|
||||||
@ -503,7 +511,6 @@ def update_host_9_config():
|
|||||||
if host:
|
if host:
|
||||||
current_config = host.get('advanced_config', '')
|
current_config = host.get('advanced_config', '')
|
||||||
|
|
||||||
# Fetch streams from the backend API
|
|
||||||
backend_url = f"{app.config['BACKEND_URL']}/get_all_stream_urls"
|
backend_url = f"{app.config['BACKEND_URL']}/get_all_stream_urls"
|
||||||
credentials = base64.b64decode(session["auth_credentials"]).decode()
|
credentials = base64.b64decode(session["auth_credentials"]).decode()
|
||||||
username, password = credentials.split(":", 1)
|
username, password = credentials.split(":", 1)
|
||||||
@ -514,13 +521,29 @@ def update_host_9_config():
|
|||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
streams = response.json()
|
streams = response.json()
|
||||||
except requests.exceptions.RequestException as e:
|
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:
|
if streams:
|
||||||
new_config = update_config_with_streams(current_config, streams)
|
new_config = update_config_with_streams(current_config, streams)
|
||||||
npm.update_proxy_host_config(9, new_config)
|
npm.update_proxy_host_config(9, new_config)
|
||||||
return jsonify({'message': 'Config updated successfully'}), 200
|
print("NPM config updated successfully.")
|
||||||
return jsonify({'error': 'Failed to update config'}), 500
|
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__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
x
Reference in New Issue
Block a user