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
|
<?php
|
||||||
|
|
||||||
require __DIR__ . "/Classes/ApiHandler.php";
|
require __DIR__ . "/../vendor/autoload.php";
|
||||||
require __DIR__ . "/Utils/init.php";
|
require __DIR__ . "/Utils/init.php";
|
||||||
|
|
||||||
use App\Classes\ApiHandler;
|
use App\Classes\ApiHandler;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
require __DIR__ . "/Classes/ApiHandler.php";
|
require __DIR__ . "/../vendor/autoload.php";
|
||||||
|
|
||||||
use App\Classes\ApiHandler;
|
use App\Classes\ApiHandler;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
require __DIR__ . "/Classes/BaseHandler.php";
|
require __DIR__ . "/../vendor/autoload.php";
|
||||||
|
|
||||||
use App\Classes\BaseHandler;
|
use App\Classes\BaseHandler;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
require __DIR__ . "/Classes/ApiHandler.php";
|
require __DIR__ . "/../vendor/autoload.php";
|
||||||
|
|
||||||
use App\Classes\ApiHandler;
|
use App\Classes\ApiHandler;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
require __DIR__ . "/../vendor/autoload.php";
|
||||||
require __DIR__ . '/../server/utils/init.php';
|
require __DIR__ . '/../server/utils/init.php';
|
||||||
require __DIR__ . "/Classes/BaseHandler.php";
|
|
||||||
|
|
||||||
use App\Classes\BaseHandler;
|
use App\Classes\BaseHandler;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Handlers;
|
require __DIR__ . "/../vendor/autoload.php";
|
||||||
|
|
||||||
require __DIR__ . "/Classes/BaseHandler.php";
|
|
||||||
|
|
||||||
use App\Classes\BaseHandler;
|
use App\Classes\BaseHandler;
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Classes\BaseHandler;
|
require __DIR__ . "/../vendor/autoload.php";
|
||||||
|
|
||||||
require __DIR__ . '/../server/utils/init.php';
|
require __DIR__ . '/../server/utils/init.php';
|
||||||
require __DIR__ . "/Classes/BaseHandler.php";
|
|
||||||
|
use App\Classes\BaseHandler;
|
||||||
|
|
||||||
class QueryHandler extends BaseHandler
|
class QueryHandler extends BaseHandler
|
||||||
{
|
{
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Handlers;
|
require __DIR__ . "/../vendor/autoload.php";
|
||||||
|
|
||||||
require __DIR__ . "/Classes/ApiHandler.php";
|
|
||||||
require __DIR__ . "/Utils/init.php";
|
require __DIR__ . "/Utils/init.php";
|
||||||
|
|
||||||
use App\Classes\ApiHandler;
|
use App\Classes\ApiHandler;
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace App\Handlers;
|
require __DIR__ . "/../vendor/autoload.php";
|
||||||
|
|
||||||
require __DIR__ . "/Classes/BaseHandler.php";
|
|
||||||
|
|
||||||
use App\Classes\BaseHandler;
|
use App\Classes\BaseHandler;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
require __DIR__ . "/Classes/ApiHandler.php";
|
require __DIR__ . "/../vendor/autoload.php";
|
||||||
|
|
||||||
use App\Classes\ApiHandler;
|
use App\Classes\ApiHandler;
|
||||||
use GuzzleHttp\Client;
|
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
|
<?php
|
||||||
|
|
||||||
require __DIR__ . "/Classes/ApiHandler.php";
|
require __DIR__ . "/../vendor/autoload.php";
|
||||||
|
|
||||||
use App\Classes\ApiHandler;
|
use App\Classes\ApiHandler;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"App\\": "src/"
|
"App\\Classes\\": "api/Classes/"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"config": {
|
"config": {
|
||||||
|
|
10
package-lock.json
generated
10
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "4.3.3",
|
"version": "5.0.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "4.3.3",
|
"version": "5.0.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"html-minifier-terser": "7.2.0",
|
"html-minifier-terser": "7.2.0",
|
||||||
|
@ -1737,9 +1737,9 @@
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/electron-to-chromium": {
|
"node_modules/electron-to-chromium": {
|
||||||
"version": "1.5.152",
|
"version": "1.5.153",
|
||||||
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.152.tgz",
|
"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.153.tgz",
|
||||||
"integrity": "sha512-xBOfg/EBaIlVsHipHl2VdTPJRSvErNUaqW8ejTq5OlOlIYx1wOllCHsAvAIrr55jD1IYEfdR86miUEt8H5IeJg==",
|
"integrity": "sha512-4bwluTFwjXZ0/ei1qDpHDGzVveuBfx4wiZ9VQ8j/30+T2JxSF2TfZ00d1X+wNMeDyUdZXgLkJFbarJdAMtd+/w==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "ISC"
|
"license": "ISC"
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "4.3.3",
|
"version": "5.0.0",
|
||||||
"description": "The source for my personal site. Built using 11ty (and other tools).",
|
"description": "The source for my personal site. Built using 11ty (and other tools).",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|
|
@ -1,84 +1,35 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
require __DIR__ . "/../../vendor/autoload.php";
|
require __DIR__ . "/../../vendor/autoload.php";
|
||||||
require __DIR__ . "/../../server/utils/init.php";
|
require __DIR__ . "/../../server/utils/init.php";
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use App\Classes\ArtistFetcher;
|
||||||
use voku\helper\HtmlMin;
|
use voku\helper\HtmlMin;
|
||||||
|
|
||||||
$requestUri = $_SERVER["REQUEST_URI"];
|
$requestUri = $_SERVER["REQUEST_URI"];
|
||||||
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
|
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
|
||||||
$postgrestUrl = $_ENV["POSTGREST_URL"] ?? getenv("POSTGREST_URL");
|
|
||||||
$postgrestApiKey = $_ENV["POSTGREST_API_KEY"] ?? getenv("POSTGREST_API_KEY");
|
|
||||||
|
|
||||||
if (strpos($url, "music/artists/") !== 0) {
|
if (strpos($url, "music/artists/") !== 0) {
|
||||||
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
$cacheKey = "artist_" . md5($url);
|
$fetcher = new ArtistFetcher();
|
||||||
$artist = null;
|
$artist = $fetcher->fetch($url);
|
||||||
$useRedis = false;
|
|
||||||
|
|
||||||
try {
|
if (!$artist) {
|
||||||
if (extension_loaded('redis')) {
|
|
||||||
$redis = new Redis();
|
|
||||||
$redis->connect('127.0.0.1', 6379);
|
|
||||||
$useRedis = true;
|
|
||||||
}
|
|
||||||
} catch (Exception $e) {
|
|
||||||
error_log("Redis not available: " . $e->getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($useRedis && $redis->exists($cacheKey)) {
|
|
||||||
$artist = json_decode($redis->get($cacheKey), true);
|
|
||||||
} else {
|
|
||||||
$apiUrl = "$postgrestUrl/optimized_artists?url=eq./" . $url;
|
|
||||||
$client = new Client();
|
|
||||||
|
|
||||||
try {
|
|
||||||
$response = $client->request("GET", $apiUrl, [
|
|
||||||
"headers" => [
|
|
||||||
"Accept" => "application/json",
|
|
||||||
"Authorization" => "Bearer {$postgrestApiKey}",
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$artistData = json_decode($response->getBody(), true);
|
|
||||||
|
|
||||||
if (!$artistData) {
|
|
||||||
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
$artist = $artistData[0];
|
|
||||||
|
|
||||||
if ($useRedis) {
|
|
||||||
$redis->setex($cacheKey, 3600, json_encode($artist));
|
|
||||||
}
|
|
||||||
} catch (Exception $e) {
|
|
||||||
error_log($e->getMessage());
|
|
||||||
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$artist["description"] = parseMarkdown($artist["description"]);
|
$artist["description"] = parseMarkdown($artist["description"]);
|
||||||
$pageTitle = htmlspecialchars(
|
$pageTitle = htmlspecialchars("Artists • " . $artist["name"], ENT_QUOTES, "UTF-8");
|
||||||
"Artists • " . $artist["name"],
|
$pageDescription = truncateText(htmlspecialchars(strip_tags($artist["description"]), ENT_QUOTES, "UTF-8"), 250);
|
||||||
ENT_QUOTES,
|
|
||||||
"UTF-8"
|
|
||||||
);
|
|
||||||
$pageDescription = truncateText(htmlspecialchars(
|
|
||||||
strip_tags($artist["description"]),
|
|
||||||
ENT_QUOTES,
|
|
||||||
"UTF-8"
|
|
||||||
), 250);
|
|
||||||
$ogImage = htmlspecialchars($artist["image"], ENT_QUOTES, "UTF-8");
|
$ogImage = htmlspecialchars($artist["image"], ENT_QUOTES, "UTF-8");
|
||||||
$fullUrl = "https://www.coryd.dev" . $requestUri;
|
$fullUrl = "https://www.coryd.dev" . $requestUri;
|
||||||
$oembedUrl = "https://www.coryd.dev/oembed" . $requestUri;
|
$oembedUrl = "https://www.coryd.dev/oembed" . $requestUri;
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
|
|
||||||
header("Cache-Control: public, max-age=3600");
|
header("Cache-Control: public, max-age=3600");
|
||||||
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT");
|
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT");
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
require __DIR__ . "/../vendor/autoload.php";
|
require __DIR__ . "/../vendor/autoload.php";
|
||||||
require __DIR__ . "/../server/utils/init.php";
|
require __DIR__ . "/../server/utils/init.php";
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use App\Classes\BookFetcher;
|
||||||
use voku\helper\HtmlMin;
|
use voku\helper\HtmlMin;
|
||||||
|
|
||||||
$requestUri = $_SERVER["REQUEST_URI"];
|
$requestUri = $_SERVER["REQUEST_URI"];
|
||||||
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
|
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
|
||||||
$postgrestUrl = $_ENV["POSTGREST_URL"] ?? getenv("POSTGREST_URL");
|
|
||||||
$postgrestApiKey = $_ENV["POSTGREST_API_KEY"] ?? getenv("POSTGREST_API_KEY");
|
|
||||||
|
|
||||||
if (preg_match('/^books\/years\/(\d{4})$/', $url, $matches)) {
|
if (preg_match('/^books\/years\/(\d{4})$/', $url, $matches)) {
|
||||||
$year = $matches[1];
|
$year = $matches[1];
|
||||||
|
@ -23,13 +22,9 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($url === "books/years") {
|
if ($url === "books/years" || $url === "books") {
|
||||||
echo file_get_contents(__DIR__ . "/../404/index.html");
|
$indexPath = $url === "books" ? "index.html" : __DIR__ . "/../404/index.html";
|
||||||
exit();
|
readfile($indexPath);
|
||||||
}
|
|
||||||
|
|
||||||
if ($url === "books") {
|
|
||||||
readfile("index.html");
|
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,70 +33,22 @@
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
$cacheKey = "book_" . md5($url);
|
$fetcher = new BookFetcher();
|
||||||
|
$book = $fetcher->fetch($url);
|
||||||
|
|
||||||
$book = null;
|
if (!$book) {
|
||||||
$useRedis = false;
|
|
||||||
try {
|
|
||||||
if (extension_loaded('redis')) {
|
|
||||||
$redis = new Redis();
|
|
||||||
$redis->connect('127.0.0.1', 6379);
|
|
||||||
$useRedis = true;
|
|
||||||
}
|
|
||||||
} catch (Exception $e) {
|
|
||||||
error_log("Redis not available: " . $e->getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($useRedis && $redis->exists($cacheKey)) {
|
|
||||||
$book = json_decode($redis->get($cacheKey), true);
|
|
||||||
} else {
|
|
||||||
$apiUrl = "$postgrestUrl/optimized_books?url=eq./" . $url;
|
|
||||||
$client = new Client();
|
|
||||||
|
|
||||||
try {
|
|
||||||
$response = $client->request("GET", $apiUrl, [
|
|
||||||
"headers" => [
|
|
||||||
"Accept" => "application/json",
|
|
||||||
"Authorization" => "Bearer {$postgrestApiKey}",
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$bookData = json_decode($response->getBody(), true);
|
|
||||||
|
|
||||||
if (!$bookData) {
|
|
||||||
echo file_get_contents(__DIR__ . "/../404/index.html");
|
echo file_get_contents(__DIR__ . "/../404/index.html");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
$book = $bookData[0];
|
|
||||||
|
|
||||||
if ($useRedis) {
|
|
||||||
$redis->setex($cacheKey, 3600, json_encode($book));
|
|
||||||
}
|
|
||||||
} catch (Exception $e) {
|
|
||||||
error_log($e->getMessage());
|
|
||||||
echo file_get_contents(__DIR__ . "/../404/index.html");
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$book["description"] = parseMarkdown($book["description"]);
|
$book["description"] = parseMarkdown($book["description"]);
|
||||||
$pageTitle = htmlspecialchars(
|
$pageTitle = htmlspecialchars("Books • {$book["title"]} by {$book["author"]}", ENT_QUOTES, "UTF-8");
|
||||||
"Books • " . $book["title"] . " by " . $book["author"],
|
$pageDescription = truncateText(htmlspecialchars(strip_tags($book["description"]), ENT_QUOTES, "UTF-8"), 250);
|
||||||
ENT_QUOTES,
|
|
||||||
"UTF-8"
|
|
||||||
);
|
|
||||||
$pageDescription = truncateText(htmlspecialchars(
|
|
||||||
strip_tags($book["description"]),
|
|
||||||
ENT_QUOTES,
|
|
||||||
"UTF-8"
|
|
||||||
), 250);
|
|
||||||
$ogImage = htmlspecialchars($book["image"], ENT_QUOTES, "UTF-8");
|
$ogImage = htmlspecialchars($book["image"], ENT_QUOTES, "UTF-8");
|
||||||
$fullUrl = "https://www.coryd.dev" . $requestUri;
|
$fullUrl = "https://www.coryd.dev" . $requestUri;
|
||||||
$oembedUrl = "https://www.coryd.dev/oembed" . $requestUri;
|
$oembedUrl = "https://www.coryd.dev/oembed" . $requestUri;
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
|
|
||||||
header("Cache-Control: public, max-age=3600");
|
header("Cache-Control: public, max-age=3600");
|
||||||
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT");
|
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT");
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -1,75 +1,37 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
require __DIR__ . "/../../vendor/autoload.php";
|
require __DIR__ . "/../../vendor/autoload.php";
|
||||||
require __DIR__ . "/../../server/utils/init.php";
|
require __DIR__ . "/../../server/utils/init.php";
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use App\Classes\GenreFetcher;
|
||||||
use voku\helper\HtmlMin;
|
use voku\helper\HtmlMin;
|
||||||
|
|
||||||
$requestUri = $_SERVER["REQUEST_URI"];
|
$requestUri = $_SERVER["REQUEST_URI"];
|
||||||
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
|
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
|
||||||
$postgrestUrl = $_ENV["POSTGREST_URL"] ?? getenv("POSTGREST_URL");
|
|
||||||
$postgrestApiKey = $_ENV["POSTGREST_API_KEY"] ?? getenv("POSTGREST_API_KEY");
|
|
||||||
|
|
||||||
if (!preg_match('/^music\/genres\/[\w-]+$/', $url)) {
|
if (!preg_match('/^music\/genres\/[\w-]+$/', $url)) {
|
||||||
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
$cacheKey = "genre_" . md5($url);
|
$fetcher = new GenreFetcher();
|
||||||
$genre = null;
|
$genre = $fetcher->fetch($url);
|
||||||
$useRedis = false;
|
|
||||||
|
|
||||||
try {
|
if (!$genre) {
|
||||||
if (extension_loaded('redis')) {
|
|
||||||
$redis = new Redis();
|
|
||||||
$redis->connect('127.0.0.1', 6379);
|
|
||||||
$useRedis = true;
|
|
||||||
}
|
|
||||||
} catch (Exception $e) {
|
|
||||||
error_log("Redis not available: " . $e->getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($useRedis && $redis->exists($cacheKey)) {
|
|
||||||
$genre = json_decode($redis->get($cacheKey), true);
|
|
||||||
} else {
|
|
||||||
$apiUrl = "$postgrestUrl/optimized_genres?url=eq./" . $url;
|
|
||||||
$client = new Client();
|
|
||||||
|
|
||||||
try {
|
|
||||||
$response = $client->request("GET", $apiUrl, [
|
|
||||||
"headers" => [
|
|
||||||
"Accept" => "application/json",
|
|
||||||
"Authorization" => "Bearer {$postgrestApiKey}",
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$genreData = json_decode($response->getBody(), true);
|
|
||||||
|
|
||||||
if (!$genreData) {
|
|
||||||
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
$genre = $genreData[0];
|
|
||||||
|
|
||||||
if ($useRedis) {
|
|
||||||
$redis->setex($cacheKey, 3600, json_encode($genre));
|
|
||||||
}
|
|
||||||
} catch (Exception $e) {
|
|
||||||
error_log($e->getMessage());
|
|
||||||
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$pageTitle = htmlspecialchars("Genres • " . $genre["name"], ENT_QUOTES, "UTF-8");
|
$pageTitle = htmlspecialchars("Genres • " . $genre["name"], ENT_QUOTES, "UTF-8");
|
||||||
$pageDescription = truncateText(htmlspecialchars(strip_tags($genre["description"]), ENT_QUOTES, "UTF-8"), 250);
|
$pageDescription = truncateText(
|
||||||
$ogImage = htmlspecialchars($genre["artists"][0]["image"], ENT_QUOTES, "UTF-8");
|
htmlspecialchars(strip_tags($genre["description"]), ENT_QUOTES, "UTF-8"),
|
||||||
|
250
|
||||||
|
);
|
||||||
|
$ogImage = htmlspecialchars($genre["artists"][0]["image"] ?? "", ENT_QUOTES, "UTF-8");
|
||||||
$fullUrl = "https://www.coryd.dev" . $requestUri;
|
$fullUrl = "https://www.coryd.dev" . $requestUri;
|
||||||
$oembedUrl = "https://www.coryd.dev/oembed" . $requestUri;
|
$oembedUrl = "https://www.coryd.dev/oembed" . $requestUri;
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
|
|
||||||
header("Cache-Control: public, max-age=3600");
|
header("Cache-Control: public, max-age=3600");
|
||||||
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT");
|
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT");
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -1,84 +1,35 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
require __DIR__ . "/../../vendor/autoload.php";
|
require __DIR__ . "/../../vendor/autoload.php";
|
||||||
require __DIR__ . "/../../server/utils/init.php";
|
require __DIR__ . "/../../server/utils/init.php";
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use App\Classes\MovieFetcher;
|
||||||
use voku\helper\HtmlMin;
|
use voku\helper\HtmlMin;
|
||||||
|
|
||||||
$requestUri = $_SERVER["REQUEST_URI"];
|
$requestUri = $_SERVER["REQUEST_URI"];
|
||||||
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
|
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
|
||||||
$postgrestUrl = $_ENV["POSTGREST_URL"] ?? getenv("POSTGREST_URL");
|
|
||||||
$postgrestApiKey = $_ENV["POSTGREST_API_KEY"] ?? getenv("POSTGREST_API_KEY");
|
|
||||||
|
|
||||||
if (!preg_match('/^watching\/movies\/[\w-]+$/', $url)) {
|
if (!preg_match('/^watching\/movies\/[\w-]+$/', $url)) {
|
||||||
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
$cacheKey = "movie_" . md5($url);
|
$fetcher = new MovieFetcher();
|
||||||
$movie = null;
|
$movie = $fetcher->fetch($url);
|
||||||
$useRedis = false;
|
|
||||||
|
|
||||||
try {
|
if (!$movie) {
|
||||||
if (extension_loaded('redis')) {
|
|
||||||
$redis = new Redis();
|
|
||||||
$redis->connect('127.0.0.1', 6379);
|
|
||||||
$useRedis = true;
|
|
||||||
}
|
|
||||||
} catch (Exception $e) {
|
|
||||||
error_log("Redis not available: " . $e->getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($useRedis && $redis->exists($cacheKey)) {
|
|
||||||
$movie = json_decode($redis->get($cacheKey), true);
|
|
||||||
} else {
|
|
||||||
$apiUrl = "$postgrestUrl/optimized_movies?url=eq./" . $url;
|
|
||||||
$client = new Client();
|
|
||||||
|
|
||||||
try {
|
|
||||||
$response = $client->request("GET", $apiUrl, [
|
|
||||||
"headers" => [
|
|
||||||
"Accept" => "application/json",
|
|
||||||
"Authorization" => "Bearer {$postgrestApiKey}",
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$movieData = json_decode($response->getBody(), true);
|
|
||||||
|
|
||||||
if (!$movieData) {
|
|
||||||
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
$movie = $movieData[0];
|
|
||||||
|
|
||||||
if ($useRedis) {
|
|
||||||
$redis->setex($cacheKey, 3600, json_encode($movie));
|
|
||||||
}
|
|
||||||
} catch (Exception $e) {
|
|
||||||
error_log($e->getMessage());
|
|
||||||
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$movie["description"] = parseMarkdown($movie["description"]);
|
$movie["description"] = parseMarkdown($movie["description"]);
|
||||||
$pageTitle = htmlspecialchars(
|
$pageTitle = htmlspecialchars("Movies • " . $movie["title"], ENT_QUOTES, "UTF-8");
|
||||||
"Movies • " . $movie["title"],
|
$pageDescription = truncateText(htmlspecialchars(strip_tags($movie["description"]), ENT_QUOTES, "UTF-8"), 250);
|
||||||
ENT_QUOTES,
|
|
||||||
"UTF-8"
|
|
||||||
);
|
|
||||||
$pageDescription = truncateText(htmlspecialchars(
|
|
||||||
strip_tags($movie["description"]),
|
|
||||||
ENT_QUOTES,
|
|
||||||
"UTF-8"
|
|
||||||
), 250);
|
|
||||||
$ogImage = htmlspecialchars($movie["backdrop"], ENT_QUOTES, "UTF-8");
|
$ogImage = htmlspecialchars($movie["backdrop"], ENT_QUOTES, "UTF-8");
|
||||||
$fullUrl = "https://www.coryd.dev" . $requestUri;
|
$fullUrl = "https://www.coryd.dev" . $requestUri;
|
||||||
$oembedUrl = "https://www.coryd.dev/oembed" . $requestUri;
|
$oembedUrl = "https://www.coryd.dev/oembed" . $requestUri;
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
|
|
||||||
header("Cache-Control: public, max-age=3600");
|
header("Cache-Control: public, max-age=3600");
|
||||||
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT");
|
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT");
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -1,75 +1,37 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
require __DIR__ . "/../../vendor/autoload.php";
|
require __DIR__ . "/../../vendor/autoload.php";
|
||||||
require __DIR__ . "/../../server/utils/init.php";
|
require __DIR__ . "/../../server/utils/init.php";
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
use App\Classes\ShowFetcher;
|
||||||
use voku\helper\HtmlMin;
|
use voku\helper\HtmlMin;
|
||||||
|
|
||||||
$requestUri = $_SERVER["REQUEST_URI"];
|
$requestUri = $_SERVER["REQUEST_URI"];
|
||||||
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
|
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
|
||||||
$postgrestUrl = $_ENV["POSTGREST_URL"] ?? getenv("POSTGREST_URL");
|
|
||||||
$postgrestApiKey = $_ENV["POSTGREST_API_KEY"] ?? getenv("POSTGREST_API_KEY");
|
|
||||||
|
|
||||||
if (!preg_match('/^watching\/shows\/[\w-]+$/', $url)) {
|
if (!preg_match('/^watching\/shows\/[\w-]+$/', $url)) {
|
||||||
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
$cacheKey = "show_" . md5($url);
|
$fetcher = new ShowFetcher();
|
||||||
$show = null;
|
$show = $fetcher->fetch($url);
|
||||||
$useRedis = false;
|
|
||||||
|
|
||||||
try {
|
if (!$show) {
|
||||||
if (extension_loaded('redis')) {
|
|
||||||
$redis = new Redis();
|
|
||||||
$redis->connect('127.0.0.1', 6379);
|
|
||||||
$useRedis = true;
|
|
||||||
}
|
|
||||||
} catch (Exception $e) {
|
|
||||||
error_log("Redis not available: " . $e->getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($useRedis && $redis->exists($cacheKey)) {
|
|
||||||
$show = json_decode($redis->get($cacheKey), true);
|
|
||||||
} else {
|
|
||||||
$apiUrl = "$postgrestUrl/optimized_shows?url=eq./" . $url;
|
|
||||||
$client = new Client();
|
|
||||||
|
|
||||||
try {
|
|
||||||
$response = $client->request("GET", $apiUrl, [
|
|
||||||
"headers" => [
|
|
||||||
"Accept" => "application/json",
|
|
||||||
"Authorization" => "Bearer {$postgrestApiKey}",
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
|
|
||||||
$showData = json_decode($response->getBody(), true);
|
|
||||||
|
|
||||||
if (!$showData) {
|
|
||||||
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
$show = $showData[0];
|
|
||||||
|
|
||||||
if ($useRedis) {
|
|
||||||
$redis->setex($cacheKey, 3600, json_encode($show));
|
|
||||||
}
|
|
||||||
} catch (Exception $e) {
|
|
||||||
error_log($e->getMessage());
|
|
||||||
echo file_get_contents(__DIR__ . "/../../404/index.html");
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$pageTitle = htmlspecialchars("Show • " . $show["title"], ENT_QUOTES, "UTF-8");
|
$pageTitle = htmlspecialchars("Show • " . $show["title"], ENT_QUOTES, "UTF-8");
|
||||||
$pageDescription = truncateText(htmlspecialchars(strip_tags($show["description"]), ENT_QUOTES, "UTF-8"), 250);
|
$pageDescription = truncateText(
|
||||||
|
htmlspecialchars(strip_tags($show["description"]), ENT_QUOTES, "UTF-8"),
|
||||||
|
250
|
||||||
|
);
|
||||||
$ogImage = htmlspecialchars($show["image"], ENT_QUOTES, "UTF-8");
|
$ogImage = htmlspecialchars($show["image"], ENT_QUOTES, "UTF-8");
|
||||||
$fullUrl = "https://www.coryd.dev" . $requestUri;
|
$fullUrl = "https://www.coryd.dev" . $requestUri;
|
||||||
$oembedUrl = "https://www.coryd.dev/oembed" . $requestUri;
|
$oembedUrl = "https://www.coryd.dev/oembed" . $requestUri;
|
||||||
|
|
||||||
ob_start();
|
ob_start();
|
||||||
|
|
||||||
header("Cache-Control: public, max-age=3600");
|
header("Cache-Control: public, max-age=3600");
|
||||||
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT");
|
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT");
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -1,92 +1,49 @@
|
||||||
<?php
|
<?php
|
||||||
require __DIR__ . "/../vendor/autoload.php";
|
|
||||||
require __DIR__ . "/../server/utils/init.php";
|
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
require __DIR__ . "/../vendor/autoload.php";
|
||||||
use voku\helper\HtmlMin;
|
require __DIR__ . "/../server/utils/init.php";
|
||||||
|
|
||||||
$requestUri = $_SERVER["REQUEST_URI"];
|
use App\Classes\TagFetcher;
|
||||||
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
|
use voku\helper\HtmlMin;
|
||||||
$postgrestUrl = $_ENV["POSTGREST_URL"] ?? getenv("POSTGREST_URL");
|
|
||||||
$postgrestApiKey = $_ENV["POSTGREST_API_KEY"] ?? getenv("POSTGREST_API_KEY");
|
|
||||||
|
|
||||||
if ($url === "tags") {
|
$requestUri = $_SERVER["REQUEST_URI"];
|
||||||
|
$url = trim(parse_url($requestUri, PHP_URL_PATH), "/");
|
||||||
|
|
||||||
|
if ($url === "tags") {
|
||||||
readfile("index.html");
|
readfile("index.html");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!preg_match('/^tags\/(.+?)(?:\/(\d+))?$/', $url, $matches)) {
|
if (!preg_match('/^tags\/(.+?)(?:\/(\d+))?$/', $url, $matches)) {
|
||||||
echo file_get_contents(__DIR__ . "/../404/index.html");
|
echo file_get_contents(__DIR__ . "/../404/index.html");
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($matches[2]) && (int)$matches[2] === 1) {
|
if (isset($matches[2]) && (int)$matches[2] === 1) {
|
||||||
header("Location: /tags/{$matches[1]}", true, 301);
|
header("Location: /tags/{$matches[1]}", true, 301);
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
$tag = strtolower(urldecode($matches[1]));
|
$tag = strtolower(urldecode($matches[1]));
|
||||||
|
|
||||||
if (!preg_match('/^[\p{L}\p{N} _\.\-\&]+$/u', $tag)) {
|
if (!preg_match('/^[\p{L}\p{N} _\.\-\&]+$/u', $tag)) {
|
||||||
echo file_get_contents(__DIR__ . "/../404/index.html");
|
echo file_get_contents(__DIR__ . "/../404/index.html");
|
||||||
exit();
|
exit();
|
||||||
}
|
|
||||||
|
|
||||||
$pageParam = isset($matches[2]) ? (int)$matches[2] : 0;
|
|
||||||
$page = max($pageParam, 1);
|
|
||||||
$pageSize = 20;
|
|
||||||
$offset = ($page - 1) * $pageSize;
|
|
||||||
|
|
||||||
$cacheKey = "tag_{$tag}_{$page}";
|
|
||||||
$useRedis = false;
|
|
||||||
$tagged = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (extension_loaded('redis')) {
|
|
||||||
$redis = new Redis();
|
|
||||||
$redis->connect('127.0.0.1', 6379);
|
|
||||||
$useRedis = true;
|
|
||||||
}
|
}
|
||||||
} catch (Exception $e) {
|
|
||||||
error_log("Redis not available: " . $e->getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($useRedis && $redis->exists($cacheKey)) {
|
$page = isset($matches[2]) ? max(1, (int)$matches[2]) : 1;
|
||||||
$tagged = json_decode($redis->get($cacheKey), true);
|
$pageSize = 20;
|
||||||
} else {
|
$fetcher = new TagFetcher();
|
||||||
$client = new Client();
|
$tagged = $fetcher->fetch($tag, $page, $pageSize);
|
||||||
try {
|
|
||||||
$response = $client->post($postgrestUrl . '/rpc/get_tagged_content', [
|
|
||||||
'headers' => [
|
|
||||||
'Content-Type' => 'application/json',
|
|
||||||
'Authorization' => "Bearer {$postgrestApiKey}",
|
|
||||||
],
|
|
||||||
'json' => [
|
|
||||||
'tag_query' => $tag,
|
|
||||||
'page_size' => $pageSize,
|
|
||||||
'page_offset' => $offset
|
|
||||||
]
|
|
||||||
]);
|
|
||||||
|
|
||||||
$tagged = json_decode($response->getBody(), true);
|
if (!$tagged || count($tagged) === 0) {
|
||||||
if ($useRedis) {
|
|
||||||
$redis->setex($cacheKey, 3600, json_encode($tagged));
|
|
||||||
}
|
|
||||||
} catch (Exception $e) {
|
|
||||||
error_log($e->getMessage());
|
|
||||||
echo file_get_contents(__DIR__ . '/../404/index.html');
|
echo file_get_contents(__DIR__ . '/../404/index.html');
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!$tagged || count($tagged) === 0) {
|
$totalCount = $tagged[0]['total_count'] ?? 0;
|
||||||
echo file_get_contents(__DIR__ . '/../404/index.html');
|
$totalPages = max(ceil($totalCount / $pageSize), 1);
|
||||||
exit();
|
$pagination = [
|
||||||
}
|
|
||||||
|
|
||||||
$totalCount = $tagged[0]['total_count'] ?? 0;
|
|
||||||
$totalPages = max(ceil($totalCount / $pageSize), 1);
|
|
||||||
$pagination = [
|
|
||||||
'pageNumber' => $page,
|
'pageNumber' => $page,
|
||||||
'pages' => range(1, $totalPages),
|
'pages' => range(1, $totalPages),
|
||||||
'href' => [
|
'href' => [
|
||||||
|
@ -94,16 +51,14 @@ $pagination = [
|
||||||
'next' => $page < $totalPages ? "/tags/{$tag}/" . ($page + 1) : null
|
'next' => $page < $totalPages ? "/tags/{$tag}/" . ($page + 1) : null
|
||||||
],
|
],
|
||||||
'links' => range(1, $totalPages)
|
'links' => range(1, $totalPages)
|
||||||
];
|
];
|
||||||
|
|
||||||
$pageTitle = "#" . strtolower(ucfirst($tag)) . "";
|
$pageTitle = "#" . strtolower(ucfirst($tag));
|
||||||
$pageDescription = "All content tagged with #" . strtolower(ucfirst($tag)) . ".";
|
$pageDescription = "All content tagged with #" . strtolower(ucfirst($tag)) . ".";
|
||||||
$fullUrl = "https://www.coryd.dev" . $requestUri;
|
$fullUrl = "https://www.coryd.dev" . $requestUri;
|
||||||
$oembedUrl = "https://www.coryd.dev/oembed" . $requestUri;
|
$oembedUrl = "https://www.coryd.dev/oembed" . $requestUri;
|
||||||
|
|
||||||
ob_start();
|
|
||||||
|
|
||||||
header("Cache-Control: public, max-age=3600");
|
|
||||||
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT");
|
|
||||||
|
|
||||||
|
ob_start();
|
||||||
|
header("Cache-Control: public, max-age=3600");
|
||||||
|
header("Expires: " . gmdate("D, d M Y H:i:s", time() + 3600) . " GMT");
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -28,7 +28,7 @@
|
||||||
eleventy:eleventy
|
eleventy:eleventy
|
||||||
%}
|
%}
|
||||||
<script defer src="/assets/scripts/index.js?v={% appVersion %}"></script>
|
<script defer src="/assets/scripts/index.js?v={% appVersion %}"></script>
|
||||||
<script defer src="https://stats.apps.coryd.dev/script.js?v={% appVersion %}" data-website-id="a30f4735-6c27-4761-b180-a8f247a4a3a3"></script>
|
<script defer src="/assets/scripts/util.js?v={% appVersion %}" data-host-url="https://stats.apps.coryd.dev" data-website-id="a30f4735-6c27-4761-b180-a8f247a4a3a3"></script>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="main-wrapper">
|
<div class="main-wrapper">
|
||||||
|
|
|
@ -59,6 +59,9 @@ RewriteRule ^og/([a-z0-9\-]+)/([\d\.]+)/([a-f0-9\-]+)\.([a-z0-9]+)$ /api/og-imag
|
||||||
RewriteRule ^oembed/?$ /api/oembed.php [L]
|
RewriteRule ^oembed/?$ /api/oembed.php [L]
|
||||||
RewriteRule ^oembed/(.+)$ /api/oembed.php?url=https://www.coryd.dev/$1 [L,QSA]
|
RewriteRule ^oembed/(.+)$ /api/oembed.php?url=https://www.coryd.dev/$1 [L,QSA]
|
||||||
|
|
||||||
|
## analytics
|
||||||
|
RewriteRule ^assets/scripts/util\.js$ /api/umami.php [L]
|
||||||
|
|
||||||
{% for redirect in redirects -%}
|
{% for redirect in redirects -%}
|
||||||
Redirect {{ redirect.status_code | default: "301" }} {{ redirect.source_url }} {{ redirect.destination_url }}
|
Redirect {{ redirect.status_code | default: "301" }} {{ redirect.source_url }} {{ redirect.destination_url }}
|
||||||
{% endfor -%}
|
{% endfor -%}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue