feat(*.php): refactor, clean up and abstract dynamic page code
This commit is contained in:
parent
2f9038dccb
commit
60be0ed01d
30 changed files with 318 additions and 401 deletions
19
api/Classes/ArtistFetcher.php
Normal file
19
api/Classes/ArtistFetcher.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
class ArtistFetcher extends PageFetcher
|
||||
{
|
||||
public function fetch(string $url): ?array
|
||||
{
|
||||
$cacheKey = "artist_" . md5($url);
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
if ($cached) return $cached;
|
||||
|
||||
$artist = $this->fetchSingleFromApi("optimized_artists", $url);
|
||||
if (!$artist) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $artist);
|
||||
return $artist;
|
||||
}
|
||||
}
|
19
api/Classes/BookFetcher.php
Normal file
19
api/Classes/BookFetcher.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
class BookFetcher extends PageFetcher
|
||||
{
|
||||
public function fetch(string $url): ?array
|
||||
{
|
||||
$cacheKey = "book_" . md5($url);
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
if ($cached) return $cached;
|
||||
|
||||
$book = $this->fetchSingleFromApi("optimized_books", $url);
|
||||
if (!$book) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $book);
|
||||
return $book;
|
||||
}
|
||||
}
|
19
api/Classes/GenreFetcher.php
Normal file
19
api/Classes/GenreFetcher.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
class GenreFetcher extends PageFetcher
|
||||
{
|
||||
public function fetch(string $url): ?array
|
||||
{
|
||||
$cacheKey = "genre_" . md5($url);
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
if ($cached) return $cached;
|
||||
|
||||
$genre = $this->fetchSingleFromApi("optimized_genres", $url);
|
||||
if (!$genre) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $genre);
|
||||
return $genre;
|
||||
}
|
||||
}
|
19
api/Classes/MovieFetcher.php
Normal file
19
api/Classes/MovieFetcher.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
class MovieFetcher extends PageFetcher
|
||||
{
|
||||
public function fetch(string $url): ?array
|
||||
{
|
||||
$cacheKey = "movie_" . md5($url);
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
if ($cached) return $cached;
|
||||
|
||||
$movie = $this->fetchSingleFromApi("optimized_movies", $url);
|
||||
if (!$movie) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $movie);
|
||||
return $movie;
|
||||
}
|
||||
}
|
29
api/Classes/PageFetcher.php
Normal file
29
api/Classes/PageFetcher.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
use App\Classes\BaseHandler;
|
||||
|
||||
abstract class PageFetcher extends BaseHandler
|
||||
{
|
||||
protected function cacheGet(string $key): mixed
|
||||
{
|
||||
return $this->cache && $this->cache->exists($key) ? json_decode($this->cache->get($key), true) : null;
|
||||
}
|
||||
|
||||
protected function cacheSet(string $key, mixed $value, int $ttl = 3600): void
|
||||
{
|
||||
if ($this->cache) $this->cache->setex($key, $ttl, json_encode($value));
|
||||
}
|
||||
|
||||
protected function fetchSingleFromApi(string $endpoint, string $url): ?array
|
||||
{
|
||||
$data = $this->fetchFromApi($endpoint, "url=eq./{$url}");
|
||||
return $data[0] ?? null;
|
||||
}
|
||||
|
||||
protected function fetchPostRpc(string $endpoint, array $body): ?array
|
||||
{
|
||||
return $this->makeRequest("POST", $endpoint, ['json' => $body]);
|
||||
}
|
||||
}
|
19
api/Classes/ShowFetcher.php
Normal file
19
api/Classes/ShowFetcher.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
class ShowFetcher extends PageFetcher
|
||||
{
|
||||
public function fetch(string $url): ?array
|
||||
{
|
||||
$cacheKey = "show_" . md5($url);
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
if ($cached) return $cached;
|
||||
|
||||
$show = $this->fetchSingleFromApi("optimized_shows", $url);
|
||||
if (!$show) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $show);
|
||||
return $show;
|
||||
}
|
||||
}
|
26
api/Classes/TagFetcher.php
Normal file
26
api/Classes/TagFetcher.php
Normal file
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace App\Classes;
|
||||
|
||||
class TagFetcher extends PageFetcher
|
||||
{
|
||||
public function fetch(string $tag, int $page = 1, int $pageSize = 20): ?array
|
||||
{
|
||||
$offset = ($page - 1) * $pageSize;
|
||||
$cacheKey = "tag_" . md5("{$tag}_{$page}");
|
||||
|
||||
$cached = $this->cacheGet($cacheKey);
|
||||
if ($cached) return $cached;
|
||||
|
||||
$results = $this->fetchPostRpc("rpc/get_tagged_content", [
|
||||
"tag_query" => $tag,
|
||||
"page_size" => $pageSize,
|
||||
"page_offset" => $offset
|
||||
]);
|
||||
|
||||
if (!$results || count($results) === 0) return null;
|
||||
|
||||
$this->cacheSet($cacheKey, $results);
|
||||
return $results;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
require __DIR__ . "/Classes/ApiHandler.php";
|
||||
require __DIR__ . "/../vendor/autoload.php";
|
||||
require __DIR__ . "/Utils/init.php";
|
||||
|
||||
use App\Classes\ApiHandler;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
require __DIR__ . "/Classes/ApiHandler.php";
|
||||
require __DIR__ . "/../vendor/autoload.php";
|
||||
|
||||
use App\Classes\ApiHandler;
|
||||
use GuzzleHttp\Client;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
require __DIR__ . "/Classes/BaseHandler.php";
|
||||
require __DIR__ . "/../vendor/autoload.php";
|
||||
|
||||
use App\Classes\BaseHandler;
|
||||
use GuzzleHttp\Client;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
require __DIR__ . "/Classes/ApiHandler.php";
|
||||
require __DIR__ . "/../vendor/autoload.php";
|
||||
|
||||
use App\Classes\ApiHandler;
|
||||
use GuzzleHttp\Client;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<?php
|
||||
|
||||
require __DIR__ . "/../vendor/autoload.php";
|
||||
require __DIR__ . '/../server/utils/init.php';
|
||||
require __DIR__ . "/Classes/BaseHandler.php";
|
||||
|
||||
use App\Classes\BaseHandler;
|
||||
use GuzzleHttp\Client;
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace App\Handlers;
|
||||
|
||||
require __DIR__ . "/Classes/BaseHandler.php";
|
||||
require __DIR__ . "/../vendor/autoload.php";
|
||||
|
||||
use App\Classes\BaseHandler;
|
||||
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
<?php
|
||||
|
||||
use App\Classes\BaseHandler;
|
||||
|
||||
require __DIR__ . "/../vendor/autoload.php";
|
||||
require __DIR__ . '/../server/utils/init.php';
|
||||
require __DIR__ . "/Classes/BaseHandler.php";
|
||||
|
||||
use App\Classes\BaseHandler;
|
||||
|
||||
class QueryHandler extends BaseHandler
|
||||
{
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace App\Handlers;
|
||||
|
||||
require __DIR__ . "/Classes/ApiHandler.php";
|
||||
require __DIR__ . "/../vendor/autoload.php";
|
||||
require __DIR__ . "/Utils/init.php";
|
||||
|
||||
use App\Classes\ApiHandler;
|
||||
|
|
|
@ -1,8 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace App\Handlers;
|
||||
|
||||
require __DIR__ . "/Classes/BaseHandler.php";
|
||||
require __DIR__ . "/../vendor/autoload.php";
|
||||
|
||||
use App\Classes\BaseHandler;
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
require __DIR__ . "/Classes/ApiHandler.php";
|
||||
require __DIR__ . "/../vendor/autoload.php";
|
||||
|
||||
use App\Classes\ApiHandler;
|
||||
use GuzzleHttp\Client;
|
||||
|
|
42
api/umami.php
Normal file
42
api/umami.php
Normal file
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
$remoteUrl = 'https://stats.apps.coryd.dev/script.js';
|
||||
$cacheKey = 'remote_stats_script';
|
||||
$ttl = 3600;
|
||||
$js = null;
|
||||
$code = 200;
|
||||
$redis = null;
|
||||
|
||||
try {
|
||||
if (extension_loaded('redis')) {
|
||||
$redis = new Redis();
|
||||
$redis->connect('127.0.0.1', 6379);
|
||||
|
||||
if ($redis->exists($cacheKey)) $js = $redis->get($cacheKey);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
error_log("Redis unavailable: " . $e->getMessage());
|
||||
}
|
||||
|
||||
if (!is_string($js)) {
|
||||
$ch = curl_init($remoteUrl);
|
||||
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HEADER, false);
|
||||
|
||||
$js = curl_exec($ch);
|
||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
if ($redis && $code === 200 && $js) $redis->setex($cacheKey, $ttl, $js);
|
||||
}
|
||||
|
||||
if (!is_string($js) || trim($js) === '') {
|
||||
$js = '// Failed to fetch remote script';
|
||||
$code = 502;
|
||||
}
|
||||
|
||||
http_response_code($code);
|
||||
header('Content-Type: application/javascript; charset=UTF-8');
|
||||
header('Cache-Control: public, max-age=60');
|
||||
echo $js;
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
require __DIR__ . "/Classes/ApiHandler.php";
|
||||
require __DIR__ . "/../vendor/autoload.php";
|
||||
|
||||
use App\Classes\ApiHandler;
|
||||
use GuzzleHttp\Client;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue