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:
parent
9d3e175544
commit
334dfb3771
6 changed files with 25 additions and 35 deletions
17
Dockerfile
17
Dockerfile
|
|
@ -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 \
|
||||
&& 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
|
||||
FROM icewind1991/php-alpine-apcu
|
||||
|
||||
COPY --from=build /root/build/parser/target/x86_64-unknown-linux-musl/release/parse_demo /app/parse_demo
|
||||
COPY composer.json /app
|
||||
COPY src /app/src
|
||||
|
||||
ENV PARSER_PATH /app/parse_demo
|
||||
|
||||
RUN wget https://getcomposer.org/composer.phar \
|
||||
&& php composer.phar --working-dir=/app install --no-dev --no-interaction --ignore-platform-reqs \
|
||||
&& rm composer.phar
|
||||
|
|
|
|||
2
Makefile
2
Makefile
|
|
@ -12,7 +12,7 @@ node_modules: package.json
|
|||
.PHONY: mocha
|
||||
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\
|
||||
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
|
||||
phpunit:
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ class Container {
|
|||
private $connection;
|
||||
private $generator;
|
||||
private $baseUrl;
|
||||
private $parserUrl;
|
||||
private $parserPath;
|
||||
private $storeRoot;
|
||||
private $storeUrl;
|
||||
private $apiRoot;
|
||||
|
|
@ -43,7 +43,7 @@ class Container {
|
|||
Connection $connection,
|
||||
Generator $generator,
|
||||
string $baseUrl,
|
||||
string $parserUrl,
|
||||
string $parserPath,
|
||||
string $storeRoot,
|
||||
string $storeUrl,
|
||||
string $apiRoot,
|
||||
|
|
@ -55,7 +55,7 @@ class Container {
|
|||
$this->connection = $connection;
|
||||
$this->generator = $generator;
|
||||
$this->baseUrl = $baseUrl;
|
||||
$this->parserUrl = $parserUrl;
|
||||
$this->parserPath = $parserPath;
|
||||
$this->storeRoot = $storeRoot;
|
||||
$this->storeUrl = $storeUrl;
|
||||
$this->apiRoot = $apiRoot;
|
||||
|
|
@ -92,7 +92,7 @@ class Container {
|
|||
}
|
||||
|
||||
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 {
|
||||
|
|
@ -127,8 +127,8 @@ class Container {
|
|||
return $this->baseUrl;
|
||||
}
|
||||
|
||||
public function getParserUrl(): string {
|
||||
return $this->parserUrl;
|
||||
public function getParserPath(): string {
|
||||
return $this->parserPath;
|
||||
}
|
||||
|
||||
public function getStoreRoot(): string {
|
||||
|
|
|
|||
|
|
@ -16,38 +16,26 @@ use function GuzzleHttp\Psr7\stream_for;
|
|||
*/
|
||||
class RawParser {
|
||||
/** @var string */
|
||||
private $parserUrl;
|
||||
private $parserPath;
|
||||
|
||||
private $tempController;
|
||||
|
||||
public function __construct(string $parserUrl, TempController $tempController) {
|
||||
$this->parserUrl = $parserUrl;
|
||||
public function __construct(string $parserPath, TempController $tempController) {
|
||||
$this->parserPath = $parserPath;
|
||||
$this->tempController = $tempController;
|
||||
}
|
||||
|
||||
public function parse(string $path): ?array {
|
||||
$key = md5($path);
|
||||
$url = $this->tempController->register($key, $path);
|
||||
try {
|
||||
$client = new Client();
|
||||
$response = $client->post($this->parserUrl, [
|
||||
'body' => stream_for($url),
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/octet-stream',
|
||||
],
|
||||
]);
|
||||
$result = json_decode($response->getBody()->getContents(), true);
|
||||
$this->tempController->unregister($key);
|
||||
$command = $this->parserPath . " " . escapeshellarg($path);
|
||||
$output = shell_exec($command);
|
||||
$result = \GuzzleHttp\json_decode($output, true);
|
||||
if (null === $result) {
|
||||
throw new \Exception('Failed to parse demo, unexpected result from parser');
|
||||
} else {
|
||||
return $result;
|
||||
}
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ $db = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);
|
|||
$host = getenv('BASE_HOST') ?: '';
|
||||
$storeRoot = getenv('DEMO_ROOT') ?: '';
|
||||
$storeHost = getenv('DEMO_HOST') ?: '';
|
||||
$parserUrl = getenv('PARSER_URL') ?: '';
|
||||
$parserPath = getenv('PARSER_PATH') ?: '';
|
||||
$appRoot = getenv('APP_ROOT') ?: '';
|
||||
$editKey = getenv('EDIT_SECRET') ?: '';
|
||||
$uploadKey = getenv('UPLOAD_KEY') ?: '';
|
||||
|
|
@ -39,7 +39,7 @@ $container = new Container(
|
|||
$db,
|
||||
$generator,
|
||||
'https://' . $host,
|
||||
$parserUrl,
|
||||
$parserPath,
|
||||
$storeRoot,
|
||||
$storeHost,
|
||||
$appRoot,
|
||||
|
|
|
|||
|
|
@ -104,6 +104,7 @@ function uploadDemo(file) {
|
|||
key: 'key1'
|
||||
}
|
||||
}).then((response) => {
|
||||
console.log(`body: "${response.body}"`);
|
||||
return parseInt(response.body.match(/\/(\d+)/)[1], 10);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue