Skip to content

Commit

Permalink
Merge pull request #25 from edersoares/cache
Browse files Browse the repository at this point in the history
Adds cache to `proxy` type
  • Loading branch information
edersoares authored Sep 9, 2024
2 parents e071630 + a6e7cdb commit 0382284
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/FrontendProxyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,16 @@ public function __construct(protected Request $request)

public function __invoke($uri, $config): Response
{
$method = $this->request->getMethod();
$accept = $this->request->header('accept', '*/*');
$url = trim($config['url'], '/') . '/' . trim($uri, '/');
$cacheKey = str($config['url'])->replace(['/', '.'], '-')->value();

$path = storage_path("framework/views/frontier-$method-$cacheKey");

if ($method === 'GET' && $config['cache'] && file_exists($path)) {
return new Response(file_get_contents($path));
}

if ($config['rewrite']) {
$url = str_replace(
Expand All @@ -31,7 +39,7 @@ public function __invoke($uri, $config): Response
'Accept' => $accept,
]);

$response = match ($this->request->getMethod()) {
$response = match ($method) {
'GET' => $http->get($url),
'HEAD' => $http->head($url),
'POST' => $http->post($url, $this->request->all()),
Expand All @@ -51,6 +59,10 @@ public function __invoke($uri, $config): Response
);
}

if ($config['cache']) {
file_put_contents($path, $content);
}

return new Response($content, headers: [
'content-type' => $contextType,
]);
Expand Down
8 changes: 8 additions & 0 deletions src/Frontier.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,16 @@ private static function proxy(array $config): void
$methods = [];
$replaces = [];
$rewrite = [];
$cache = false;
$proxyAll = true;

foreach ($segments as $segment) {
$cache = false;

if ($segment === 'cache') {
$cache = true;
}

if ($segment === 'exact') {
$proxyAll = false;
}
Expand Down Expand Up @@ -99,6 +106,7 @@ private static function proxy(array $config): void
'replaces' => $replaces,
'rewrite' => $rewrite,
'methods' => $methods,
'cache' => $cache,
],
]);
}
Expand Down
26 changes: 26 additions & 0 deletions tests/FrontendProxyControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'/all::replace(Replace,is amazing!)',
'/web',
'/all-methods::methods(get,head,options,post,put,patch,delete)',
'/with-cache::cache',
],
]));

Expand Down Expand Up @@ -163,3 +164,28 @@

Http::assertSent(fn (Request $request) => $request->method() === 'DELETE');
});

test('proxy and do cache', function () {
$file = storage_path('framework/views/frontier-GET-frontier-test-with-cache');
$text = 'Frontier by HTTP';

Http::fake([
'frontier.test/*' => Http::response($text),
]);

$this->assertFileDoesNotExist($file);

$this->get('/with-cache')
->assertStatus(200)
->assertSeeText($text);

$this->assertFileExists($file);
$this->assertStringEqualsFile($file, $text);

$this->get('/with-cache')
->assertStatus(200)
->assertSeeText($text);

// Remove cache
$this->artisan('view:clear');
});

0 comments on commit 0382284

Please sign in to comment.