import logging import os from flask import Blueprint, request, jsonify logger = logging.getLogger('picell') bp = Blueprint('cells', __name__) @bp.route('/api/cells/invite', methods=['GET']) def get_cell_invite(): try: from app import cell_link_manager, config_manager identity = config_manager.configs.get('_identity', {}) cell_name = identity.get('cell_name', os.environ.get('CELL_NAME', 'mycell')) domain = identity.get('domain', os.environ.get('CELL_DOMAIN', 'cell')) return jsonify(cell_link_manager.generate_invite(cell_name, domain)) except Exception as e: logger.error(f"Error generating cell invite: {e}") return jsonify({'error': str(e)}), 500 @bp.route('/api/cells', methods=['GET']) def list_cell_connections(): try: from app import cell_link_manager return jsonify(cell_link_manager.list_connections()) except Exception as e: return jsonify({'error': str(e)}), 500 @bp.route('/api/cells', methods=['POST']) def add_cell_connection(): try: from app import cell_link_manager data = request.get_json(silent=True) if not data: return jsonify({'error': 'No data provided'}), 400 for field in ('cell_name', 'public_key', 'vpn_subnet', 'dns_ip', 'domain'): if field not in data: return jsonify({'error': f'Missing field: {field}'}), 400 link = cell_link_manager.add_connection(data) return jsonify({'message': f"Connected to cell '{data['cell_name']}'", 'link': link}), 201 except ValueError as e: return jsonify({'error': str(e)}), 400 except Exception as e: logger.error(f"Error adding cell connection: {e}") return jsonify({'error': str(e)}), 500 @bp.route('/api/cells/', methods=['DELETE']) def remove_cell_connection(cell_name): try: from app import cell_link_manager cell_link_manager.remove_connection(cell_name) return jsonify({'message': f"Cell '{cell_name}' disconnected"}) except ValueError as e: return jsonify({'error': str(e)}), 404 except Exception as e: logger.error(f"Error removing cell connection: {e}") return jsonify({'error': str(e)}), 500 @bp.route('/api/cells//status', methods=['GET']) def get_cell_connection_status(cell_name): try: from app import cell_link_manager return jsonify(cell_link_manager.get_connection_status(cell_name)) except ValueError as e: return jsonify({'error': str(e)}), 404 except Exception as e: return jsonify({'error': str(e)}), 500