Last active
December 22, 2018 06:37
-
-
Save koriym/88afdfdc71dd5ce50fe27d9ef951ba96 to your computer and use it in GitHub Desktop.
BEAR.Sunday + Swoole ref: https://qiita.com/koriym/items/7d4fd7b1efe41b06aff3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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