1
0
Fork 0
mirror of https://codeberg.org/demostf/api.git synced 2026-06-03 09:54:17 +02:00

use parser binary

This commit is contained in:
Robin Appelman 2019-03-05 23:04:30 +01:00
commit 334dfb3771
6 changed files with 25 additions and 35 deletions

View file

@ -1,16 +1,17 @@
FROM php:7.1-fpm-alpine FROM registry.gitlab.com/rust_musl_docker/image:stable-latest AS build
WORKDIR /root/build
RUN git clone https://github.com/demostf/parser
WORKDIR /root/build/parser
RUN cargo build --release --target=x86_64-unknown-linux-musl
RUN apk add --no-cache postgresql-dev wget autoconf g++ libc-dev make pcre-dev \ FROM icewind1991/php-alpine-apcu
&& mkdir -p /app/src \
&& docker-php-ext-install pdo_pgsql \
&& pecl install apcu \
&& docker-php-ext-enable apcu \
&& apk del autoconf g++ libc-dev make pcre-dev \
&& sed -i -- 's/www-data/nobody/g' /usr/local/etc/php-fpm.d/www.conf
COPY --from=build /root/build/parser/target/x86_64-unknown-linux-musl/release/parse_demo /app/parse_demo
COPY composer.json /app COPY composer.json /app
COPY src /app/src COPY src /app/src
ENV PARSER_PATH /app/parse_demo
RUN wget https://getcomposer.org/composer.phar \ RUN wget https://getcomposer.org/composer.phar \
&& php composer.phar --working-dir=/app install --no-dev --no-interaction --ignore-platform-reqs \ && php composer.phar --working-dir=/app install --no-dev --no-interaction --ignore-platform-reqs \
&& rm composer.phar && rm composer.phar

View file

@ -12,7 +12,7 @@ node_modules: package.json
.PHONY: mocha .PHONY: mocha
mocha: node_modules mocha: node_modules
DEMO_ROOT=/tmp/demos DB_PORT=5433 DB_TYPE=pgsql DB_HOST=localhost DB_USERNAME=postgres DB_USERNAME=postgres DB_PASSWORD=test DB_DATABASE=postgres\ DEMO_ROOT=/tmp/demos DB_PORT=5433 DB_TYPE=pgsql DB_HOST=localhost DB_USERNAME=postgres DB_USERNAME=postgres DB_PASSWORD=test DB_DATABASE=postgres\
node node_modules/.bin/mocha --recursive PARSER_PATH=/home/robin/Projects/demostf/tf-demo-parser/target/release/parse_demo node node_modules/.bin/mocha --recursive
.PHONY: phpunit .PHONY: phpunit
phpunit: phpunit:

View file

