From 256248f6356c87cd7676493b08c6bc845ac6394c Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Wed, 18 Dec 2024 00:21:43 +0800 Subject: [PATCH] wip redis --- src/svc-main/.env.example | 10 ++++++- src/svc-main/composer.json | 1 + src/svc-main/composer.lock | 31 +++++++++++---------- src/svc-main/config.php | 10 ++++++- src/svc-main/controllers/HomeController.php | 2 ++ src/svc-main/core/Bootstrapper.php | 17 +++++++++++ src/svc-main/public/index.php | 1 + 7 files changed, 55 insertions(+), 17 deletions(-) diff --git a/src/svc-main/.env.example b/src/svc-main/.env.example index de4c437..0aacebd 100644 --- a/src/svc-main/.env.example +++ b/src/svc-main/.env.example @@ -1,7 +1,15 @@ APP_TITLE='Плейлисты IPTV' -APP_URL=http://localhost:8080 +APP_URL=http://localhost:${IPTV_NGNIX_PORT} + TWIG_CACHE=1 TWIG_DEBUG=0 + FLIGHT_CASE_SENSITIVE=0 FLIGHT_HANDLE_ERRORS=1 FLIGHT_LOG_ERRORS=1 + +REDIS_HOST=iptv-keydb +REDIS_PORT=6379 +REDIS_DB=0 +REDIS_USER= +REDIS_PASS= diff --git a/src/svc-main/composer.json b/src/svc-main/composer.json index 52bbf07..c38fc4a 100644 --- a/src/svc-main/composer.json +++ b/src/svc-main/composer.json @@ -4,6 +4,7 @@ "ext-json": "*", "ext-curl": "*", "ext-fileinfo": "*", + "ext-redis": "*", "mikecao/flight": "^3.12", "symfony/dotenv": "^7.1", "twig/twig": "^3.14" diff --git a/src/svc-main/composer.lock b/src/svc-main/composer.lock index 7e44423..333475a 100644 --- a/src/svc-main/composer.lock +++ b/src/svc-main/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "1aaea4609092e8a88074f050dab42323", + "content-hash": "4d763eef27bbfd40014e392726e5afc1", "packages": [ { "name": "mikecao/flight", @@ -244,8 +244,8 @@ "type": "library", "extra": { "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" + "url": "https://github.com/symfony/polyfill", + "name": "symfony/polyfill" } }, "autoload": { @@ -323,8 +323,8 @@ "type": "library", "extra": { "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" + "url": "https://github.com/symfony/polyfill", + "name": "symfony/polyfill" } }, "autoload": { @@ -397,8 +397,8 @@ "type": "library", "extra": { "thanks": { - "name": "symfony/polyfill", - "url": "https://github.com/symfony/polyfill" + "url": "https://github.com/symfony/polyfill", + "name": "symfony/polyfill" } }, "autoload": { @@ -455,16 +455,16 @@ }, { "name": "twig/twig", - "version": "v3.16.0", + "version": "v3.17.1", "source": { "type": "git", "url": "https://github.com/twigphp/Twig.git", - "reference": "475ad2dc97d65d8631393e721e7e44fb544f0561" + "reference": "677ef8da6497a03048192aeeb5aa3018e379ac71" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/twigphp/Twig/zipball/475ad2dc97d65d8631393e721e7e44fb544f0561", - "reference": "475ad2dc97d65d8631393e721e7e44fb544f0561", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/677ef8da6497a03048192aeeb5aa3018e379ac71", + "reference": "677ef8da6497a03048192aeeb5aa3018e379ac71", "shasum": "" }, "require": { @@ -519,7 +519,7 @@ ], "support": { "issues": "https://github.com/twigphp/Twig/issues", - "source": "https://github.com/twigphp/Twig/tree/v3.16.0" + "source": "https://github.com/twigphp/Twig/tree/v3.17.1" }, "funding": [ { @@ -531,7 +531,7 @@ "type": "tidelift" } ], - "time": "2024-11-29T08:27:05+00:00" + "time": "2024-12-12T09:58:10+00:00" } ], "packages-dev": [], @@ -541,10 +541,11 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": "^8.2", + "php": "^8.4", "ext-json": "*", "ext-curl": "*", - "ext-fileinfo": "*" + "ext-fileinfo": "*", + "ext-redis": "*" }, "platform-dev": {}, "plugin-api-version": "2.6.0" diff --git a/src/svc-main/config.php b/src/svc-main/config.php index 64b81ef..ba75458 100644 --- a/src/svc-main/config.php +++ b/src/svc-main/config.php @@ -4,14 +4,16 @@ declare(strict_types=1); return [ // https://flightphp.com/learn#configuration - 'flight.base_url' => env('APP_URL', 'http://localhost:8080'), + 'flight.base_url' => env('APP_URL', 'http://localhost:' . env('IPTV_NGNIX_PORT')), 'flight.case_sensitive' => bool(env('FLIGHT_CASE_SENSITIVE', false)), 'flight.handle_errors' => bool(env('FLIGHT_HANDLE_ERRORS', true)), 'flight.log_errors' => bool(env('FLIGHT_LOG_ERRORS', true)), 'flight.views.path' => views_path(), 'flight.views.extension' => '.twig', + 'twig.cache' => bool(env('TWIG_CACHE', true)) ? cache_path() . '/views' : false, 'twig.debug' => bool(env('TWIG_DEBUG', false)), + 'app.title' => env('APP_TITLE', 'IPTV Playlists'), 'app.pls_encodings' => [ 'UTF-8', @@ -19,4 +21,10 @@ return [ // 'CP866', // 'ISO-8859-5', ], + + 'redis.host' => env('REDIS_HOST', 'iptv-keydb'), + 'redis.port' => (int)env('REDIS_PORT', 6379), + // 'redis.user' => env('REDIS_USER'), + // 'redis.password' => env('REDIS_PASS'), + 'redis.database' => (int)env('REDIS_DB', 0), ]; diff --git a/src/svc-main/controllers/HomeController.php b/src/svc-main/controllers/HomeController.php index b0a95a2..216722e 100644 --- a/src/svc-main/controllers/HomeController.php +++ b/src/svc-main/controllers/HomeController.php @@ -21,6 +21,8 @@ class HomeController extends Controller */ public function index(int $page = 1): void { + $redis = Flight::get('redis'); + // если пришёл любой get-параметр, то считаем его как id плейлиста и перебрасываем на страницу о нём if (Flight::request()->query->count() > 0) { $id = Flight::request()->query->keys()[0]; diff --git a/src/svc-main/core/Bootstrapper.php b/src/svc-main/core/Bootstrapper.php index 3d7526f..fca7f4f 100644 --- a/src/svc-main/core/Bootstrapper.php +++ b/src/svc-main/core/Bootstrapper.php @@ -71,4 +71,21 @@ final class Bootstrapper Flight::route($route, $handler); } } + + public static function bootRedis(): void + { + $options = [ + 'host' => config('redis.host'), + 'port' => config('redis.port'), + // 'username' => config('redis.user'), + // 'pass' => config('redis.password'), + 'connectTimeout' => 1, + ]; + + $redis = new \Redis($options); + $redis->select((int)config('redis.database')); + $redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_JSON); + + Flight::set('redis', $redis); + } } diff --git a/src/svc-main/public/index.php b/src/svc-main/public/index.php index 078082c..ab241e3 100644 --- a/src/svc-main/public/index.php +++ b/src/svc-main/public/index.php @@ -17,4 +17,5 @@ Bootstrapper::bootSettings(); Bootstrapper::bootTwig(); Bootstrapper::bootCore(); Bootstrapper::bootRoutes(); +Bootstrapper::bootRedis(); Flight::start();