aws-vis/layout.php

55 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
header('Content-Type: application/json');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
$layoutPath = __DIR__ . '/layout.json';
$method = $_SERVER['REQUEST_METHOD'] ?? 'GET';
if ($method === 'GET') {
if (is_file($layoutPath)) {
readfile($layoutPath);
} else {
echo json_encode(['nodes' => new stdClass()], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
exit;
}
if ($method === 'POST') {
$raw = file_get_contents('php://input');
if ($raw === false || trim($raw) === '') {
http_response_code(400);
echo json_encode(['error' => 'Empty request body']);
exit;
}
$decoded = json_decode($raw, true);
if (!is_array($decoded)) {
http_response_code(400);
echo json_encode(['error' => 'Invalid JSON payload']);
exit;
}
$encoded = json_encode($decoded, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
if ($encoded === false) {
http_response_code(500);
echo json_encode(['error' => 'Failed to encode layout']);
exit;
}
$result = @file_put_contents($layoutPath, $encoded . PHP_EOL, LOCK_EX);
if ($result === false) {
http_response_code(500);
echo json_encode(['error' => 'Failed to write layout file']);
exit;
}
echo json_encode(['status' => 'ok']);
exit;
}
http_response_code(405);
header('Allow: GET, POST');
echo json_encode(['error' => 'Method not allowed']);