Skip to content

Commit

Permalink
Implement logs and redis keys endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
danieltrolezi committed Sep 23, 2024
1 parent be95f17 commit b2510c8
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 3 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=secret

REDIS_CONNECTION=default
REDIS_HOST=redis
REDIS_PASSWORD=redis

Expand Down
36 changes: 36 additions & 0 deletions app/Http/Controllers/Admin/RedisController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Redis;
use OpenApi\Attributes as OA;

class RedisController extends Controller
{
#[OA\Get(
path: '/api/admin/redis/keys',
tags: ['admin'],
responses: [
new OA\Response(response: 200, description: 'List of Redis keys and values')
]
)]
public function keys(): JsonResponse
{
$redis = Redis::connection(config('database.redis.connection'));
$prefix = config('database.redis.options.prefix');
$keys = $redis->keys('*');
$data = [];

foreach ($keys as $key) {
$value = $redis->get(
str_replace($prefix, '', $key)
);

$data[$key] = substr($value, 0, 100) . '...';
}

return response()->json($data);
}
}
2 changes: 1 addition & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private function setHealthCheck(): void
{
$env = match (config('app.url')) {
'http://gamewatch.local' => 'local',
default => 'prod'
default => 'production'
};

Health::checks([
Expand Down
16 changes: 14 additions & 2 deletions app/Services/Rawg/RawgBaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Services\Rawg;

use GuzzleHttp\Client;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

abstract class RawgBaseService
Expand All @@ -11,13 +12,18 @@ abstract class RawgBaseService

protected string $apiKey;
protected string $apiUrl;
private $redis;

public function __construct(
protected RawgFilterService $filterService,
protected Client $client
) {
$this->apiUrl = config('services.rawg.host') . '/api/';
$this->apiKey = config('services.rawg.key');

$this->redis = Redis::connection(
config('database.redis.connection')
);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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
));
}

/**
Expand Down
4 changes: 4 additions & 0 deletions app/Swagger/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
15 changes: 15 additions & 0 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_'),
Expand All @@ -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'),
Expand Down
4 changes: 4 additions & 0 deletions ecs/deployment-task.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
"name": "DB_PASSWORD",
"valueFrom": "<SSM_NAMESPACE>/DB_PASSWORD"
},
{
"name": "REDIS_CONNECTION",
"valueFrom": "<SSM_NAMESPACE>/REDIS_CONNECTION"
},
{
"name": "REDIS_HOST",
"valueFrom": "<SSM_NAMESPACE>/REDIS_HOST"
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
<env name="LOG_LEVEL" value="error"/>
</php>
</phpunit>
11 changes: 11 additions & 0 deletions public/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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'
Expand Down
8 changes: 8 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 () {
Expand Down
4 changes: 4 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ protected function prepRawgForUnitTesting(): void

protected function mockRedis(): void
{
Redis::shouldReceive('connection')
->once()
->andReturnSelf();

Redis::shouldReceive('get')
->once()
->andReturn(null);
Expand Down

0 comments on commit b2510c8

Please sign in to comment.