Unified logging
This commit is contained in:
parent
56ddab0cd5
commit
c31c55fafd
@ -91,7 +91,7 @@ class MercureHandler
|
|||||||
code: RequestException::ERR_INVALID_REQUEST_DATA
|
code: RequestException::ERR_INVALID_REQUEST_DATA
|
||||||
);
|
);
|
||||||
[ $name, $value ] = array_map('urldecode', explode("=", $param, 2));
|
[ $name, $value ] = array_map('urldecode', explode("=", $param, 2));
|
||||||
if ($name === 'topic') $topics[] = $value;
|
if ($name === 'topic' || $name === 'topic[]') $topics[] = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Grab the JWT token from the requests authorization attribute
|
// Grab the JWT token from the requests authorization attribute
|
||||||
@ -142,7 +142,9 @@ class MercureHandler
|
|||||||
|
|
||||||
// Parse out the urlencoded body. Pretty sure there is a better way to do this?
|
// Parse out the urlencoded body. Pretty sure there is a better way to do this?
|
||||||
$body = explode("&", (string)$request->getBody());
|
$body = explode("&", (string)$request->getBody());
|
||||||
$data = [];
|
$data = [
|
||||||
|
'topic' => []
|
||||||
|
];
|
||||||
foreach ($body as $param) {
|
foreach ($body as $param) {
|
||||||
if (!str_contains($param, "="))
|
if (!str_contains($param, "="))
|
||||||
throw new RequestException(
|
throw new RequestException(
|
||||||
@ -150,10 +152,8 @@ class MercureHandler
|
|||||||
code: RequestException::ERR_INVALID_REQUEST_DATA
|
code: RequestException::ERR_INVALID_REQUEST_DATA
|
||||||
);
|
);
|
||||||
[ $name, $value ] = array_map('urldecode', explode("=", $param, 2));
|
[ $name, $value ] = array_map('urldecode', explode("=", $param, 2));
|
||||||
if (in_array($name, [ 'topic' ])) {
|
if ($name === 'topic' || $name === 'topic[]') {
|
||||||
if (!isset($data[$name]))
|
$data['topic'][] = $value;
|
||||||
$data[$name] = [];
|
|
||||||
$data[$name][] = $value;
|
|
||||||
} else {
|
} else {
|
||||||
$data[$name] = $value;
|
$data[$name] = $value;
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ use NoccyLabs\Mercureact\Configuration;
|
|||||||
use NoccyLabs\Mercureact\Http\Exception\SecurityException;
|
use NoccyLabs\Mercureact\Http\Exception\SecurityException;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
use React\Http\Message\Response;
|
use React\Http\Message\Response;
|
||||||
use React\Promise\Promise;
|
use React\Promise\Promise;
|
||||||
use React\Promise\PromiseInterface;
|
use React\Promise\PromiseInterface;
|
||||||
@ -15,7 +16,8 @@ class ResponseMiddleware
|
|||||||
{
|
{
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private Configuration $config
|
private Configuration $config,
|
||||||
|
private LoggerInterface $logger,
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -60,13 +62,18 @@ class ResponseMiddleware
|
|||||||
assert("\$response instanceof ResponseInterface");
|
assert("\$response instanceof ResponseInterface");
|
||||||
$host = ($request->getServerParams()['SERVER_ADDR']??"");
|
$host = ($request->getServerParams()['SERVER_ADDR']??"");
|
||||||
//. ":" . ($request->getServerParams()['SERVER_PORT']??"80");
|
//. ":" . ($request->getServerParams()['SERVER_PORT']??"80");
|
||||||
fprintf(STDOUT, "%s %3d %s %s %d\n",
|
$this->logger->debug(sprintf("%s %s %s → %3d (%d)",
|
||||||
$request->getServerParams()['REMOTE_ADDR'],
|
$request->getServerParams()['REMOTE_ADDR'],
|
||||||
$response->getStatusCode(),
|
|
||||||
$request->getMethod(),
|
$request->getMethod(),
|
||||||
$request->getUri()->getPath(),
|
$request->getUri()->getPath(),
|
||||||
|
$response->getStatusCode(),
|
||||||
strlen($response->getBody())
|
strlen($response->getBody())
|
||||||
);
|
), [
|
||||||
|
'remote' => $request->getServerParams()['REMOTE_ADDR'],
|
||||||
|
'status' => $response->getStatusCode(),
|
||||||
|
'method' => $request->getMethod(),
|
||||||
|
'path' => $request->getUri()->getPath(),
|
||||||
|
]);
|
||||||
return $response
|
return $response
|
||||||
// ->withAddedHeader('Link', '<https://'.$host.'/.well-known/mercure>; rel="mercure"')
|
// ->withAddedHeader('Link', '<https://'.$host.'/.well-known/mercure>; rel="mercure"')
|
||||||
// ->withAddedHeader('Link', '<wss://'.$host.'/.well-known/mercure>; rel="mercure+ws"')
|
// ->withAddedHeader('Link', '<wss://'.$host.'/.well-known/mercure>; rel="mercure+ws"')
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
namespace NoccyLabs\Mercureact\Http;
|
namespace NoccyLabs\Mercureact\Http;
|
||||||
|
|
||||||
|
use Monolog\Handler\StreamHandler;
|
||||||
|
use Monolog\Logger;
|
||||||
use NoccyLabs\Mercureact\Broker\TopicManager;
|
use NoccyLabs\Mercureact\Broker\TopicManager;
|
||||||
use NoccyLabs\Mercureact\Configuration;
|
use NoccyLabs\Mercureact\Configuration;
|
||||||
use NoccyLabs\Mercureact\Http\Middleware\ApiHandler;
|
use NoccyLabs\Mercureact\Http\Middleware\ApiHandler;
|
||||||
@ -10,6 +12,7 @@ use NoccyLabs\Mercureact\Http\Middleware\NotFoundHandler;
|
|||||||
use NoccyLabs\Mercureact\Http\Middleware\ResponseMiddleware;
|
use NoccyLabs\Mercureact\Http\Middleware\ResponseMiddleware;
|
||||||
use NoccyLabs\Mercureact\Http\Middleware\SecurityMiddleware;
|
use NoccyLabs\Mercureact\Http\Middleware\SecurityMiddleware;
|
||||||
use NoccyLabs\Mercureact\Http\Middleware\WebSocketHandler;
|
use NoccyLabs\Mercureact\Http\Middleware\WebSocketHandler;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
use React\EventLoop\Loop;
|
use React\EventLoop\Loop;
|
||||||
use React\EventLoop\LoopInterface;
|
use React\EventLoop\LoopInterface;
|
||||||
use React\Http\HttpServer;
|
use React\Http\HttpServer;
|
||||||
@ -28,6 +31,8 @@ class Server
|
|||||||
|
|
||||||
private TopicManager $topicManager;
|
private TopicManager $topicManager;
|
||||||
|
|
||||||
|
private Logger|LoggerInterface $logger;
|
||||||
|
|
||||||
private ResponseMiddleware $responseMiddleware;
|
private ResponseMiddleware $responseMiddleware;
|
||||||
private SecurityMiddleware $securityMiddleware;
|
private SecurityMiddleware $securityMiddleware;
|
||||||
private WebSocketHandler $webSocketHandler;
|
private WebSocketHandler $webSocketHandler;
|
||||||
@ -46,6 +51,8 @@ class Server
|
|||||||
|
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
|
|
||||||
|
$this->logger = $this->createLogger();
|
||||||
|
|
||||||
$this->topicManager = new TopicManager();
|
$this->topicManager = new TopicManager();
|
||||||
|
|
||||||
$this->webSocketClients = new SplObjectStorage();
|
$this->webSocketClients = new SplObjectStorage();
|
||||||
@ -63,6 +70,15 @@ class Server
|
|||||||
$this->server->listen($socket);
|
$this->server->listen($socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function createLogger(): LoggerInterface
|
||||||
|
{
|
||||||
|
$handlers = [
|
||||||
|
new StreamHandler(STDOUT)
|
||||||
|
];
|
||||||
|
$logger = new Logger("main", $handlers);
|
||||||
|
return $logger;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @return HttpServer
|
* @return HttpServer
|
||||||
@ -71,7 +87,8 @@ class Server
|
|||||||
{
|
{
|
||||||
return new HttpServer(
|
return new HttpServer(
|
||||||
$this->responseMiddleware = new ResponseMiddleware(
|
$this->responseMiddleware = new ResponseMiddleware(
|
||||||
config: $this->config
|
config: $this->config,
|
||||||
|
logger: $this->logger->withName("http"),
|
||||||
),
|
),
|
||||||
$this->securityMiddleware = new SecurityMiddleware(
|
$this->securityMiddleware = new SecurityMiddleware(
|
||||||
config: $this->config
|
config: $this->config
|
||||||
|
Loading…
Reference in New Issue
Block a user