From 86c642d8748b29c143ec9a0192bc6277ce76a817 Mon Sep 17 00:00:00 2001 From: CaliBrain Date: Wed, 6 Aug 2025 17:17:32 -0400 Subject: [PATCH] Restart Button when on DEBUG mode (#211) Feature for #210 --- app.py | 9 ++++++++ static/js/main.js | 49 ++++++++++++++++++++++++++++++++++++++++++++ templates/index.html | 5 +++++ 3 files changed, 63 insertions(+) diff --git a/app.py b/app.py index b6edea4..28b304d 100644 --- a/app.py +++ b/app.py @@ -153,6 +153,15 @@ if DEBUG: logger.error_trace(f"Debug endpoint error: {e}") return jsonify({"error": str(e)}), 500 +if DEBUG: + @app.route('/api/restart', methods=['GET']) + @login_required + def restart() -> Union[Response, Tuple[Response, int]]: + """ + Restart the application + """ + os._exit(0) + @app.route('/api/search', methods=['GET']) @login_required def api_search() -> Union[Response, Tuple[Response, int]]: diff --git a/static/js/main.js b/static/js/main.js index 8e0aadb..e6584d5 100644 --- a/static/js/main.js +++ b/static/js/main.js @@ -32,6 +32,11 @@ document.addEventListener('DOMContentLoaded', () => { form: document.getElementById('debug-form'), button: document.getElementById('debug-button'), spinner: document.getElementById('debug-spinner') + }, + restart: { + form: document.getElementById('restart-form'), + button: document.getElementById('restart-button'), + spinner: document.getElementById('restart-spinner') } }; @@ -753,6 +758,49 @@ document.addEventListener('DOMContentLoaded', () => { } }; + // Restart Functions + const restart = { + init() { + if (!elements.restart.form) return; + + elements.restart.form.addEventListener('submit', (e) => { + e.preventDefault(); // Prevent default form submission + + if (elements.restart.button.disabled) { + return; // Prevent multiple submissions + } + + // Show confirmation dialog + UIkit.modal.confirm('Are you sure you want to restart the application? This will interrupt any ongoing downloads.').then(() => { + // User confirmed, proceed with restart + elements.restart.spinner.classList.remove('uk-hidden'); + elements.restart.button.disabled = true; + + // Show restarting message + UIkit.notification({ + message: 'Application restarting...', + status: 'warning', + pos: 'top-center', + timeout: 5000 + }); + + // Make the restart request - this will kill the server + fetch(elements.restart.form.action) + .catch(() => { + // Expected to fail when server shuts down + }); + + // Wait a bit then refresh the page to reconnect to the restarted server + setTimeout(() => { + window.location.reload(); + }, 3000); + }, () => { + // User cancelled, do nothing + }); + }); + } + }; + // Event Listeners function setupEventListeners() { // Search events @@ -830,6 +878,7 @@ document.addEventListener('DOMContentLoaded', () => { setupEventListeners(); theme.init(); // Initialize theme management debug.init(); // Initialize debug functionality + restart.init(); // Initialize restart functionality status.fetch(); setInterval(() => status.fetch(), REFRESH_INTERVAL); } diff --git a/templates/index.html b/templates/index.html index 8dd7db4..15ca25f 100644 --- a/templates/index.html +++ b/templates/index.html @@ -22,6 +22,11 @@

Book Search & Download

{% if debug %} +
+ +