Skip to content

Instantly share code, notes, and snippets.

@koriym
Last active December 22, 2018 06:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koriym/88afdfdc71dd5ce50fe27d9ef951ba96 to your computer and use it in GitHub Desktop.
Save koriym/88afdfdc71dd5ce50fe27d9ef951ba96 to your computer and use it in GitHub Desktop.
return function (
string $context,
string $name,
string $ip,
int $port,
int $mode = SWOOLE_BASE,
int $sockType = SWOOLE_SOCK_TCP,
array $settings = ['worker_num' => 4]
) : int {
$http = new Server($ip, $port, $mode, $sockType);
$http->set($settings);
$http->on('start', function () use ($ip, $port) {
echo "Swoole http server is started at http://{$ip}:{$port}" . PHP_EOL;
});
$injector = new AppInjector($name, $context);
/* @var App $app */
$app = $injector->getOverrideInstance(new Psr7SwooleModule, App::class);
$superGlobals = new SuperGlobals;
$http->on('request', function (Request $request, Response $response) use ($app, $superGlobals) {
if ($app->httpCache->isNotModified($request->header)) {
$app->httpCache->transfer($response);
return;
}
$superGlobals($request);
$match = $app->router->match($GLOBALS, $_SERVER);
try {
/* @var ResourceObject $ro */
$ro = $app->resource->{$match->method}->uri($match->path)($match->query);
$app->responder->setResponse($response);
$ro->transfer($app->responder, []);
} catch (\Exception $e) {
$app->error->transfer($e, $request, $response);
}
});
$http->start();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment