Skip to content

Commit d20bf8b

Browse files
committed
Small refactoring.
1 parent 9442b35 commit d20bf8b

File tree

1 file changed

+36
-53
lines changed

1 file changed

+36
-53
lines changed

timeboost/src/api.rs

Lines changed: 36 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use axum::{
44
Json, Router,
55
body::Body,
66
extract::State,
7-
response::IntoResponse,
7+
response::Result,
88
routing::{get, post},
99
};
1010
use bon::Builder;
@@ -33,11 +33,11 @@ pub struct ApiServer {
3333
impl ApiServer {
3434
pub fn router(&self) -> Router {
3535
Router::new()
36-
.route("/v1/submit/priority", post(Self::submit_priority))
37-
.route("/v1/submit/regular", post(Self::submit_regular))
38-
.route("/v1/encryption-key", get(Self::encryption_key))
39-
.route("/i/health", get(Self::health))
40-
.route("/i/metrics", get(Self::metrics))
36+
.route("/v1/submit/priority", post(submit_priority))
37+
.route("/v1/submit/regular", post(submit_regular))
38+
.route("/v1/encryption-key", get(encryption_key))
39+
.route("/i/health", get(health))
40+
.route("/i/metrics", get(metrics))
4141
.with_state(self.clone())
4242
.layer(
4343
ServiceBuilder::new()
@@ -58,7 +58,7 @@ impl ApiServer {
5858
.on_request(|_r: &Request<Body>, _s: &Span| {
5959
debug!("request received")
6060
})
61-
.on_response(|r: &Response<Body>, d: Duration, _span: &Span| {
61+
.on_response(|r: &Response<Body>, d: Duration, _s: &Span| {
6262
debug!(status = %r.status().as_u16(), duration = ?d, "response created")
6363
})
6464
)
@@ -70,58 +70,41 @@ impl ApiServer {
7070
axum::serve(listener, self.router()).await
7171
}
7272

73-
async fn health(this: State<Self>) -> StatusCode {
74-
if this.bundles.is_closed() {
75-
StatusCode::INTERNAL_SERVER_ERROR
76-
} else {
77-
StatusCode::OK
73+
async fn submit_bundle(&self, bundle: BundleVariant) -> Result<()> {
74+
if self.bundles.send(bundle).await.is_err() {
75+
error!("bundle channel is closed");
76+
return Err(StatusCode::INTERNAL_SERVER_ERROR.into());
7877
}
78+
Ok(())
7979
}
80+
}
8081

81-
async fn submit_priority(
82-
this: State<Self>,
83-
bundle: Json<SignedPriorityBundle>,
84-
) -> (StatusCode, &'static str) {
85-
match this.bundles.send(BundleVariant::Priority(bundle.0)).await {
86-
Ok(()) => (StatusCode::OK, "priority bundle enqueued"),
87-
Err(_) => {
88-
error!("bundle channel is closed");
89-
(
90-
StatusCode::INTERNAL_SERVER_ERROR,
91-
"failed to enqueue priority bundle",
92-
)
93-
}
94-
}
95-
}
82+
async fn submit_priority(server: State<ApiServer>, json: Json<SignedPriorityBundle>) -> Result<()> {
83+
server.submit_bundle(BundleVariant::Priority(json.0)).await
84+
}
9685

97-
async fn submit_regular(this: State<Self>, bundle: Json<Bundle>) -> (StatusCode, &'static str) {
98-
match this.bundles.send(BundleVariant::Regular(bundle.0)).await {
99-
Ok(()) => (StatusCode::OK, "bundle enqueued"),
100-
Err(_) => {
101-
error!("bundle channel is closed");
102-
(
103-
StatusCode::INTERNAL_SERVER_ERROR,
104-
"failed to enqueue bundle",
105-
)
106-
}
107-
}
108-
}
86+
async fn submit_regular(server: State<ApiServer>, json: Json<Bundle>) -> Result<()> {
87+
server.submit_bundle(BundleVariant::Regular(json.0)).await
88+
}
10989

110-
async fn encryption_key(this: State<Self>) -> Json<ThresholdEncKey> {
111-
Json(this.enc_key.read().await)
112-
}
90+
async fn encryption_key(server: State<ApiServer>) -> Json<ThresholdEncKey> {
91+
Json(server.enc_key.read().await)
92+
}
11393

114-
async fn metrics(this: State<Self>) -> impl IntoResponse {
115-
match this.metrics.export() {
116-
Ok(output) => (StatusCode::OK, output).into_response(),
117-
Err(err) => {
118-
error!(%err, "metrics export error");
119-
(
120-
StatusCode::INTERNAL_SERVER_ERROR,
121-
"failed to export metrics",
122-
)
123-
.into_response()
124-
}
94+
async fn metrics(server: State<ApiServer>) -> Result<String> {
95+
match server.metrics.export() {
96+
Ok(output) => Ok(output),
97+
Err(err) => {
98+
error!(%err, "metrics export error");
99+
Err(StatusCode::INTERNAL_SERVER_ERROR.into())
125100
}
126101
}
127102
}
103+
104+
async fn health(server: State<ApiServer>) -> Result<()> {
105+
if server.bundles.is_closed() {
106+
error!("bundle channel is closed");
107+
return Err(StatusCode::INTERNAL_SERVER_ERROR.into());
108+
}
109+
Ok(())
110+
}

0 commit comments

Comments
 (0)