Skip to content

Commit d28ff60

Browse files
authored
Merge pull request swooletw#183 from fractalzombie/develop
Added hot reload and access log to console
2 parents 6af7b17 + c50a714 commit d28ff60

File tree

77 files changed

+1382
-647
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+1382
-647
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@ script:
3737
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml
3838

3939
after_success:
40-
- vendor/bin/coveralls -v
40+
- vendor/bin/php-coveralls -v

composer.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,17 @@
2626
"illuminate/contracts": "~5.3",
2727
"illuminate/http": "~5.3",
2828
"illuminate/support": "~5.3",
29-
"predis/predis": "^1.1"
29+
"predis/predis": "^1.1",
30+
"ext-json": "*",
31+
"ext-fileinfo": "*",
32+
"ext-pdo": "*",
33+
"ext-pcntl": "*"
3034
},
3135
"require-dev": {
3236
"laravel/lumen-framework": "~5.3",
33-
"phpunit/phpunit": "^6.1",
34-
"phpunit/php-code-coverage": "^5.2",
35-
"satooshi/php-coveralls": "^1.0",
37+
"phpunit/phpunit": "^7.5",
38+
"phpunit/php-code-coverage": "^6.1",
39+
"php-coveralls/php-coveralls": "^2.1",
3640
"mockery/mockery": "~1.0",
3741
"codedungeon/phpunit-result-printer": "^0.14.0",
3842
"php-mock/php-mock": "^2.0"

config/swoole_http.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
'public_path' => base_path('public'),
1616
// Determine if to use swoole to respond request for static files
1717
'handle_static_files' => env('SWOOLE_HANDLE_STATIC', true),
18-
'log' => env('SWOOLE_HTTP_LOG', true),
18+
'access_log' => env('SWOOLE_HTTP_ACCESS_LOG', true),
1919
// You must add --enable-openssl while compiling Swoole
2020
// Put `SWOOLE_SOCK_TCP | SWOOLE_SSL` if you want to enable SSL
2121
'socket_type' => SWOOLE_SOCK_TCP,
@@ -59,11 +59,11 @@
5959
|--------------------------------------------------------------------------
6060
*/
6161
'hot_reload' => [
62-
'enabled' => env('SWOOLE_HOT_RELOAD_ENABLE', true),
63-
'level' => env('SWOOLE_HOT_RELOAD_DEEP_LEVEL', 10),
62+
'enabled' => env('SWOOLE_HOT_RELOAD_ENABLE', false),
63+
'recursively' => env('SWOOLE_HOT_RELOAD_RECURSIVELY', true),
6464
'directory' => env('SWOOLE_HOT_RELOAD_DIRECTORY', base_path()),
6565
'log' => env('SWOOLE_HOT_RELOAD_LOG', true),
66-
'files' => ['*.php'],
66+
'filter' => env('SWOOLE_HOT_RELOAD_FILTER', '.php'),
6767
],
6868

6969
/*

src/Commands/HttpServerCommand.php

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,19 @@
33
namespace SwooleTW\Http\Commands;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Console\OutputStyle;
7+
use Illuminate\Contracts\Container\Container;
68
use Illuminate\Support\Arr;
79
use Swoole\Process;
10+
use SwooleTW\Http\Helpers\Alias;
11+
use SwooleTW\Http\HotReload\FSEvent;
12+
use SwooleTW\Http\HotReload\FSOutput;
13+
use SwooleTW\Http\HotReload\FSProcess;
14+
use SwooleTW\Http\Middlewares\AccessLog;
15+
use SwooleTW\Http\Server\AccessOutput;
16+
use SwooleTW\Http\Server\Facades\Server;
17+
use SwooleTW\Http\Server\Manager;
18+
use Symfony\Component\Console\Output\ConsoleOutput;
819
use Throwable;
920

1021
/**
@@ -66,7 +77,7 @@ public function handle()
6677
*/
6778
protected function loadConfigs()
6879
{
69-
$this->config = $this->laravel->make('config')->get('swoole_http');
80+
$this->config = $this->laravel->make(Alias::CONFIG)->get('swoole_http');
7081
}
7182

7283
/**
@@ -90,17 +101,30 @@ protected function start()
90101

91102
$host = Arr::get($this->config, 'server.host');
92103
$port = Arr::get($this->config, 'server.port');
104+
$hotReloadEnabled = Arr::get($this->config, 'hot_reload.enabled');
105+
$accessLogEnabled = Arr::get($this->config, 'server.access_log');
93106

94107
$this->info('Starting swoole http server...');
95108
$this->info("Swoole http server started: <http://{$host}:{$port}>");
96109
if ($this->isDaemon()) {
97110
$this->info(
98-
'> (You can run this command to ensure the '.
111+
'> (You can run this command to ensure the ' .
99112
'swoole_http_server process is running: ps aux|grep "swoole")'
100113
);
101114
}
102115

103-
$this->laravel->make('swoole.manager')->run();
116+
$manager = $this->laravel->make(Manager::class);
117+
$server = $this->laravel->make(Server::class);
118+
119+
if ($accessLogEnabled) {
120+
$this->registerAccessLog();
121+
}
122+
123+
if ($hotReloadEnabled) {
124+
$manager->addProcess($this->getHotReloadProcess($server));
125+
}
126+
127+
$manager->run();
104128
}
105129

106130
/**
@@ -230,6 +254,26 @@ protected function initAction()
230254
}
231255
}
232256

257+
/**
258+
* @param \SwooleTW\Http\Server\Facades\Server $server
259+
*
260+
* @return \Swoole\Process
261+
*/
262+
protected function getHotReloadProcess($server)
263+
{
264+
$recursively = Arr::get($this->config, 'hot_reload.recursively');
265+
$directory = Arr::get($this->config, 'hot_reload.directory');
266+
$filter = Arr::get($this->config, 'hot_reload.filter');
267+
$log = Arr::get($this->config, 'hot_reload.log');
268+
269+
$cb = function (FSEvent $event) use ($server, $log) {
270+
$log ? $this->info(FSOutput::format($event)) : null;
271+
$server->reload();
272+
};
273+
274+
return (new FSProcess($filter, $recursively, $directory))->make($cb);
275+
}
276+
233277
/**
234278
* If Swoole process is running.
235279
*
@@ -292,7 +336,7 @@ protected function getCurrentPid()
292336
$path = $this->getPidPath();
293337

294338
return $this->currentPid = file_exists($path)
295-
? (int)file_get_contents($path) ?? $this->removePidFile()
339+
? (int) file_get_contents($path) ?? $this->removePidFile()
296340
: null;
297341
}
298342

@@ -332,19 +376,37 @@ protected function checkEnvironment()
332376
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
333377
$this->error("Swoole extension doesn't support Windows OS yet.");
334378

335-
return;
336-
} else {
337-
if (! extension_loaded('swoole')) {
338-
$this->error("Can't detect Swoole extension installed.");
379+
exit(1);
380+
}
339381

340-
return;
341-
} else {
342-
if (! version_compare(swoole_version(), '4.0.0', 'ge')) {
343-
$this->error("Your Swoole version must be higher than 4.0 to use coroutine.");
382+
if (! extension_loaded('swoole')) {
383+
$this->error("Can't detect Swoole extension installed.");
344384

345-
return;
346-
}
347-
}
385+
exit(1);
348386
}
387+
388+
if (! version_compare(swoole_version(), '4.0.0', 'ge')) {
389+
$this->error("Your Swoole version must be higher than 4.0 to use coroutine.");
390+
391+
exit(1);
392+
}
393+
}
394+
395+
/**
396+
* Register access log services.
397+
*/
398+
protected function registerAccessLog()
399+
{
400+
$this->laravel->singleton(OutputStyle::class, function () {
401+
return new OutputStyle($this->input, $this->output);
402+
});
403+
404+
$this->laravel->singleton(AccessOutput::class, function () {
405+
return new AccessOutput(new ConsoleOutput);
406+
});
407+
408+
$this->laravel->singleton(AccessLog::class, function (Container $container) {
409+
return new AccessLog($container->make(AccessOutput::class));
410+
});
349411
}
350412
}

