Skip to content

Commit

Permalink
bubble error messages up
Browse files Browse the repository at this point in the history
  • Loading branch information
1ma committed Apr 16, 2024
1 parent e1c0d52 commit d603d74
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
12 changes: 7 additions & 5 deletions frontend/views/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@
</head>
<body>

{% set service_down = chain is not defined %}

<div class="container mt-5">
<div class="row">
<div class="col-md-6 offset-md-3">
<h2 class="mb-4">Bitcoin GroupHug <span class="badge bg-warning">Testnet</span></h2>
<h2 class="mb-4">Bitcoin GroupHug {% if service_down %}<span class="badge bg-dark">Down</span>{% elseif chain == 'MAINNET' %}<span class="badge bg-success">Mainnet</span>{% else %}<span class="badge bg-warning">Testnet</span>{% endif %}</h2>

{% if role is not null and message is not null %}
<div class="col-md-12 alert alert-{{ role }}" role="alert">{{ message }}</div>
{% if alert is defined %}
<div class="col-md-12 alert {{ alert.class }}" role="alert">{{ alert.message }}</div>
{% endif %}

<form method="post" action="/">
<div class="mb-3">
<label for="tx" class="form-label">Submit Transaction</label>
<textarea class="form-control" id="tx" name="tx" rows="5" placeholder="Transaction hex"></textarea>
<textarea class="form-control" id="tx" name="tx" rows="5" placeholder="Transaction hex" required></textarea>
</div>
<button type="submit" class="btn btn-primary">Full Send</button>
<button type="submit" {% if not service_down %}class="btn btn-primary"{% else %}class="btn btn-dark" disabled{% endif %}>Full Send</button>
</form>
</div>
</div>
Expand Down
31 changes: 22 additions & 9 deletions frontend/web/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();

0 comments on commit d603d74

Please sign in to comment.