Skip to content

Commit

Permalink
refactor: extract exception and cache middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
dvikan committed Sep 1, 2024
1 parent 36fd72c commit 465f6e8
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 56 deletions.
36 changes: 2 additions & 34 deletions actions/DisplayAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,6 @@ public function __invoke(Request $request): Response
$format = $request->get('format');
$noproxy = $request->get('_noproxy');

$cacheKey = 'http_' . json_encode($request->toArray());
/** @var Response $cachedResponse */
$cachedResponse = $this->cache->get($cacheKey);
if ($cachedResponse) {
$ifModifiedSince = $request->server('HTTP_IF_MODIFIED_SINCE');
$lastModified = $cachedResponse->getHeader('last-modified');
if ($ifModifiedSince && $lastModified) {
$lastModified = new \DateTimeImmutable($lastModified);
$lastModifiedTimestamp = $lastModified->getTimestamp();
$modifiedSince = strtotime($ifModifiedSince);
// TODO: \DateTimeImmutable can be compared directly
if ($lastModifiedTimestamp <= $modifiedSince) {
$modificationTimeGMT = gmdate('D, d M Y H:i:s ', $lastModifiedTimestamp);
return new Response('', 304, ['last-modified' => $modificationTimeGMT . 'GMT']);
}
}
return $cachedResponse->withHeader('rss-bridge', 'This is a cached response');
}

if (!$bridgeName) {
return new Response(render(__DIR__ . '/../templates/error.html.php', ['message' => 'Missing bridge parameter']), 400);
}
Expand All @@ -66,6 +47,8 @@ public function __invoke(Request $request): Response
define('NOPROXY', true);
}

$cacheKey = 'http_' . json_encode($request->toArray());

$bridge = $this->bridgeFactory->create($bridgeClassName);

$response = $this->createResponse($request, $bridge, $format);
Expand All @@ -80,21 +63,6 @@ public function __invoke(Request $request): Response
$this->cache->set($cacheKey, $response, $ttl);
}

if (in_array($response->getCode(), [403, 429, 503])) {
// Cache these responses for about ~20 mins on average
$this->cache->set($cacheKey, $response, 60 * 15 + rand(1, 60 * 10));
}

if ($response->getCode() === 500) {
$this->cache->set($cacheKey, $response, 60 * 15);
}

// For 1% of requests, prune cache
if (rand(1, 100) === 1) {
// This might be resource intensive!
$this->cache->prune();
}

return $response;
}

Expand Down
15 changes: 5 additions & 10 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,8 @@
$request = Request::fromGlobals();
}

try {
$rssBridge = new RssBridge($container);
$response = $rssBridge->main($request);
$response->send();
} catch (\Throwable $e) {
// Probably an exception inside an action
$logger->error('Exception in RssBridge::main()', ['e' => $e]);
$response = new Response(render(__DIR__ . '/templates/exception.html.php', ['e' => $e]), 500);
$response->send();
}
$rssBridge = new RssBridge($container);

$response = $rssBridge->main($request);

$response->send();
4 changes: 4 additions & 0 deletions lib/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public static function loadConfiguration(array $customConfig = [], array $env =
}
}

if (Debug::isEnabled()) {
self::setConfig('cache', 'type', 'array');
}

if (!is_array(self::getConfig('system', 'enabled_bridges'))) {
self::throwConfigError('system', 'enabled_bridges', 'Is not an array');
}
Expand Down
4 changes: 3 additions & 1 deletion lib/RssBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public function main(Request $request): Response
$handler = $this->container[$actionName];

$middlewares = [
new CacheMiddleware($this->container['cache']),
new ExceptionMiddleware($this->container['logger']),
new SecurityMiddleware(),
new MaintenanceMiddleware(),
new BasicAuthMiddleware(),
Expand All @@ -34,6 +36,6 @@ public function main(Request $request): Response
foreach (array_reverse($middlewares) as $middleware) {
$action = fn ($req) => $middleware($req, $action);
}
return $action($request);
return $action($request->withAttribute('action', $actionName));
}
}
12 changes: 2 additions & 10 deletions lib/dependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,14 @@
// $logger->addHandler(new StreamHandler('/tmp/rss-bridge.txt', Logger::INFO));

// Uncomment this for debug logging to fs
//$logger->addHandler(new StreamHandler('/tmp/rss-bridge-debug.txt', Logger::DEBUG));
// $logger->addHandler(new StreamHandler('/tmp/rss-bridge-debug.txt', Logger::DEBUG));
return $logger;
};

$container['cache'] = function ($c) {
/** @var CacheFactory $cacheFactory */
$cacheFactory = $c['cache_factory'];
$type = Configuration::getConfig('cache', 'type');
if (!$type) {
throw new \Exception('No cache type configured');
}
if (Debug::isEnabled()) {
$cache = $cacheFactory->create('array');
} else {
$cache = $cacheFactory->create($type);
}
$cache = $cacheFactory->create(Configuration::getConfig('cache', 'type'));
return $cache;
};

Expand Down
59 changes: 59 additions & 0 deletions middlewares/CacheMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

class CacheMiddleware implements Middleware
{
private CacheInterface $cache;

public function __construct(CacheInterface $cache)
{
$this->cache = $cache;
}

public function __invoke(Request $request, $next): Response
{
$action = $request->attribute('action');

if ($action !== 'DisplayAction') {
// We only cache DisplayAction (for now)
return $next($request);
}

// TODO: might want to remove som params from query
$cacheKey = 'http_' . json_encode($request->toArray());
$cachedResponse = $this->cache->get($cacheKey);

if ($cachedResponse) {
$ifModifiedSince = $request->server('HTTP_IF_MODIFIED_SINCE');
$lastModified = $cachedResponse->getHeader('last-modified');
if ($ifModifiedSince && $lastModified) {
$lastModified = new \DateTimeImmutable($lastModified);
$lastModifiedTimestamp = $lastModified->getTimestamp();
$modifiedSince = strtotime($ifModifiedSince);
// TODO: \DateTimeImmutable can be compared directly
if ($lastModifiedTimestamp <= $modifiedSince) {
$modificationTimeGMT = gmdate('D, d M Y H:i:s ', $lastModifiedTimestamp);
return new Response('', 304, ['last-modified' => $modificationTimeGMT . 'GMT']);
}
}
return $cachedResponse;
}

/** @var Response $response */
$response = $next($request);

if (in_array($response->getCode(), [403, 429, 500, 503])) {
// Cache these responses for about ~20 mins on average
$this->cache->set($cacheKey, $response, 60 * 15 + rand(1, 60 * 10));
}

// For 1% of requests, prune cache
if (rand(1, 100) === 1) {
// This might be resource intensive!
$this->cache->prune();
}

return $response;
}
}
24 changes: 24 additions & 0 deletions middlewares/ExceptionMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

class ExceptionMiddleware implements Middleware
{
private Logger $logger;

public function __construct(Logger $logger)
{
$this->logger = $logger;
}

public function __invoke(Request $request, $next): Response
{
try {
return $next($request);
} catch (\Throwable $e) {
$this->logger->error('Exception in ExceptionMiddleware', ['e' => $e]);

return new Response(render(__DIR__ . '/../templates/exception.html.php', ['e' => $e]), 500);
}
}
}
1 change: 0 additions & 1 deletion templates/html-format.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
</head>

<body>

<div class="container">

<h1 class="pagetitle">
Expand Down

0 comments on commit 465f6e8

Please sign in to comment.