From e9d1f985aed6152443c5e87ef936bd8217c2f2f9 Mon Sep 17 00:00:00 2001 From: pika Date: Thu, 3 Apr 2025 11:58:17 +0200 Subject: [PATCH] now the ports cannot be assigned twice, also simmilar application names work --- app/routes/api.py | 97 +++++++-------------------- app/templates/dashboard/app_form.html | 6 +- app/templates/layout.html | 4 +- app/utils/app_utils.py | 39 ++++++----- compose.yml | 8 +-- config/settings.py | 4 +- run.py | 5 +- 7 files changed, 57 insertions(+), 106 deletions(-) diff --git a/app/routes/api.py b/app/routes/api.py index ffd1cdf..36c40d2 100644 --- a/app/routes/api.py +++ b/app/routes/api.py @@ -18,7 +18,7 @@ from flask_wtf import CSRFProtect import markdown from datetime import datetime from flask import flash -from app.utils.app_utils import is_port_in_use +from app.utils.app_utils import is_port_in_use, validate_port_data bp = Blueprint("api", __name__, url_prefix="/api") csrf = CSRFProtect() @@ -287,89 +287,40 @@ def add_app_port(app_id): """Add a port to an application""" app = App.query.get_or_404(app_id) - # Check if request is AJAX (XMLHttpRequest) - is_ajax = request.headers.get('X-Requested-With') == 'XMLHttpRequest' or request.accept_mimetypes.best == 'application/json' + # Get port details from the form + port_number = request.form.get("port_number") + protocol = request.form.get("protocol", "TCP") + description = request.form.get("description", "") + # Validate the port + valid, clean_port, error = validate_port_data ( # validate_port_data( + port_number, + protocol, + description, + app.server_id, + app_id + ) + + if not valid: + flash(error, "danger") + return redirect(url_for("dashboard.app_view", app_id=app_id)) + + # Create the new port try: - port_number = request.form.get("port_number") - protocol = request.form.get("protocol", "TCP") - description = request.form.get("description", "") - - # Validate port data with server-side conflict check - valid, clean_port, error = validate_port_data( - port_number, protocol, description, app.server_id, app_id - ) - - if not valid: - flash(error, "danger") - - # If port is in use by another app, provide a direct link - if "already in use by application" in error: - app_name = error.split("'")[1] # Extract app name from error message - conflict_app = App.query.filter_by(name=app_name, server_id=app.server_id).first() - if conflict_app: - edit_url = url_for('dashboard.app_edit', app_id=conflict_app.id) - edit_link = f'Edit {app_name}' - flash(f"Would you like to edit the conflicting application? {edit_link}", "info") - - return ( - redirect(url_for("dashboard.app_view", app_id=app_id)) - if not is_ajax - else jsonify({"success": False, "error": error}) - ), 400 - - # Check if port already exists for this app - existing_port = Port.query.filter_by( - app_id=app_id, port_number=clean_port, protocol=protocol - ).first() - - if existing_port: - error_msg = f"Port {clean_port}/{protocol} already exists for this application" - flash(error_msg, "warning") - return ( - redirect(url_for("dashboard.app_view", app_id=app_id)) - if not is_ajax - else jsonify({"success": False, "error": error_msg}) - ), 400 - - # Create new port new_port = Port( app_id=app_id, port_number=clean_port, protocol=protocol, - description=description, + description=description ) db.session.add(new_port) db.session.commit() - - success_msg = f"Port {clean_port}/{protocol} added successfully" - flash(success_msg, "success") - - # If it's a regular form submission (not AJAX), redirect - if not is_ajax and request.content_type != 'application/json': - return redirect(url_for("dashboard.app_view", app_id=app_id)) - - # Otherwise return JSON response - return jsonify({ - "success": True, - "message": success_msg, - "port": { - "id": new_port.id, - "port_number": new_port.port_number, - "protocol": new_port.protocol, - "description": new_port.description - } - }) - + flash(f"Port {clean_port}/{protocol} added successfully", "success") except Exception as e: db.session.rollback() - error_msg = f"Error adding port: {str(e)}" - flash(error_msg, "danger") - return ( - redirect(url_for("dashboard.app_view", app_id=app_id)) - if not is_ajax - else jsonify({"success": False, "error": error_msg}) - ), 500 + flash(f"Error adding port: {str(e)}", "danger") + + return redirect(url_for("dashboard.app_view", app_id=app_id)) @bp.route("/app//ports", methods=["GET"]) diff --git a/app/templates/dashboard/app_form.html b/app/templates/dashboard/app_form.html index d5250f6..343e6d0 100644 --- a/app/templates/dashboard/app_form.html +++ b/app/templates/dashboard/app_form.html @@ -297,13 +297,13 @@ return; } - const response = await fetch(`/api/servers/${serverId}/suggest_port`); + const response = await fetch(`/api/server/${serverId}/free-port`); const data = await response.json(); - if (data.port) { + if (data.success && data.port) { addPortRow(data.port); } else { - showNotification('No available ports found', 'warning'); + showNotification(data.error || 'No available ports found', 'warning'); } } catch (error) { console.error('Error generating random port:', error); diff --git a/app/templates/layout.html b/app/templates/layout.html index 4fcb611..195641a 100644 --- a/app/templates/layout.html +++ b/app/templates/layout.html @@ -325,7 +325,7 @@ - +