Skip to content

Commit 9442b35

Browse files
committed
Streamline APIs.
1 parent ecc0c56 commit 9442b35

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

timeboost/src/api.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ use http::{Request, Response, StatusCode};
1212
use timeboost_crypto::prelude::{ThresholdEncKey, ThresholdEncKeyCell};
1313
use timeboost_types::{Bundle, BundleVariant, SignedPriorityBundle};
1414
use timeboost_utils::types::prometheus::PrometheusMetrics;
15-
use tokio::{net::TcpListener, sync::mpsc::Sender};
15+
use tokio::{
16+
net::{TcpListener, ToSocketAddrs},
17+
sync::mpsc::Sender,
18+
};
1619
use tower::ServiceBuilder;
1720
use tower_http::trace::TraceLayer;
1821
use tower_http::{ServiceBuilderExt, request_id::MakeRequestUuid};
@@ -62,8 +65,9 @@ impl ApiServer {
6265
)
6366
}
6467

65-
pub fn serve(self, listener: TcpListener) -> impl Future<Output = io::Result<()>> {
66-
axum::serve(listener, self.router()).into_future()
68+
pub async fn serve<A: ToSocketAddrs>(self, addr: A) -> io::Result<()> {
69+
let listener = TcpListener::bind(addr).await?;
70+
axum::serve(listener, self.router()).await
6771
}
6872

6973
async fn health(this: State<Self>) -> StatusCode {

timeboost/src/api/internal.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use std::{io, net::SocketAddr};
1+
use std::io;
22

33
use proto::internal::internal_api_server::InternalApi;
44
use timeboost_builder::{CertifierDown, Handle};
55
use timeboost_types::Block;
6+
use tokio::net::{ToSocketAddrs, lookup_host};
67
use tonic::{Request, Response, Status};
78

89
use timeboost_proto::{self as proto, internal::internal_api_server::InternalApiServer};
@@ -18,7 +19,11 @@ impl GrpcServer {
1819
}
1920
}
2021

21-
pub async fn serve(self, addr: SocketAddr) -> io::Result<()> {
22+
pub async fn serve<A: ToSocketAddrs>(self, addr: A) -> io::Result<()> {
23+
let addr = lookup_host(addr)
24+
.await?
25+
.next()
26+
.ok_or_else(|| io::Error::other("can not resolve grpc server address"))?;
2227
tonic::transport::Server::builder()
2328
.add_service(InternalApiServer::new(self.service))
2429
.serve(addr)

timeboost/src/binaries/timeboost.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use timeboost::{Timeboost, TimeboostConfig};
77
use timeboost_builder::robusta;
88
use timeboost_crypto::prelude::ThresholdEncKeyCell;
99
use timeboost_types::DkgKeyStore;
10-
use tokio::net::{TcpListener, lookup_host};
1110
use tokio::select;
1211
use tokio::signal;
1312
use tokio::task::spawn;
@@ -231,18 +230,11 @@ async fn main() -> Result<()> {
231230
let timeboost = Timeboost::new(config).await?;
232231

233232
let mut grpc = {
234-
let addr = my_keyset.internal_address.clone();
235-
let addr = lookup_host(addr.to_string())
236-
.await?
237-
.next()
238-
.ok_or_else(|| anyhow!("{} does not resolve to a socket address", addr))?;
233+
let addr = my_keyset.internal_address.to_string();
239234
spawn(timeboost.internal_grpc_api().serve(addr))
240235
};
241236

242-
let mut api = {
243-
let listener = TcpListener::bind(format!("0.0.0.0:{}", cli.http_port)).await?;
244-
spawn(timeboost.api().serve(listener))
245-
};
237+
let mut api = spawn(timeboost.api().serve(format!("0.0.0.0:{}", cli.http_port)));
246238

247239
for peer in sailfish_peer_hosts_and_keys {
248240
let p = peer.2.port();

0 commit comments

Comments
 (0)