diff --git a/.env.example b/.env.example index 52a28f0..03d84cb 100644 --- a/.env.example +++ b/.env.example @@ -15,6 +15,7 @@ DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD=secret +REDIS_CONNECTION=default REDIS_HOST=redis REDIS_PASSWORD=redis diff --git a/app/Http/Controllers/Admin/RedisController.php b/app/Http/Controllers/Admin/RedisController.php new file mode 100644 index 0000000..d5f669f --- /dev/null +++ b/app/Http/Controllers/Admin/RedisController.php @@ -0,0 +1,36 @@ +keys('*'); + $data = []; + + foreach ($keys as $key) { + $value = $redis->get( + str_replace($prefix, '', $key) + ); + + $data[$key] = substr($value, 0, 100) . '...'; + } + + return response()->json($data); + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 09837cf..a4aa82b 100755 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -39,7 +39,7 @@ private function setHealthCheck(): void { $env = match (config('app.url')) { 'http://gamewatch.local' => 'local', - default => 'prod' + default => 'production' }; Health::checks([ diff --git a/app/Services/Rawg/RawgBaseService.php b/app/Services/Rawg/RawgBaseService.php index f549a11..3af8f76 100644 --- a/app/Services/Rawg/RawgBaseService.php +++ b/app/Services/Rawg/RawgBaseService.php @@ -3,6 +3,7 @@ namespace App\Services\Rawg; use GuzzleHttp\Client; +use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Redis; abstract class RawgBaseService @@ -11,6 +12,7 @@ abstract class RawgBaseService protected string $apiKey; protected string $apiUrl; + private $redis; public function __construct( protected RawgFilterService $filterService, @@ -18,6 +20,10 @@ public function __construct( ) { $this->apiUrl = config('services.rawg.host') . '/api/'; $this->apiKey = config('services.rawg.key'); + + $this->redis = Redis::connection( + config('database.redis.connection') + ); } /** @@ -57,7 +63,7 @@ protected function call( private function getCacheContents(string $uri, array $data): ?string { $key = $this->getCacheKey($uri, $data['query']); - return Redis::get($key); + return $this->redis->get($key); } /** @@ -69,7 +75,13 @@ private function getCacheContents(string $uri, array $data): ?string private function setCacheContents(string $uri, array $data, string $contents): void { $key = $this->getCacheKey($uri, $data['query']); - Redis::setEx($key, self::CACHE_TTL, $contents); + $result = $this->redis->setEx($key, self::CACHE_TTL, $contents); + + Log::debug(sprintf( + 'Redis cache set [key: %s, result: %s]', + $key, + $result + )); } /** diff --git a/app/Swagger/Application.php b/app/Swagger/Application.php index 328df53..49ee6d8 100644 --- a/app/Swagger/Application.php +++ b/app/Swagger/Application.php @@ -24,6 +24,10 @@ class Application name: 'application', description: 'health and other application routes' )] + #[OA\Tag( + name: 'admin', + description: 'admin panel routes' + )] #[OA\Tag( name: 'domain', description: 'rawg domain routes' diff --git a/config/database.php b/config/database.php index f8e8dcb..da15022 100755 --- a/config/database.php +++ b/config/database.php @@ -142,6 +142,8 @@ 'client' => env('REDIS_CLIENT', 'phpredis'), + 'connection' => env('REDIS_CONNECTION', 'default'), + 'options' => [ 'cluster' => env('REDIS_CLUSTER', 'redis'), 'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'), @@ -156,6 +158,19 @@ 'database' => env('REDIS_DB', '0'), ], + 'clusters' => [ + 'default' => [ + [ + 'url' => env('REDIS_URL'), + 'host' => env('REDIS_HOST', '127.0.0.1'), + 'username' => env('REDIS_USERNAME'), + 'password' => env('REDIS_PASSWORD'), + 'port' => env('REDIS_PORT', '6379'), + 'database' => env('REDIS_DB', '0'), + ], + ], + ], + 'cache' => [ 'url' => env('REDIS_URL'), 'host' => env('REDIS_HOST', '127.0.0.1'), diff --git a/ecs/deployment-task.json b/ecs/deployment-task.json index 1f672e8..d2c9369 100644 --- a/ecs/deployment-task.json +++ b/ecs/deployment-task.json @@ -79,6 +79,10 @@ "name": "DB_PASSWORD", "valueFrom": "/DB_PASSWORD" }, + { + "name": "REDIS_CONNECTION", + "valueFrom": "/REDIS_CONNECTION" + }, { "name": "REDIS_HOST", "valueFrom": "/REDIS_HOST" diff --git a/phpunit.xml b/phpunit.xml index 61c031c..a26fc41 100755 --- a/phpunit.xml +++ b/phpunit.xml @@ -29,5 +29,6 @@ + diff --git a/public/swagger.yaml b/public/swagger.yaml index 15be61a..8ba1fbb 100644 --- a/public/swagger.yaml +++ b/public/swagger.yaml @@ -117,6 +117,14 @@ paths: application/json: schema: $ref: '#/components/schemas/User' + /api/admin/redis/keys: + get: + tags: + - admin + operationId: 519129f3cdf7eea66301bd70ced39c8c + responses: + '200': + description: 'List of Redis keys and values' /api/auth/login: post: tags: @@ -606,6 +614,9 @@ tags: - name: application description: 'health and other application routes' + - + name: admin + description: 'admin panel routes' - name: domain description: 'rawg domain routes' diff --git a/routes/api.php b/routes/api.php index b1ac8fe..2050844 100644 --- a/routes/api.php +++ b/routes/api.php @@ -4,6 +4,7 @@ use App\Enums\Rawg\RawgGenre; use App\Enums\Scope; use App\Http\Controllers\AccountController; +use App\Http\Controllers\Admin\RedisController; use App\Http\Controllers\AuthController; use App\Http\Controllers\DiscordController; use App\Http\Controllers\RawgGamesController; @@ -34,6 +35,13 @@ Route::middleware('scopes:' . Scope::Root->value)->group(function () { Route::post('/account/register', [AccountController::class, 'register']); + + Route::prefix('admin') + ->group(function () { + Route::prefix('redis')->controller(RedisController::class)->group(function() { + Route::get('/keys', 'keys'); + }); + }); Route::prefix('rawg') ->group(function () { diff --git a/tests/TestCase.php b/tests/TestCase.php index bbf14d9..86d4908 100755 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -66,6 +66,10 @@ protected function prepRawgForUnitTesting(): void protected function mockRedis(): void { + Redis::shouldReceive('connection') + ->once() + ->andReturnSelf(); + Redis::shouldReceive('get') ->once() ->andReturn(null);