async def find_nodes(subnet: str):

    # First 4 bytes from IP
    subnet_part = ".".join(subnet.split(".")[0:3])

    my_ip = get_my_ip()

    for i in range(2, 5):
        logging.info(f'Adding node {subnet_part}.{i}')

        tested_ip = f'{subnet_part}.{i}'

        if tested_ip == my_ip:
            continue

        try:
            response = requests.post(f'http://{tested_ip}:5049/add?ip={my_ip}',
                                     timeout=1)
            response.raise_for_status()
        except requests.exceptions.ReadTimeout as e:
            logging.error(f'{e}')

        except requests.exceptions.ConnectTimeout as e:
            logging.error(f'{e}')

    return [{"status": "ok", "nodes": list(Globals.nodes)}]
async def add(ip: str):
    my_ip = get_my_ip()
    node_ip = ip

    if my_ip != node_ip and node_ip not in Globals.nodes:

        logging.info(f"Adding {node_ip}")
        Globals.nodes.add(node_ip)

        logging.info("All nodes in cluster")
        logging.info(Globals.nodes)

        return Response(
            json.dumps({
                "status": "ok",
                "nodes": list(Globals.nodes)
            }), 200)

    if node_ip == my_ip or node_ip in Globals.nodes:
        return Response(
            json.dumps({
                "status": "ok",
                "message": "already added",
                "nodes": list(Globals.nodes)
            }), 200)

    return {"status": "ok", "nodes": list(Globals.nodes)}
def get_next_node():

    # Get uniq and sorted nodes
    node_list: list = list(Globals.nodes)
    node_list.sort()

    my_ip: str = get_my_ip()
    my_index = node_list.index(my_ip)

    # Select first node in list
    if my_index >= (len(node_list) - 1):
        logging.info(f'Next node is {node_list[0]}')
        return node_list[0]
    # Select next node in list
    else:
        logging.info(f'Next node is {node_list[my_index+1]}')
        return node_list[my_index + 1]
def create_app(settings: Settings = get_settings()) -> FastAPI:
    # logger = logging.getLogger("Server")
    # logger.setLevel(logging.DEBUG)

    Globals.my_ip = get_my_ip()
    Globals.nodes = set()
    Globals.nodes.add(Globals.my_ip)

    set_logging()

    notifier.subscribe("received_message",
                       EventHandler.handle_received_message)
    notifier.subscribe("received_token", EventHandler.handle_received_token)
    notifier.subscribe("waiting_message", EventHandler.handle_waiting_message)
    notifier.subscribe("send_token", EventHandler.handle_send_token)

    # FAST API INIT
    app = FastAPI(title=settings.PROJECT_NAME, )

    # API ROUTER and endpoints
    app.include_router(create_router_endpoints())

    return app
async def ip(city):
    ip = get_my_ip()
    return {"status": "ok", "ip": ip}