Add basic request logging, configurable listen addr
All checks were successful
phpunit / Integration Tests (push) Successful in 10m48s
All checks were successful
phpunit / Integration Tests (push) Successful in 10m48s
This commit is contained in:
@ -33,9 +33,10 @@ class Daemon
|
||||
|
||||
public function start(): self
|
||||
{
|
||||
$this->listener = new TcpServer("0.0.0.0:8000");
|
||||
$this->listener = new TcpServer(APP_LISTEN_ADDR);
|
||||
|
||||
$this->http = new HttpServer(
|
||||
$this->loggingMiddleware(...),
|
||||
$this->errorHandlingMiddleware(...),
|
||||
$this->onRequest(...)
|
||||
);
|
||||
@ -55,6 +56,29 @@ class Daemon
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function loggingMiddleware(ServerRequestInterface $request, callable $next): ResponseInterface
|
||||
{
|
||||
$ms1 = hrtime(true);
|
||||
try {
|
||||
$response = $next($request);
|
||||
}
|
||||
catch (\Throwable $t) {
|
||||
$response = Response::plaintext($t)->withStatus(500);
|
||||
}
|
||||
$ms2 = hrtime(true);
|
||||
$elapsed = ($ms2-$ms1)/1000000;
|
||||
|
||||
printf(
|
||||
"%s %s %3d %.2fms\n",
|
||||
$request->getMethod(),
|
||||
$request->getUri()->getPath(),
|
||||
$response->getStatusCode(),
|
||||
$elapsed
|
||||
);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
private function errorHandlingMiddleware(ServerRequestInterface $request, callable $next): ResponseInterface
|
||||
{
|
||||
try {
|
||||
|
@ -4,15 +4,25 @@ require_once __DIR__."/../vendor/autoload.php";
|
||||
|
||||
define("APP_ROOT", dirname(__DIR__));
|
||||
define("APP_DATA", APP_ROOT."/var");
|
||||
define("APP_LISTEN_ADDR", getenv("PARAMDB_LISTEN")?:"0.0.0.0:8000");
|
||||
|
||||
echo <<<EOT
|
||||
__________ ________ __________
|
||||
\\______ \\_____ ____________ _____ \\______ \\\\______ \\
|
||||
| ___/\\__ \\\\_ __ \\__ \\ / \\ | | \\| | _/
|
||||
| | / __ \\| | \\// __ \\| Y Y \\| ` \\ | \\
|
||||
|____| (____ /__| (____ /__|_| /_______ /______ /
|
||||
\\/ \\/ \\/ \\/ \\/
|
||||
|
||||
EOT;
|
||||
|
||||
echo "ParamDB v0.0.0 (c) 2024, NoccyLabs\n";
|
||||
echo "Licensed under GNU GPL v3 or later\n\n";
|
||||
|
||||
echo "Application root: ".APP_ROOT."\n";
|
||||
echo "Data directory: ".APP_DATA."\n";
|
||||
echo "Listen address: 0.0.0.0:8000\n";
|
||||
|
||||
|
||||
echo "Licensed under GNU GPL v3 or later\n";
|
||||
echo "\n";
|
||||
echo " Application root: ".APP_ROOT."\n";
|
||||
echo " Data directory: ".APP_DATA."\n";
|
||||
echo " Listen address: ".APP_LISTEN_ADDR."\n";
|
||||
echo "\n";
|
||||
|
||||
$daemon = new NoccyLabs\ParamDb\Daemon(APP_DATA."/data.db");
|
||||
$daemon->start();
|
||||
|
Reference in New Issue
Block a user