feat: initial commit

This commit is contained in:
Cory Dransfeldt 2025-03-27 16:46:02 -07:00
commit e214116e40
No known key found for this signature in database
253 changed files with 17406 additions and 0 deletions

View file

@ -0,0 +1,72 @@
<?php
namespace App\Classes;
use GuzzleHttp\Client;
require __DIR__ . "/../../vendor/autoload.php";
abstract class ApiHandler
{
protected string $postgrestUrl;
protected string $postgrestApiKey;
public function __construct()
{
$this->loadEnvironment();
}
private function loadEnvironment(): void
{
$this->postgrestUrl =
$_ENV["POSTGREST_URL"] ?? getenv("POSTGREST_URL") ?: "";
$this->postgrestApiKey =
$_ENV["POSTGREST_API_KEY"] ?? getenv("POSTGREST_API_KEY") ?: "";
}
protected function ensureCliAccess(): void
{
if (php_sapi_name() !== "cli" && $_SERVER["REQUEST_METHOD"] !== "POST") {
$this->redirectNotFound();
}
}
protected function redirectNotFound(): void
{
header("Location: /404", true, 302);
exit();
}
protected function fetchFromPostgREST(
string $endpoint,
string $query = "",
string $method = "GET",
?array $body = null
): array {
$url = "{$this->postgrestUrl}/{$endpoint}?{$query}";
$options = [
"headers" => [
"Content-Type" => "application/json",
"Authorization" => "Bearer {$this->postgrestApiKey}",
],
];
if ($method === "POST" && $body) $options["json"] = $body;
$response = (new Client())->request($method, $url, $options);
return json_decode($response->getBody(), true) ?? [];
}
protected function sendResponse(string $message, int $statusCode): void
{
http_response_code($statusCode);
header("Content-Type: application/json");
echo json_encode(["message" => $message]);
exit();
}
protected function sendErrorResponse(string $message, int $statusCode): void
{
$this->sendResponse($message, $statusCode);
}
}

129
api/Classes/BaseHandler.php Normal file
View file

@ -0,0 +1,129 @@
<?php
namespace App\Classes;
require __DIR__ . "/../../vendor/autoload.php";
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
abstract class BaseHandler
{
protected string $postgrestUrl;
protected string $postgrestApiKey;
protected ?\Redis $cache = null;
public function __construct()
{
$this->loadEnvironment();
}
private function loadEnvironment(): void
{
$this->postgrestUrl =
$_ENV["POSTGREST_URL"] ?? getenv("POSTGREST_URL") ?: "";
$this->postgrestApiKey =
$_ENV["POSTGREST_API_KEY"] ?? getenv("POSTGREST_API_KEY") ?: "";
}
protected function makeRequest(
string $method,
string $endpoint,
array $options = []
): array {
$client = new Client();
$url = rtrim($this->postgrestUrl, "/") . "/" . ltrim($endpoint, "/");
try {
$response = $client->request(
$method,
$url,
array_merge($options, [
"headers" => [
"Authorization" => "Bearer {$this->postgrestApiKey}",
"Content-Type" => "application/json",
],
])
);
$responseBody = $response->getBody()->getContents();
if (empty($responseBody)) return [];
$responseData = json_decode($responseBody, true);
if (json_last_error() !== JSON_ERROR_NONE) throw new \Exception("Invalid JSON response: {$responseBody}");
return $responseData;
} catch (RequestException $e) {
$response = $e->getResponse();
$statusCode = $response ? $response->getStatusCode() : "N/A";
$responseBody = $response
? $response->getBody()->getContents()
: "No response body";
throw new \Exception(
"Request to {$url} failed with status {$statusCode}. Response: {$responseBody}"
);
} catch (\Exception $e) {
throw new \Exception("Request to {$url} failed: " . $e->getMessage());
}
}
protected function sendResponse(array $data, int $statusCode = 200): void
{
http_response_code($statusCode);
header("Content-Type: application/json");
echo json_encode($data);
exit();
}
protected function sendErrorResponse(
string $message,
int $statusCode = 500
): void {
$this->sendResponse(["error" => $message], $statusCode);
}
protected function fetchFromApi(string $endpoint, string $query): array
{
$client = new Client();
$url =
rtrim($this->postgrestUrl, "/") .
"/" .
ltrim($endpoint, "/") .
"?" .
$query;
try {
$response = $client->request("GET", $url, [
"headers" => [
"Content-Type" => "application/json",
"Authorization" => "Bearer {$this->postgrestApiKey}",
],
]);
if ($response->getStatusCode() !== 200) throw new Exception("API call to {$url} failed with status code " . $response->getStatusCode());
return json_decode($response->getBody(), true);
} catch (RequestException $e) {
throw new Exception("Error fetching from API: " . $e->getMessage());
}
}
protected function initializeCache(): void
{
if (class_exists("Redis")) {
$redis = new \Redis();
try {
$redis->connect("127.0.0.1", 6379);
$this->cache = $redis;
} catch (Exception $e) {
error_log("Redis connection failed: " . $e->getMessage());
$this->cache = null;
}
} else {
$this->cache = null;
}
}
}