Skip to content

Commit 50e08ae

Browse files
committed
add drift server commands to console
1 parent 7e90440 commit 50e08ae

File tree

6 files changed

+124
-8
lines changed

6 files changed

+124
-8
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"php": "^7.4|^8.0",
1818
"antidot-fw/framework": "^0.2.0",
1919
"beberlei/assert": "^3.3",
20+
"drift/server": "^0.1.18",
2021
"psr/container": "^1.0.0",
2122
"ramsey/uuid": "^4.1",
2223
"react/http": "^1.2"

src/Container/Config/ConfigProvider.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@
55
use Antidot\Application\Http\Application;
66
use Antidot\React\LoopFactory;
77
use Antidot\React\ReactApplicationFactory;
8+
use Antidot\React\RunServerCommandFactory;
89
use Antidot\React\ServerFactory;
910
use Antidot\React\SocketFactory;
11+
use Antidot\React\WatchServerCommandFactory;
12+
use Drift\Server\Console\RunServerCommand;
13+
use Drift\Server\Console\WatchServerCommand;
1014
use React\EventLoop\LoopInterface;
1115
use React\Http\Server;
1216
use React\Socket\Server as Socket;
@@ -29,12 +33,23 @@ public function __invoke(): array
2933
Socket::class => SocketFactory::class,
3034
],
3135
],
36+
'console' => [
37+
'commands' => [
38+
'server:run' => RunServerCommand::class,
39+
'server:watch' => WatchServerCommand::class
40+
],
41+
'factories' => [
42+
RunServerCommand::class => RunServerCommandFactory::class,
43+
WatchServerCommand::class => WatchServerCommandFactory::class,
44+
],
45+
],
3246
'server' => [
47+
'host' => '0.0.0.0',
48+
'port' => 5555,
49+
'buffer_size' => 4096,
50+
'max_concurrency' => 100,
3351
'workers' => 1,
34-
'host' => self::DEFAULT_HOST,
35-
'port' => self::DEFAULT_PORT,
36-
'max_concurrency' => self::DEFAULT_CONCURRENCY,
37-
'buffer_size' => self::DEFAULT_BUFFER_SIZE,
52+
'static_folder' => 'public'
3853
]
3954
];
4055
}

src/DriftKernelAdapter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function handle(ServerRequestInterface $request): PromiseResponse
7171

7272
public static function getStaticFolder(): ?string
7373
{
74-
return '/public';
74+
return 'public';
7575
}
7676

7777
public function shutDown(): PromiseInterface

src/RunServerCommandFactory.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Antidot\React;
6+
7+
use Drift\Server\Console\RunServerCommand;
8+
use Psr\Container\ContainerInterface;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
11+
class RunServerCommandFactory
12+
{
13+
public function __invoke(ContainerInterface $container): RunServerCommand
14+
{
15+
/** @var array<string, array> $globalConfig */
16+
$globalConfig = $container->get('config');
17+
/** @var array<string, string|int> $config */
18+
$config = $globalConfig['server'];
19+
20+
$command = new RunServerCommand(dirname('./'), 'server:run');
21+
$command->setDescription('Run Drift HTTP Server');
22+
$definition = $command->getDefinition();
23+
24+
$path = new InputArgument('path', InputArgument::OPTIONAL, $definition->getArgument('path')->getDescription());
25+
$path->setDefault(sprintf('%s:%s', $config['host'], $config['port']));
26+
$definition->setArguments([$path]);
27+
28+
$adapter = $definition->getOption('adapter');
29+
$adapter->setDefault(DriftKernelAdapter::class);
30+
$staticFolder = $definition->getOption('static-folder');
31+
$staticFolder->setDefault($config['static_folder']);
32+
$workers = $definition->getOption('workers');
33+
$workers->setDefault($config['workers']);
34+
$concurrentRequests = $definition->getOption('concurrent-requests');
35+
$concurrentRequests->setDefault($config['max_concurrency']);
36+
$bufferSize = $definition->getOption('request-body-buffer');
37+
$bufferSize->setDefault($config['buffer_size']);
38+
39+
return $command;
40+
}
41+
}

src/WatchServerCommandFactory.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Antidot\React;
6+
7+
use Drift\Server\Console\WatchServerCommand;
8+
use Psr\Container\ContainerInterface;
9+
use Symfony\Component\Console\Input\InputArgument;
10+
11+
class WatchServerCommandFactory
12+
{
13+
public function __invoke(ContainerInterface $container): WatchServerCommand
14+
{
15+
/** @var array<string, array> $globalConfig */
16+
$globalConfig = $container->get('config');
17+
/** @var array<string, string|int> $config */
18+
$config = $globalConfig['server'];
19+
20+
$command = new WatchServerCommand(dirname('./'), [
21+
'bin/console',
22+
'server:run',
23+
sprintf('--adapter=%s', DriftKernelAdapter::class),
24+
'--debug',
25+
], 'server:watch');
26+
$definition = $command->getDefinition();
27+
$command->setDescription('Watch Drift HTTP Server for development purposes');
28+
29+
$path = new InputArgument('path', InputArgument::OPTIONAL, $definition->getArgument('path')->getDescription());
30+
$path->setDefault(sprintf('%s:%s', $config['host'], $config['port']));
31+
$definition->setArguments([$path]);
32+
33+
$staticFolder = $definition->getOption('static-folder');
34+
$staticFolder->setDefault($config['static_folder']);
35+
$concurrentRequests = $definition->getOption('concurrent-requests');
36+
$concurrentRequests->setDefault($config['max_concurrency']);
37+
$bufferSize = $definition->getOption('request-body-buffer');
38+
$bufferSize->setDefault($config['buffer_size']);
39+
40+
return $command;
41+
}
42+
}

test/Container/Config/ConfigProviderTest.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace AntidotTest\React\Container\Config;
46

57
use Antidot\Application\Http\Application;
68
use Antidot\React\Container\Config\ConfigProvider;
79
use Antidot\React\LoopFactory;
810
use Antidot\React\ReactApplicationFactory;
11+
use Antidot\React\RunServerCommandFactory;
912
use Antidot\React\ServerFactory;
1013
use Antidot\React\SocketFactory;
14+
use Antidot\React\WatchServerCommandFactory;
15+
use Drift\Server\Console\RunServerCommand;
16+
use Drift\Server\Console\WatchServerCommand;
1117
use PHPUnit\Framework\TestCase;
1218
use React\EventLoop\LoopInterface;
1319
use React\Http\Server;
@@ -29,12 +35,23 @@ public function testItShouldReturnTheConfigArray(): void
2935
Socket::class => SocketFactory::class,
3036
]
3137
],
38+
'console' => [
39+
'commands' => [
40+
'server:run' => RunServerCommand::class,
41+
'server:watch' => WatchServerCommand::class
42+
],
43+
'factories' => [
44+
RunServerCommand::class => RunServerCommandFactory::class,
45+
WatchServerCommand::class => WatchServerCommandFactory::class,
46+
],
47+
],
3248
'server' => [
33-
'workers' => 1,
3449
'host' => '0.0.0.0',
35-
'port' => 8080,
50+
'port' => 5555,
51+
'buffer_size' => 4096,
3652
'max_concurrency' => 100,
37-
'buffer_size' => 4194304,
53+
'workers' => 1,
54+
'static_folder' => 'public'
3855
]
3956
],
4057
$configProvider(),

0 commit comments

Comments
 (0)