@ -28,7 +28,7 @@ class Container {
private $connection; private $connection;
private $generator; private $generator;
private $baseUrl; private $baseUrl;
private $parserUrl; private $parserPath;
private $storeRoot; private $storeRoot;
private $storeUrl; private $storeUrl;
private $apiRoot; private $apiRoot;
@ -43,7 +43,7 @@ class Container {
Connection $connection, Connection $connection,
Generator $generator, Generator $generator,
string $baseUrl, string $baseUrl,
string $parserUrl, string $parserPath,
string $storeRoot, string $storeRoot,
string $storeUrl, string $storeUrl,
string $apiRoot, string $apiRoot,
@ -55,7 +55,7 @@ class Container {
$this->connection = $connection; $this->connection = $connection;
$this->generator = $generator; $this->generator = $generator;
$this->baseUrl = $baseUrl; $this->baseUrl = $baseUrl;
$this->parserUrl = $parserUrl; $this->parserPath = $parserPath;
$this->storeRoot = $storeRoot; $this->storeRoot = $storeRoot;
$this->storeUrl = $storeUrl; $this->storeUrl = $storeUrl;
$this->apiRoot = $apiRoot; $this->apiRoot = $apiRoot;
@ -92,7 +92,7 @@ class Container {
} }
public function getRawParser(): RawParser { public function getRawParser(): RawParser {
return new RawParser($this->getParserUrl(), new TempController($this->getApiRoot() . '/temp/')); return new RawParser($this->getParserPath(), new TempController($this->getApiRoot() . '/temp/'));
} }
public function getUploadProvider(): UploadProvider { public function getUploadProvider(): UploadProvider {
@ -127,8 +127,8 @@ class Container {
return $this->baseUrl; return $this->baseUrl;
} }
public function getParserUrl(): string { public function getParserPath(): string {
return $this->parserUrl; return $this->parserPath;
} }
public function getStoreRoot(): string { public function getStoreRoot(): string {

View file

@ -16,38 +16,26 @@ use function GuzzleHttp\Psr7\stream_for;
*/ */
class RawParser { class RawParser {
/** @var string */ /** @var string */
private $parserUrl; private $parserPath;
private $tempController; private $tempController;
public function __construct(string $parserUrl, TempController $tempController) { public function __construct(string $parserPath, TempController $tempController) {
$this->parserUrl = $parserUrl; $this->parserPath = $parserPath;
$this->tempController = $tempController; $this->tempController = $tempController;
} }
public function parse(string $path): ?array { public function parse(string $path): ?array {
$key = md5($path);
$url = $this->tempController->register($key, $path);
try { try {
$client = new Client(); $command = $this->parserPath . " " . escapeshellarg($path);
$response = $client->post($this->parserUrl, [ $output = shell_exec($command);
'body' => stream_for($url), $result = \GuzzleHttp\json_decode($output, true);
'headers' => [
'Content-Type' => 'application/octet-stream',
],
]);
$result = json_decode($response->getBody()->getContents(), true);
$this->tempController->unregister($key);
if (null === $result) { if (null === $result) {
throw new \Exception('Failed to parse demo, unexpected result from parser'); throw new \Exception('Failed to parse demo, unexpected result from parser');
} else { } else {
return $result; return $result;
} }
} catch (RequestException $e) { } catch (RequestException $e) {
$this->tempController->unregister($key);
if (false !== strpos($e->getMessage(), 'cURL error 52')) {
throw new \Exception('Failed to parse demo, can\'t reach demo parser');
}
throw new \Exception('Failed to parse demo, ' . $e->getMessage() . ' ' . $url); throw new \Exception('Failed to parse demo, ' . $e->getMessage() . ' ' . $url);
} }
} }

View file

@ -25,7 +25,7 @@ $db = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);
$host = getenv('BASE_HOST') ?: ''; $host = getenv('BASE_HOST') ?: '';
$storeRoot = getenv('DEMO_ROOT') ?: ''; $storeRoot = getenv('DEMO_ROOT') ?: '';
$storeHost = getenv('DEMO_HOST') ?: ''; $storeHost = getenv('DEMO_HOST') ?: '';
$parserUrl = getenv('PARSER_URL') ?: ''; $parserPath = getenv('PARSER_PATH') ?: '';
$appRoot = getenv('APP_ROOT') ?: ''; $appRoot = getenv('APP_ROOT') ?: '';
$editKey = getenv('EDIT_SECRET') ?: ''; $editKey = getenv('EDIT_SECRET') ?: '';
$uploadKey = getenv('UPLOAD_KEY') ?: ''; $uploadKey = getenv('UPLOAD_KEY') ?: '';
@ -39,7 +39,7 @@ $container = new Container(
$db, $db,
$generator, $generator,
'https://' . $host, 'https://' . $host,
$parserUrl, $parserPath,
$storeRoot, $storeRoot,
$storeHost, $storeHost,
$appRoot, $appRoot,

View file

@ -104,6 +104,7 @@ function uploadDemo(file) {
key: 'key1' key: 'key1'
} }
}).then((response) => { }).then((response) => {
console.log(`body: "${response.body}"`);
return parseInt(response.body.match(/\/(\d+)/)[1], 10); return parseInt(response.body.match(/\/(\d+)/)[1], 10);
}); });
} }