Skip to content

Commit

Permalink
Add logs and redis controller
Browse files Browse the repository at this point in the history
  • Loading branch information
danieltrolezi committed Sep 23, 2024
1 parent be95f17 commit c8e368a
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,6 @@ jobs:
aws ecs update-service \
--cluster ${{ vars.ECS_CLUSTER }} \
--service ${{ vars.ECS_SERVICE_NAME }} \
--task-definition ${{ vars.ECS_SERVICE_NAME }}-task \
--force-new-deployment \
--region ${{ vars.AWS_REGION }}
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
9 changes: 8 additions & 1 deletion 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 Down Expand Up @@ -69,7 +70,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 = 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
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

0 comments on commit c8e368a

Please sign in to comment.