diff --git a/ws/common/Listener/Currencies.php b/ws/common/Listener/Currencies.php new file mode 100644 index 0000000..c62ae6d --- /dev/null +++ b/ws/common/Listener/Currencies.php @@ -0,0 +1,76 @@ +find(Currency::class)->many(); + array_walk($currencies, function(&$item) { + $item = $item->asArray(); + }); + $response->getBody()->write(compact('currencies')); + return $response; + } + public function get(Request $request, Response $response, ModelFactory $factory): Response { + $currency_id = $request->getBody()->read()['currency_id']; + $currency = $factory->find(Currency::class)->one($currency_id); + $response->getBody()->write(['currency' => $currency->asArray()]); + return $response; + } + public function latest(Request $request, Response $response, ModelFactory $factory): Response { + $currency_id = $request->getBody()->read()['currency_id']; + $currency = $factory->find(Currency::class)->one($currency_id); + $output = [ + 'currency' => null, + 'value' => null + ]; + if ($currency) { + $output['currency'] = $currency->asArray(); + if ($currency->latest()) { + $output['value'] = $currency->latest()->asArray(); + } + } + $response->getBody()->write($output); + return $response; + } + public function getSources(Request $request, Response $response, ModelFactory $factory): Response { + $currency_id = $request->getBody()->read()['currency_id']; + $currency = $factory->find(Currency::class)->one($currency_id); + $output = [ + 'currency' => null, + 'sources' => [] + ]; + if ($currency) { + $output['currency'] = $currency->asArray(); + if ($currency->sources()) { + $output['sources'] = array_map(function($item) { + return $item->asArray(); + }, $currency->sources()); + } + } + $response->getBody()->write($output); + return $response; + } + public function getValues(Request $request, Response $response, ModelFactory $factory): Response { + $currency_id = $request->getBody()->read()['currency_id']; + $currency = $factory->find(Currency::class)->one($currency_id); + $output = [ + 'currency' => null, + 'values' => [] + ]; + if ($currency) { + $output['currency'] = $currency->asArray(); + if ($currency->values()) { + $output['values'] = array_map(function($item) { + return $item->asArray(); + }, $currency->values()); + } + } + $response->getBody()->write($output); + return $response; + } +} diff --git a/ws/composer.json b/ws/composer.json new file mode 100644 index 0000000..48c93a0 --- /dev/null +++ b/ws/composer.json @@ -0,0 +1,43 @@ +{ + "name": "provm/money-ws", + "description": "Websocket para la aplicacion web para revisar los valores de distintas monedas", + "type": "project", + "require": { + "php-di/php-di": "^6.3", + "provm/models": "dev-master", + "vlucas/phpdotenv": "^5.3", + "nesbot/carbon": "^2.46", + "provm/events": "dev-master" + }, + "require-dev": { + "phpunit/phpunit": "^9.5", + "kint-php/kint": "^3.3" + }, + "license": "MIT", + "authors": [ + { + "name": "Aldarien", + "email": "aldarien85@gmail.com" + } + ], + "autoload": { + "psr-4": { + "ProVM\\Money\\Common\\": "common", + "ProVM\\Common\\": "../provm/common", + "ProVM\\Money\\": "../src" + } + }, + "repositories": [ + { + "type": "git", + "url": "http://git.provm.cl/ProVM/models.git" + }, + { + "type": "git", + "url": "http://git.provm.cl/ProVM/events.git" + } + ], + "config": { + "secure-http": false + } +} diff --git a/ws/docker/PHP.Dockerfile b/ws/docker/PHP.Dockerfile new file mode 100644 index 0000000..b26aaba --- /dev/null +++ b/ws/docker/PHP.Dockerfile @@ -0,0 +1,8 @@ +FROM php:7.4-fpm + +RUN docker-php-ext-install pdo pdo_mysql + +RUN pecl install xdebug-3.0.3 \ + && docker-php-ext-enable xdebug + +CMD ["php", "/code/ws/public/index.php"] diff --git a/ws/docker/nginx.conf b/ws/docker/nginx.conf new file mode 100644 index 0000000..487119a --- /dev/null +++ b/ws/docker/nginx.conf @@ -0,0 +1,16 @@ +upstream websocket { + server ws-php:9010; +} + +server { + listen 80; + + location / { + proxy_pass http://websocket; + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + proxy_set_header Host $host; + proxy_read_timeout 86400; + } +} diff --git a/ws/public/index.php b/ws/public/index.php new file mode 100644 index 0000000..da7da19 --- /dev/null +++ b/ws/public/index.php @@ -0,0 +1,7 @@ +run(); diff --git a/ws/resources/routes/ws.php b/ws/resources/routes/ws.php new file mode 100644 index 0000000..21224de --- /dev/null +++ b/ws/resources/routes/ws.php @@ -0,0 +1,8 @@ +isDir()) { + continue; + } + include_once $file->getRealPath(); +} diff --git a/ws/resources/routes/ws/currencies.php b/ws/resources/routes/ws/currencies.php new file mode 100644 index 0000000..6c668d4 --- /dev/null +++ b/ws/resources/routes/ws/currencies.php @@ -0,0 +1,9 @@ +add('currencies', $controller); +$app->add('currency', [$controller, 'get']); +$app->add('currency.values.latest', [$controller, 'latest']); +$app->add('currency.sources', [$controller, 'getSources']); +$app->add('currency.values', [$controller, 'getValues']); diff --git a/ws/setup/app.php b/ws/setup/app.php new file mode 100644 index 0000000..5b70205 --- /dev/null +++ b/ws/setup/app.php @@ -0,0 +1,45 @@ +addDefinitions($filename); + } +} + +$container = $builder->build(); +$app = $container->get(Ratchet\Server\IoServer::class); + +include_once 'databases.php'; +include_once 'router.php'; diff --git a/ws/setup/composer.php b/ws/setup/composer.php new file mode 100644 index 0000000..2d27ee1 --- /dev/null +++ b/ws/setup/composer.php @@ -0,0 +1,6 @@ +getContainer()->get('databases'); +foreach ($databases as $name => $settings) { + switch($settings->system) { + case 'mysql': + $dsn = implode(':', [ + 'mysql', + implode(';', [ + implode('=', [ + 'host', + $settings->host->name + ]), + implode('=', [ + 'dbname', + $settings->name + ]) + ]) + ]); + if (isset($settings->host->port)) { + $dsn .= ';' . implode('=', [ + 'port', + $settings->host->port + ]); + } + break; + } + ORM::configure($dsn, null, $name); + switch ($settings->system) { + case 'mysql': + ORM::configure('username', $settings->user->name, $name); + ORM::configure('password', $settings->user->password, $name); + } +} +if (isset($databases->short_names)) { + Model::$short_table_names = $databases->short_names; +} diff --git a/ws/setup/router.php b/ws/setup/router.php new file mode 100644 index 0000000..13859ed --- /dev/null +++ b/ws/setup/router.php @@ -0,0 +1,5 @@ +getContainer()->get('locations')->routes, + 'ws.php' +]); diff --git a/ws/setup/ws/settings.php b/ws/setup/ws/settings.php new file mode 100644 index 0000000..4fa708a --- /dev/null +++ b/ws/setup/ws/settings.php @@ -0,0 +1,19 @@ + DI\decorate(function($prev, Container $c) { + $arr = (array) $prev; + $arr['base'] = dirname(__DIR__, 2); + $arr['resources'] = implode(DIRECTORY_SEPARATOR, [ + $arr['base'], + 'resources' + ]); + $arr['routes'] = implode(DIRECTORY_SEPARATOR, [ + $arr['resources'], + 'routes' + ]); + return (object) $arr; + }), + 'port' => '9010' +]; diff --git a/ws/setup/ws/setups.php b/ws/setup/ws/setups.php new file mode 100644 index 0000000..399d5e9 --- /dev/null +++ b/ws/setup/ws/setups.php @@ -0,0 +1,36 @@ + function(Container $c) { + return new \SplObjectStorage(); + }, + ProVM\Common\Factory\Event\Request::class => function(Container $c) { + return new ProVM\Common\Factory\Event\Request(); + }, + ProVM\Common\Factory\Event\Response::class => function(Container $c) { + return new ProVM\Common\Factory\Event\Response(); + }, + ProVM\Common\Factory\Event\Listener::class => function(Container $c) { + return new ProVM\Common\Factory\Event\Listener($c); + }, + Psr\EventDispatcher\EventDispatcherInterface::class => function(Container $c) { + return new ProVM\Common\Service\Event\Dispatcher($c); + }, + Ratchet\MessageComponentInterface::class => function(Container $c) { + return (new ProVM\Common\Alias\Event\Message($c->get('storage'))) + ->setDispatcher($c->get(Psr\EventDispatcher\EventDispatcherInterface::class)) + ->setRequestBuilder($c->get(ProVM\Common\Factory\Event\Request::class)); + }, + Ratchet\WebSocket\WsServer::class => function(Container $c) { + return new Ratchet\WebSocket\WsServer($c->get(Ratchet\MessageComponentInterface::class)); + }, + Ratchet\Http\HttpServer::class => function(Container $c) { + return new Ratchet\Http\HttpServer($c->get(Ratchet\WebSocket\WsServer::class)); + }, + Ratchet\Server\IoServer::class => function(Container $c) { + $app = ProVM\Common\Alias\Server\App::factory($c->get(Ratchet\Http\HttpServer::class), $c->get('port')); + $app->setContainer($c); + return $app; + } +];