public function boot()
{
if (!$this->port) {
// The server doesn't run.
return;
}
$this->socket = new ReactSocketServer($this->eventLoop);
$http = new ReactHttpServer($this->socket);
$this->socket->listen($this->port, $this->host);
$routes = $this->router->getRoutes();
$http->on('request', function ($request, $response) use($routes) {
// check api key.
if (count($this->apiKeys) > 0) {
$query = $request->getQuery();
if (!isset($query['apiKey']) || !isset($this->apiKeys[$query['apiKey']])) {
$response->writeHead(401, array('Content-Type' => 'text/plain'));
$response->end("Unauthorized\n");
$this->outputAccessLog($request, 401);
return;
}
}
$context = new RequestContext($request->getPath(), $request->getMethod());
$matcher = new UrlMatcher($routes, $context);
try {
$parameters = $matcher->match($request->getPath());
$action = $parameters['_action'];
$controller = new HttpController($this->worker, $this, $request, $response);
call_user_func(array($controller, $action), $parameters);
} catch (ResourceNotFoundException $e) {
$response->writeHead(404, array('Content-Type' => 'text/plain'));
$response->end("Not found\n");
$this->outputAccessLog($request, 404);
}
});
$this->booted = true;
$this->output->writeln("<info>Initializing http server:</info> <comment>http://" . $this->host . ":" . $this->port . "/</comment>");
}