This commit is contained in:
as2252258@163.com
2021-08-04 01:47:26 +08:00
parent e3f81d83b1
commit 76e3308edd
4 changed files with 38 additions and 81 deletions
+15 -8
View File
@@ -6,6 +6,7 @@ use Annotation\Inject;
use Exception;
use HttpServer\Http\Context;
use HttpServer\Http\Request as HSRequest;
use HttpServer\Route\Node;
use HttpServer\Route\Router;
use Server\Events\OnAfterRequest;
use Snowflake\Event;
@@ -39,6 +40,7 @@ class HTTPServerListener extends Abstracts\Server
public \HttpServer\Http\Response $response;
/** @var EventDispatch */
#[Inject(EventDispatch::class)]
public EventDispatch $eventDispatch;
@@ -90,15 +92,20 @@ class HTTPServerListener extends Abstracts\Server
public function onRequest(Request $request, Response $response)
{
try {
Context::setContext(Response::class, $response);
HSRequest::create($request);
// $this->router->dispatch(HSRequest::create($request));
} catch (Error | Throwable $exception) {
// $this->response->send(jTraceEx($exception), 500);
$node = $this->router->find_path(HSRequest::create($request));
if (!($node instanceof Node)) {
$this->response->setFormat(\HttpServer\Http\Response::HTML);
$data = '<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>';
} else {
$data= $node->dispatch();
}
} catch (Error | Throwable $exception) {
$data = jTraceEx($exception);
} finally {
$response->status(200);
$response->end('ok');
if (Context::hasContext(Response::class)) {
return;
}
Context::getContext(Response::class)->send($data,200, $response);
$this->eventDispatch->dispatch(new OnAfterRequest());
}
}