Commit 9edd7c6f authored by François Agneray's avatar François Agneray
Browse files

Merge branch '38-actions-services' into 'develop'

Resolve "Actions services"

Closes #38

See merge request !38
parents 5a7a2f36 0ad7dfbe
Pipeline #1530 failed with stages
in 3 minutes and 59 seconds
AMQP_HOST=amqp_host
\ No newline at end of file
......@@ -14,4 +14,5 @@ cache
.project
.settings
.phpunit*
anis_v3.sqlite
\ No newline at end of file
anis_v3.sqlite
.env
\ No newline at end of file
......@@ -24,7 +24,7 @@ install:
@docker run --init -it --rm --user $(UID):$(GID) \
-e COMPOSER_CACHE_DIR=/dev/null \
-v $(CURDIR):/project \
-w /project jakzal/phpqa composer install
-w /project jakzal/phpqa composer install --ignore-platform-reqs
up:
@docker-compose up --build -d
......
This diff is collapsed.
......@@ -3,7 +3,7 @@ FROM php:7.3-apache
# Install modules
RUN apt-get update \
&& apt-get install -y zlib1g zlib1g-dev libpq-dev libpq5 libzip-dev zip unzip \
&& docker-php-ext-install pgsql pdo_pgsql zip
&& docker-php-ext-install pgsql pdo_pgsql zip bcmath
# Install pecl modules
RUN pecl install xdebug \
......
......@@ -20,6 +20,10 @@ services:
LOGGER_NAME: "anis-v3-server"
LOGGER_PATH: "php://stdout"
LOGGER_LEVEL: "debug"
AMQP_HOST: ${AMQP_HOST}
AMQP_PORT: 5672
AMQP_USER: "guest"
AMQP_PASSWORD: "guest"
ports:
- 8080:80
volumes:
......
<?php
/*
* This file is part of ANIS SERVER API.
*
* (c) François Agneray <francois.agneray@lam.fr>
* (c) Chrystel Moreau <chrystel.moreau@lam.fr>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Action\Search;
use Psr\Log\LoggerInterface;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Message\ResponseInterface as Response;
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;
use App\Utils\ActionTrait;
use App\Utils\MetaEntityManagerFactory;
final class ServiceAction
{
use ActionTrait;
private $logger;
private $memf;
private $amqp;
public function __construct(LoggerInterface $logger, MetaEntityManagerFactory $memf, AMQPStreamConnection $amqp)
{
$this->logger = $logger;
$this->memf = $memf;
$this->amqp = $amqp;
}
public function __invoke(Request $request, Response $response, array $args): Response
{
$this->logger->info('Service action dispatched');
if ($request->isOptions()) {
return $response->withHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
}
// Create the doctrine Entity Manager object for the instance metamodel database
$this->memf->createMetaEntityManager($args['instance']);
$dataset = $this->memf->getMetaEntityManager()->find('App\Entity\Metamodel\Dataset', $args['dname']);
if (is_null($dataset)) {
return $this->dispatchHttpError(
$response,
'Invalid request',
'Dataset with id ' . $args['dname'] . ' is not found'
)->withStatus(404);
}
$queryParams = $request->getQueryParams();
if (!array_key_exists('a', $queryParams)) {
return $this->dispatchHttpError(
$response,
'Invalid request',
'Param a is required for this request'
)->withStatus(400);
}
$searchUri = '/search/' . $args['instance'] . '/data/' . $args['dname'];
$searchUri .= '?a=' . $queryParams['a'];
if (array_key_exists('c', $queryParams)) {
$searchUri .= '&c=' . $queryParams['c'];
}
$uniqId = uniqid();
$channel = $this->amqp->channel();
$channel->queue_declare('csv', false, false, false, false);
$msg = new AMQPMessage(json_encode(array('search' => $searchUri, 'uniqid' => $uniqId)));
$channel->basic_publish($msg, '', 'csv');
$channel->close();
$this->amqp->close();
return $response->withJson(array('message' => $uniqId));
}
}
......@@ -56,6 +56,18 @@ $container['mailer'] = function ($c) {
return $mailer;
};
// RabbitMQ
$container['amqp'] = function ($c) {
$settings = $c->get('settings');
$amqp = new \PhpAmqpLib\Connection\AMQPStreamConnection(
$settings['amqp']['host'],
$settings['amqp']['port'],
$settings['amqp']['user'],
$settings['amqp']['password']
);
return $amqp;
};
// MetaEntityManagerFactory
$container['memf'] = function ($c) {
$adminEntityManager = $c->get('aem');
......@@ -251,3 +263,7 @@ $container['App\Action\Search\SearchAction'] = function ($c) {
$settings['encryption_key']
);
};
$container['App\Action\Search\ServiceAction'] = function ($c) {
return new App\Action\Search\ServiceAction($c->get('logger'), $c->get('memf'), $c->get('amqp'));
};
......@@ -62,3 +62,4 @@ $app->group('/metadata', function (Slim\App $app) {
});
$app->map(['OPTIONS', 'GET'], '/search/{instance}/{type}/{dname}', App\Action\Search\SearchAction::class);
$app->map(['OPTIONS', 'GET'], '/service/{instance}/{dname}', App\Action\Search\ServiceAction::class);
......@@ -41,6 +41,13 @@ return [
'mailer' => [
'host' => getenv('MAILER_HOST'),
'port' => getenv('MAILER_PORT')
],
// RabbitMQ settings
'amqp' => [
'host' => getenv('AMQP_HOST'),
'port' => getenv('AMQP_PORT'),
'user' => getenv('AMQP_USER'),
'password' => getenv('AMQP_PASSWORD')
]
]
];
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment