Mansurm · 3 days ago · 4 views
javascript Public

it is helps humanity

javascript
if ($requestCount >= 100) {
    jsonResponse(429, ['error' => 'Rate limit exceeded. Maximum 100 requests per hour.']);
}

// Increment counter
file_put_contents($rateLimitFile, json_encode(['hour' => $hourKey, 'count' => $requestCount + 1]));

// ── Route to handler ──────────────────────
$snippetId = $apiSnippetId ?? null; // Set by router

switch ($method) {
    case 'GET':
        if ($snippetId) {
            getSnippet($pdo, $apiUser, $snippetId);
        } else {
            listSnippets($pdo, $apiUser);
        }
        break;

    case 'POST':
        createSnippet($pdo, $apiUser);
        break;

    case 'PUT':
        if (!$snippetId) jsonResponse(400, ['error' => 'Snippet ID required for update.']);
        updateSnippet($pdo, $apiUser, $snippetId);
        break;

    case 'DELETE':
        if (!$snippetId) jsonResponse(400, ['error' => 'Snippet ID required for delete.']);
        deleteSnippet($pdo, $apiUser, $snippetId);
        break;

    default:
        jsonResponse(405, ['error' => 'Method not allowed.']);
}

// ── Handler Functions ─────────────────────

function listSnippets(PDO $pdo, array $user): void
{
    $stmt = $pdo->prepare('
        SELECT id, title, description, language, tags, is_public, view_count, created_at, updated_at
        FROM snippets WHERE user_id = :uid ORDER BY updated_at DESC
    ');
    $stmt->execute([':uid' => $user['id']]);
    jsonResponse(200, ['snippets' => $stmt->fetchAll()]);
}

function getSnippet(PDO $pdo, array $user, string $id): void
{
    $stmt = $pdo->prepare('SELECT * FROM snippets WHERE id = :id AND user_id = :uid');
    $stmt->execute([':id' => $id, ':uid' => $user['id']]);
    $snippet = $stmt->fetch();

    if (!$snippet) jsonResponse(404, ['error' => 'Snippet not found.']);
    jsonResponse(200, ['snippet' => $snippet]);
}

function createSnippet(PDO $pdo, array $user): void
{
    $input = json_decode(file_get_contents('php://input'), true);

    if (!$input) jsonResponse(400, ['error' => 'Invalid JSON body.']);

    $title    = trim($input['title'] ?? '');
    $code     = $input['code'] ?? '';
    $language = trim($input['language'] ?? '');

Embed this snippet

<iframe src="https://codevault-production-86f2.up.railway.app/snippet/47e66e87-141a-41c7-9263-519ab415cb2e?embed=1" width="100%" height="300" frameborder="0"></iframe>

More from Mansurm

gistfile1.rbx ruby
Yo Yo yep javascript
fbsdbzdbzd