<?php
header('Content-Type: application/json');
require '../nodes/config.php'; // 确保此文件中有数据库连接

// 检查连接是否成功
if ($conn->connect_error) {
    http_response_code(500);
    echo json_encode(['error' => 'Database connection failed: ' . $conn->connect_error]);
    exit();
}

// 查询活跃的服务器节点
$sql = "SELECT id, name, location, ip_address, port FROM mc_servers WHERE status = 'active'";
$result = $conn->query($sql);

$servers = [];

if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
        $servers[] = $row;
    }
}

$conn->close();

// 返回JSON格式的服务器列表
echo json_encode(['servers' => $servers]);