diff --git a/app/routes/__pycache__/api.cpython-313.pyc b/app/routes/__pycache__/api.cpython-313.pyc index 271063a..44c4252 100644 Binary files a/app/routes/__pycache__/api.cpython-313.pyc and b/app/routes/__pycache__/api.cpython-313.pyc differ diff --git a/app/routes/api.py b/app/routes/api.py index ff5bd31..da88c8b 100644 --- a/app/routes/api.py +++ b/app/routes/api.py @@ -8,6 +8,8 @@ import ipaddress from flask_wtf import CSRFProtect import markdown from datetime import datetime +from flask import flash +from app.utils.app_utils import validate_port_data bp = Blueprint("api", __name__, url_prefix="/api") csrf = CSRFProtect() @@ -284,61 +286,48 @@ def suggest_port(server_id): def add_app_port(app_id): """Add a port to an application""" app = App.query.get_or_404(app_id) - - # Accept both JSON and form data - if request.is_json: - data = request.json - else: - data = request.form - - port_number = data.get("port") - protocol = data.get("protocol", "TCP") - description = data.get("description", "") - - if not port_number: - return jsonify({"success": False, "error": "Port number is required"}), 400 - + try: - port_number = int(port_number) - - # Check if port already exists for this app - existing_port = Port.query.filter_by(app_id=app_id, number=port_number).first() + port_number = request.form.get("port_number") + protocol = request.form.get("protocol", "TCP") + description = request.form.get("description", "") + + # Validate port data + valid, clean_port, error = validate_port_data(port_number, protocol, description) + + if not valid: + return jsonify({"success": False, "error": error}), 400 + + # Check if port already exists + existing_port = Port.query.filter_by(app_id=app_id, port_number=clean_port).first() if existing_port: - return ( - jsonify( - { - "success": False, - "error": "Port already exists for this application", - } - ), - 400, - ) - + return jsonify({ + "success": False, + "error": f"Port {clean_port} already exists for this application" + }), 400 + # Create new port - port = Port( - number=port_number, - protocol=protocol, - description=description, + new_port = Port( app_id=app_id, + port_number=clean_port, + protocol=protocol, + description=description ) - - db.session.add(port) + db.session.add(new_port) db.session.commit() - - return jsonify( - { - "success": True, - "message": f"Port {port_number} added to {app.name}", - "port": { - "id": port.id, - "number": port.number, - "protocol": port.protocol, - "description": port.description, - }, + + flash(f"Port {clean_port}/{protocol} added successfully", "success") + return jsonify({ + "success": True, + "message": f"Port {clean_port}/{protocol} added successfully", + "port": { + "id": new_port.id, + "number": new_port.port_number, + "protocol": new_port.protocol, + "description": new_port.description } - ) - except ValueError: - return jsonify({"success": False, "error": "Invalid port number"}), 400 + }) + except Exception as e: db.session.rollback() return jsonify({"success": False, "error": str(e)}), 500 diff --git a/app/static/libs/tabler-icons/fonts/tabler-icons.ttf b/app/static/libs/tabler-icons/fonts/tabler-icons.ttf new file mode 100644 index 0000000..f42247e --- /dev/null +++ b/app/static/libs/tabler-icons/fonts/tabler-icons.ttf @@ -0,0 +1,1814 @@ + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +