From d603d748f253ae6e49c7cc4323d7322b56eec7da Mon Sep 17 00:00:00 2001 From: Marcel Hernandez Date: Tue, 16 Apr 2024 16:27:34 +0200 Subject: [PATCH] bubble error messages up --- frontend/views/index.html.twig | 12 +++++++----- frontend/web/index.php | 31 ++++++++++++++++++++++--------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/frontend/views/index.html.twig b/frontend/views/index.html.twig index 3eb526c..1e21b50 100644 --- a/frontend/views/index.html.twig +++ b/frontend/views/index.html.twig @@ -8,21 +8,23 @@ +{% set service_down = chain is not defined %} +
-

Bitcoin GroupHug Testnet

+

Bitcoin GroupHug {% if service_down %}Down{% elseif chain == 'MAINNET' %}Mainnet{% else %}Testnet{% endif %}

- {% if role is not null and message is not null %} - + {% if alert is defined %} + {% endif %}
- +
- +
diff --git a/frontend/web/index.php b/frontend/web/index.php index a1b0140..a9b76ff 100644 --- a/frontend/web/index.php +++ b/frontend/web/index.php @@ -29,13 +29,15 @@ $app->add(function (ServerRequestInterface $request, RequestHandlerInterface $handler) use ($twig, $settings): ResponseInterface { if (false === $fh = stream_socket_client($settings['grouphug_server'])) { - return $twig->render(new Response(), 'index.html.twig', ['role' => 'warning', 'message' => 'Service down, try again later.']); + return $twig->render(new Response(), 'index.html.twig', ['alert' => ['class' => 'alert-warning', 'message' => 'Service down, try again later.']]); } + $twig->getEnvironment()->addGlobal('chain', $chain = stream_get_line($fh, 16, "\n")); + $response = $handler->handle( $request ->withAttribute('grouphug_conn', $fh) - ->withAttribute('grouphug_chain', stream_get_line($fh, 16, "\n")) + ->withAttribute('grouphug_chain', $chain) ); stream_socket_shutdown($fh, \STREAM_SHUT_RDWR); @@ -45,21 +47,32 @@ }); $app->get('/', function (ServerRequestInterface $request, ResponseInterface $response) use ($twig) { - return $twig->render($response, 'index.html.twig', ['role' => null, 'message' => null]); + return $twig->render($response, 'index.html.twig', ['chain' => $request->getAttribute('grouphug_chain')]); }); $app->post('/', function (ServerRequestInterface $request, ResponseInterface $response) use ($twig) { - $form = $request->getParsedBody(); + return $twig->render($response, 'index.html.twig', ['alert' => processTx($request->getParsedBody(), $request->getAttribute('grouphug_conn'))]); +}); +function processTx(mixed $form, $conn): array +{ if (!is_array($form) || empty($form['tx']) || strlen($form['tx']) > 1024 || !preg_match('/^([0-9a-fA-F]{2})+$/', $form['tx'])) { - return $twig->render($response, 'index.html.twig', ['role' => 'danger', 'message' => 'Transaction rejected!']); + return ['class' => 'alert-danger', 'message' => 'Invalid transaction received.']; } - if (false === fwrite($request->getAttribute('grouphug_conn'), "add_tx {$form['tx']}")) { - return $twig->render($response, 'index.html.twig', ['role' => 'warning', 'message' => 'Service down, try again later.']); + $tx = $form['tx']; + + if (false === stream_socket_sendto($conn, "add_tx $tx")) { + return ['class' => 'alert-warning', 'message' => 'Service down, try again later.']; } - return $twig->render($response, 'index.html.twig', ['role' => 'success', 'message' => 'Transaction accepted!']); -}); + $reply = stream_get_line($conn, 128, "\n"); + + if ($reply != 'Ok') { + return ['class' => 'alert-warning', 'message' => 'Transaction rejected. ' . $reply]; + } + + return ['class' => 'alert-success', 'message' => 'Transaction accepted!']; +} $app->run();