PHP Classes

File: public/api.php

Recommend this page to a friend!
  Packages of Dwight José Trujillo Barco   Dynamic Values System   public/api.php   Download  
File: public/api.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Dynamic Values System
Manipulate values to be displayed dynamically
Author: By
Last change:
Date: 7 months ago
Size: 1,418 bytes
 

 

Contents

Class file image Download
<?php
declare(strict_types=1);

require_once __DIR__ . "/../vendor/autoload.php";

use DynamicValues\Core\ValueManager;
use DynamicValues\Cache\RedisCache;
use DynamicValues\Security\RateLimiter;
use DynamicValues\Security\InputValidator;
use DynamicValues\DataSources\RandomDataSource;

header("Content-Type: application/json");
header("Access-Control-Allow-Origin: *");

try {
    $cache = new RedisCache(["127.0.0.1"]);
    $rateLimiter = new RateLimiter($cache);
    $validator = new InputValidator();
    $manager = new ValueManager($cache);

    $manager->registerSource(new RandomDataSource("random", "Random Number", 1, 100, 30));

    $clientIp = $_SERVER["REMOTE_ADDR"] ?? "unknown";
    
    if (!$rateLimiter->checkLimit($clientIp)) {
        http_response_code(429);
        echo json_encode(["error" => "Rate limit exceeded"]);
        exit;
    }

    $specificKey = $_GET["key"] ?? null;
    
    if ($specificKey) {
        if (!$validator->validateKey($specificKey)) {
            http_response_code(400);
            echo json_encode(["error" => "Invalid key format"]);
            exit;
        }
        $response = [$specificKey => $manager->getValue($specificKey)];
    } else {
        $response = $manager->getAllValues();
    }

    echo json_encode($response, JSSN_PRETTY_PRINT);

} catch (\Throwable $e) {
    http_response_code(500);
    echo json_encode(["error" => "Internal error"]);
}