From 9ed1323e349e436d700ff1a617cf106b302a32d7 Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 17 May 2022 12:26:50 +0200 Subject: [PATCH] Introduce class `Icinga\Application\Daemon\HttpJob` --- library/Icinga/Application/Daemon/HttpJob.php | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 library/Icinga/Application/Daemon/HttpJob.php diff --git a/library/Icinga/Application/Daemon/HttpJob.php b/library/Icinga/Application/Daemon/HttpJob.php new file mode 100644 index 000000000..0a71e51c0 --- /dev/null +++ b/library/Icinga/Application/Daemon/HttpJob.php @@ -0,0 +1,70 @@ +socketPath = $config->get('socket', '/var/run/icingawebd.socket'); + } + + public function attach(LoopInterface $loop): void + { + Logger::info('Starting handling of incoming HTTP requests'); + + if (file_exists($this->socketPath)) { + // The SocketServer itself doesn't remove it, for whatever reason, and fails on restart if we don't either + unlink($this->socketPath); + } + + $this->socket = new SocketServer('unix://' . $this->socketPath, [], $loop); + $this->server = new HttpServer($loop, function ($req) { + return $this->handle($req); + }); + $this->server->listen($this->socket); + } + + public function cancel(): void + { + Logger::info('Stopping handling of incoming HTTP requests'); + + $this->socket->close(); + } + + /** + * Handle the given HTTP request + * + * @param ServerRequestInterface $request + * + * @return ResponseInterface + */ + protected function handle(ServerRequestInterface $request): ResponseInterface + { + return Response::plaintext('Hello World!' . PHP_EOL); + } +}