WebSocket app
This commit is contained in:
76
ws/common/Listener/Currencies.php
Normal file
76
ws/common/Listener/Currencies.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?php
|
||||
namespace ProVM\Money\Common\Listener;
|
||||
|
||||
use ProVM\Common\Define\Event\Request;
|
||||
use ProVM\Common\Define\Event\Response;
|
||||
use ProVM\Common\Factory\Model as ModelFactory;
|
||||
use ProVM\Money\Currency;
|
||||
|
||||
class Currencies {
|
||||
public function __invoke(Request $request, Response $response, ModelFactory $factory): Response {
|
||||
$currencies = $factory->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;
|
||||
}
|
||||
}
|
43
ws/composer.json
Normal file
43
ws/composer.json
Normal file
@ -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
|
||||
}
|
||||
}
|
8
ws/docker/PHP.Dockerfile
Normal file
8
ws/docker/PHP.Dockerfile
Normal file
@ -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"]
|
16
ws/docker/nginx.conf
Normal file
16
ws/docker/nginx.conf
Normal file
@ -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;
|
||||
}
|
||||
}
|
7
ws/public/index.php
Normal file
7
ws/public/index.php
Normal file
@ -0,0 +1,7 @@
|
||||
<?php
|
||||
include_once implode(DIRECTORY_SEPARATOR, [
|
||||
dirname(__DIR__),
|
||||
'setup',
|
||||
'app.php'
|
||||
]);
|
||||
$app->run();
|
8
ws/resources/routes/ws.php
Normal file
8
ws/resources/routes/ws.php
Normal file
@ -0,0 +1,8 @@
|
||||
<?php
|
||||
$files = new DirectoryIterator(implode(DIRECTORY_SEPARATOR, [__DIR__, 'ws']));
|
||||
foreach ($files as $file) {
|
||||
if ($file->isDir()) {
|
||||
continue;
|
||||
}
|
||||
include_once $file->getRealPath();
|
||||
}
|
9
ws/resources/routes/ws/currencies.php
Normal file
9
ws/resources/routes/ws/currencies.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
use ProVM\Money\Common\Listener\Currencies;
|
||||
|
||||
$controller = new Currencies();
|
||||
$app->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']);
|
45
ws/setup/app.php
Normal file
45
ws/setup/app.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
use DI\ContainerBuilder as Builder;
|
||||
|
||||
include_once 'composer.php';
|
||||
|
||||
$builder = new Builder();
|
||||
|
||||
$folders = [
|
||||
implode(DIRECTORY_SEPARATOR, [
|
||||
dirname(__DIR__, 2),
|
||||
'setup',
|
||||
'env'
|
||||
]),
|
||||
implode(DIRECTORY_SEPARATOR, [
|
||||
dirname(__DIR__, 2),
|
||||
'setup',
|
||||
'common'
|
||||
]),
|
||||
implode(DIRECTORY_SEPARATOR, [
|
||||
__DIR__,
|
||||
'ws'
|
||||
])
|
||||
];
|
||||
$files = [
|
||||
'settings',
|
||||
'setups'
|
||||
];
|
||||
foreach ($files as $file) {
|
||||
foreach ($folders as $folder) {
|
||||
$filename = implode(DIRECTORY_SEPARATOR, [
|
||||
$folder,
|
||||
$file . '.php'
|
||||
]);
|
||||
if (!file_exists($filename)) {
|
||||
continue;
|
||||
}
|
||||
$builder->addDefinitions($filename);
|
||||
}
|
||||
}
|
||||
|
||||
$container = $builder->build();
|
||||
$app = $container->get(Ratchet\Server\IoServer::class);
|
||||
|
||||
include_once 'databases.php';
|
||||
include_once 'router.php';
|
6
ws/setup/composer.php
Normal file
6
ws/setup/composer.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
include_once implode(DIRECTORY_SEPARATOR, [
|
||||
dirname(__DIR__),
|
||||
'vendor',
|
||||
'autoload.php'
|
||||
]);
|
36
ws/setup/databases.php
Normal file
36
ws/setup/databases.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
$databases = $app->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;
|
||||
}
|
5
ws/setup/router.php
Normal file
5
ws/setup/router.php
Normal file
@ -0,0 +1,5 @@
|
||||
<?php
|
||||
include_once implode(DIRECTORY_SEPARATOR, [
|
||||
$app->getContainer()->get('locations')->routes,
|
||||
'ws.php'
|
||||
]);
|
19
ws/setup/ws/settings.php
Normal file
19
ws/setup/ws/settings.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
use Psr\Container\ContainerInterface as Container;
|
||||
|
||||
return [
|
||||
'locations' => 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'
|
||||
];
|
36
ws/setup/ws/setups.php
Normal file
36
ws/setup/ws/setups.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
use Psr\Container\ContainerInterface as Container;
|
||||
|
||||
return [
|
||||
'storage' => 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;
|
||||
}
|
||||
];
|
Reference in New Issue
Block a user