mirror of
https://github.com/CachetHQ/Cachet.git
synced 2025-01-17 13:38:20 +01:00
Merge pull request #3481 from CachetHQ/feature/api-cache-control
API cache control
This commit is contained in:
commit
ea8c9454e7
@ -15,6 +15,7 @@ use Barryvdh\Cors\HandleCors;
|
|||||||
use CachetHQ\Cachet\Http\Middleware\Admin;
|
use CachetHQ\Cachet\Http\Middleware\Admin;
|
||||||
use CachetHQ\Cachet\Http\Middleware\ApiAuthentication;
|
use CachetHQ\Cachet\Http\Middleware\ApiAuthentication;
|
||||||
use CachetHQ\Cachet\Http\Middleware\Authenticate;
|
use CachetHQ\Cachet\Http\Middleware\Authenticate;
|
||||||
|
use CachetHQ\Cachet\Http\Middleware\CacheControl;
|
||||||
use CachetHQ\Cachet\Http\Middleware\Localize;
|
use CachetHQ\Cachet\Http\Middleware\Localize;
|
||||||
use CachetHQ\Cachet\Http\Middleware\ReadyForUse;
|
use CachetHQ\Cachet\Http\Middleware\ReadyForUse;
|
||||||
use CachetHQ\Cachet\Http\Middleware\RedirectIfAuthenticated;
|
use CachetHQ\Cachet\Http\Middleware\RedirectIfAuthenticated;
|
||||||
@ -47,6 +48,7 @@ class Kernel extends HttpKernel
|
|||||||
'admin' => Admin::class,
|
'admin' => Admin::class,
|
||||||
'can' => Authorize::class,
|
'can' => Authorize::class,
|
||||||
'cors' => HandleCors::class,
|
'cors' => HandleCors::class,
|
||||||
|
'cache' => CacheControl::class,
|
||||||
'auth' => Authenticate::class,
|
'auth' => Authenticate::class,
|
||||||
'auth.api' => ApiAuthentication::class,
|
'auth.api' => ApiAuthentication::class,
|
||||||
'guest' => RedirectIfAuthenticated::class,
|
'guest' => RedirectIfAuthenticated::class,
|
||||||
|
37
app/Http/Middleware/CacheControl.php
Normal file
37
app/Http/Middleware/CacheControl.php
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Cachet.
|
||||||
|
*
|
||||||
|
* (c) Alt Three Services Limited
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace CachetHQ\Cachet\Http\Middleware;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class CacheControl
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Handle an incoming request.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @param \Closure $next
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle(Request $request, Closure $next)
|
||||||
|
{
|
||||||
|
$response = $next($request);
|
||||||
|
|
||||||
|
$maxAge = time() + 30;
|
||||||
|
|
||||||
|
$response->header('Cache-Control', 'public,max-age='.$maxAge);
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
}
|
@ -43,7 +43,7 @@ class ApiSystemRoutes
|
|||||||
$router->group(['middleware' => ['auth.api']], function (Registrar $router) {
|
$router->group(['middleware' => ['auth.api']], function (Registrar $router) {
|
||||||
$router->get('ping', 'GeneralController@ping');
|
$router->get('ping', 'GeneralController@ping');
|
||||||
$router->get('version', 'GeneralController@version');
|
$router->get('version', 'GeneralController@version');
|
||||||
$router->get('status', 'GeneralController@status');
|
$router->get('status', ['uses' => 'GeneralController@status', 'middleware' => ['cache']]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace CachetHQ\Tests\Cachet\Api;
|
namespace CachetHQ\Tests\Cachet\Api;
|
||||||
|
|
||||||
|
use CachetHQ\Cachet\Models\Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the general test class.
|
* This is the general test class.
|
||||||
*
|
*
|
||||||
@ -42,4 +44,36 @@ class GeneralTest extends AbstractApiTestCase
|
|||||||
|
|
||||||
$response->assertStatus(406);
|
$response->assertStatus(406);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_can_get_system_status()
|
||||||
|
{
|
||||||
|
$response = $this->json('GET', '/api/v1/status');
|
||||||
|
|
||||||
|
$response->assertStatus(200)
|
||||||
|
->assertHeader('Cache-Control')
|
||||||
|
->assertJsonFragment([
|
||||||
|
'data' => [
|
||||||
|
'status' => 'success',
|
||||||
|
'message' => 'System operational',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_can_get_system_status_not_success()
|
||||||
|
{
|
||||||
|
factory(Component::class)->create([
|
||||||
|
'status' => 3,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response = $this->json('GET', '/api/v1/status');
|
||||||
|
|
||||||
|
$response->assertStatus(200)
|
||||||
|
->assertHeader('Cache-Control')
|
||||||
|
->assertJsonFragment([
|
||||||
|
'data' => [
|
||||||
|
'status' => 'info',
|
||||||
|
'message' => 'The system is experiencing issues',
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user