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

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

// 查询 nodes 表
$sql = "SELECT id, name, location, status FROM nodes";
$result = $conn->query($sql);

// 检查是否有数据
if (!$result || $result->num_rows === 0) {
    http_response_code(404);
    echo json_encode(['error' => 'No nodes found']);
    exit();
}

// 获取节点列表
$nodes = [];
while ($row = $result->fetch_assoc()) {
    $nodes[] = $row;
}

// 关闭数据库连接
$conn->close();

// 返回 JSON 格式的节点列表
echo json_encode($nodes);