src/Concerns/InteractsWithWebsocket.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use Illuminate\Pipeline\Pipeline;
77
use Illuminate\Support\Arr;
88
use SwooleTW\Http\Exceptions\WebsocketNotSetInConfigException;
9-
use SwooleTW\Http\Helpers\Service;
9+
use SwooleTW\Http\Helpers\Alias;
10+
use SwooleTW\Http\Server\Facades\Server;
11+
use SwooleTW\Http\Server\Sandbox;
1012
use SwooleTW\Http\Transformers\Request;
1113
use SwooleTW\Http\Websocket\HandlerContract;
1214
use SwooleTW\Http\Websocket\Parser;
@@ -54,8 +56,8 @@ trait InteractsWithWebsocket
5456
public function onOpen($swooleRequest)
5557
{
5658
$illuminateRequest = Request::make($swooleRequest)->toIlluminate();
57-
$websocket = $this->app->make(Service::WEBSOCKET_ALIAS);
58-
$sandbox = $this->app->make(Service::SANDBOX_ALIAS);
59+
$websocket = $this->app->make(Websocket::class);
60+
$sandbox = $this->app->make(Sandbox::class);
5961

6062
try {
6163
$websocket->reset(true)->setSender($swooleRequest->fd);
@@ -94,8 +96,8 @@ public function onMessage($server, $frame)
9496
return;
9597
}
9698

97-
$websocket = $this->app->make(Service::WEBSOCKET_ALIAS);
98-
$sandbox = $this->app->make(Service::SANDBOX_ALIAS);
99+
$websocket = $this->app->make(Websocket::class);
100+
$sandbox = $this->app->make(Sandbox::class);
99101

100102
try {
101103
// decode raw message via parser
@@ -132,7 +134,7 @@ public function onClose($server, $fd, $reactorId)
132134
return;
133135
}
134136

135-
$websocket = $this->app->make(Service::WEBSOCKET_ALIAS);
137+
$websocket = $this->app->make(Websocket::class);
136138

137139
try {
138140
$websocket->reset(true)->setSender($fd);
@@ -172,7 +174,7 @@ public function pushMessage($server, array $data)
172174

173175
// push message to designated fds
174176
foreach ($push->getDescriptors() as $descriptor) {
175-
if ($server->exist($descriptor) || ! $push->isBroadcastToDescriptor((int)$descriptor)) {
177+
if ($server->exist($descriptor) || ! $push->isBroadcastToDescriptor((int) $descriptor)) {
176178
$server->push($descriptor, $payload, $push->getOpcode());
177179
}
178180
}
@@ -205,7 +207,7 @@ public function getPayloadParser()
205207
*/
206208
protected function prepareWebsocket()
207209
{
208-
$config = $this->container->make(Service::CONFIG_ALIAS);
210+
$config = $this->container->make(Alias::CONFIG);
209211
$isWebsocket = $config->get('swoole_http.websocket.enabled');
210212
$parser = $config->get('swoole_websocket.parser');
211213

@@ -225,7 +227,7 @@ protected function prepareWebsocket()
225227
*/
226228
protected function isServerWebsocket(int $fd): bool
227229
{
228-
$info = $this->container->make(Service::SERVER_ALIAS)->connection_info($fd);
230+
$info = $this->container->make(Server::class)->connection_info($fd);
229231

230232
return Arr::has($info, 'websocket_status') && Arr::get($info, 'websocket_status');
231233
}
@@ -253,7 +255,7 @@ protected function filterWebsocket(array $descriptors): array
253255
*/
254256
protected function prepareWebsocketHandler()
255257
{
256-
$handlerClass = $this->container->make(Service::CONFIG_ALIAS)->get('swoole_websocket.handler');
258+
$handlerClass = $this->container->make(Alias::CONFIG)->get('swoole_websocket.handler');
257259

258260
if (! $handlerClass) {
259261
throw new WebsocketNotSetInConfigException;
@@ -303,15 +305,15 @@ protected function createRoom(string $class, array $settings): RoomContract
303305
protected function bindRoom(): void
304306
{
305307
$this->app->singleton(RoomContract::class, function (Container $container) {
306-
$config = $container->make(Service::CONFIG_ALIAS);
308+
$config = $container->make(Alias::CONFIG);
307309
$driver = $config->get('swoole_websocket.default');
308310
$settings = $config->get("swoole_websocket.settings.{$driver}");
309311
$className = $config->get("swoole_websocket.drivers.{$driver}");
310312

311313
return $this->createRoom($className, $settings);
312314
});
313315

314-
$this->app->alias(RoomContract::class, 'swoole.room');
316+
$this->app->alias(RoomContract::class, Alias::ROOM);
315317
}
316318

317319
/**
@@ -320,21 +322,21 @@ protected function bindRoom(): void
320322
protected function bindWebsocket()
321323
{
322324
$this->app->singleton(Websocket::class, function (Container $app) {
323-
return new Websocket($app->make(Service::ROOM_ALIAS), new Pipeline($app));
325+
return new Websocket($app->make(RoomContract::class), new Pipeline($app));
324326
});
325327

326-
$this->app->alias(Websocket::class, Service::WEBSOCKET_ALIAS);
328+
$this->app->alias(Websocket::class, 'swoole.websocket');
327329
}
328330

329331
/**
330332
* Load websocket routes file.
331333
*/
332334
protected function loadWebsocketRoutes()
333335
{
334-
$routePath = $this->container->make(Service::CONFIG_ALIAS)->get('swoole_websocket.route_file');
336+
$routePath = $this->container->make(Alias::CONFIG)->get('swoole_websocket.route_file');
335337

336338
if (! file_exists($routePath)) {
337-
$routePath = __DIR__.'/../../routes/websocket.php';
339+
$routePath = __DIR__ . '/../../routes/websocket.php';
338340
}
339341

340342
return require $routePath;

src/Concerns/ResetApplication.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SwooleTW\Http\Concerns;
44

5+
use Illuminate\Contracts\Config\Repository;
56
use Illuminate\Contracts\Container\Container;
67
use SwooleTW\Http\Exceptions\SandboxException;
78
use SwooleTW\Http\Server\Resetters\ResetterContract;
@@ -28,7 +29,7 @@ trait ResetApplication
2829
*/
2930
protected function setInitialConfig()
3031
{
31-
$this->config = clone $this->getBaseApp()->make('config');
32+
$this->config = clone $this->getBaseApp()->make(Repository::class);
3233
}
3334

3435
/**
@@ -74,7 +75,7 @@ protected function setInitialResetters()
7475
foreach ($resetters as $resetter) {
7576
$resetterClass = $app->make($resetter);
7677
if (! $resetterClass instanceof ResetterContract) {
77-
throw new SandboxException("{$resetter} must implement ".ResetterContract::class);
78+
throw new SandboxException("{$resetter} must implement " . ResetterContract::class);
7879
}
7980
$this->resetters[$resetter] = $resetterClass;
8081
}

src/Concerns/WithApplication.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Illuminate\Contracts\Http\Kernel;
77
use Illuminate\Support\Facades\Facade;
88
use SwooleTW\Http\Exceptions\FrameworkNotSupportException;
9-
use SwooleTW\Http\Helpers\Service;
9+
use SwooleTW\Http\Helpers\Alias;
1010

1111
/**
1212
* Trait WithApplication
@@ -149,7 +149,7 @@ public function getBasePath()
149149
*/
150150
protected function preResolveInstances()
151151
{
152-
$resolves = $this->container->make(Service::CONFIG_ALIAS)->get('swoole_http.pre_resolved', []);
152+
$resolves = $this->container->make(Alias::CONFIG)->get('swoole_http.pre_resolved', []);
153153

154154
foreach ($resolves as $abstract) {
155155
if ($this->getApplication()->offsetExists($abstract)) {

src/Controllers/SocketIOController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function upgrade(Request $request)
3434
]
3535
);
3636

37-
return '97:0'.$payload.'2:40';
37+
return '97:0' . $payload . '2:40';
3838
}
3939

4040
public function reject()

0 commit comments

Comments
 (0)