Skip to content

Commit c12629b

Browse files
ghubertpaloDamien LACHAUME / PALO-IT
authored andcommitted
add handler module
1 parent 67d4cfb commit c12629b

File tree

3 files changed

+103
-59
lines changed

3 files changed

+103
-59
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use axum::{
2+
extract::{Path, State},
3+
response::{IntoResponse, Response},
4+
Json,
5+
};
6+
7+
use crate::shared_state::{AppState, SharedState};
8+
use crate::{AppError, StdResult};
9+
10+
pub async fn epoch_settings(State(state): State<SharedState>) -> Result<String, AppError> {
11+
let app_state = state.read().await;
12+
let epoch_settings = app_state.get_epoch_settings().await?;
13+
14+
Ok(epoch_settings.into())
15+
}
16+
17+
pub async fn snapshot(
18+
Path(key): Path<String>,
19+
State(state): State<SharedState>,
20+
) -> Result<Response, AppError> {
21+
let app_state = state.read().await;
22+
23+
app_state
24+
.get_snapshot(&key)
25+
.await?
26+
.map(|s| s.into_response())
27+
.ok_or_else(|| AppError::NotFound(format!("snapshot digest={key}")))
28+
}
29+
30+
pub async fn snapshots(State(state): State<SharedState>) -> Result<String, AppError> {
31+
let app_state = state.read().await;
32+
let snapshots = app_state.get_snapshots().await?;
33+
34+
Ok(snapshots)
35+
}
36+
37+
pub async fn msds(State(state): State<SharedState>) -> Result<Json<String>, AppError> {
38+
todo!()
39+
}
40+
41+
pub async fn msd(
42+
Path(key): Path<String>,
43+
State(state): State<SharedState>,
44+
) -> Result<Json<String>, AppError> {
45+
todo!()
46+
}
47+
48+
pub async fn certificates(State(state): State<SharedState>) -> Result<Json<String>, AppError> {
49+
todo!()
50+
}
51+
52+
pub async fn certificate(
53+
Path(key): Path<String>,
54+
State(state): State<SharedState>,
55+
) -> Result<Json<String>, AppError> {
56+
todo!()
57+
}

mithril-fake-aggregator/src/main.rs

Lines changed: 30 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod handlers;
12
mod shared_state;
23

34
use std::{
@@ -62,77 +63,43 @@ impl CliArguments {
6263
}
6364

6465
/// error that wraps `anyhow::Error`.
65-
pub struct AppError(anyhow::Error);
66+
pub enum AppError {
67+
/// Catching anyhow errors
68+
Internal(anyhow::Error),
69+
70+
/// Resource not found (specify what)
71+
NotFound(String),
72+
}
6673

6774
/// Tell axum how to convert `AppError` into a response.
6875
impl IntoResponse for AppError {
6976
fn into_response(self) -> Response<Body> {
70-
error!("{}", self.0);
77+
match self {
78+
Self::Internal(e) => {
79+
error!("{}", e);
7180

72-
(
73-
StatusCode::INTERNAL_SERVER_ERROR,
74-
format!("Error: {:?}", self.0),
75-
)
76-
.into_response()
81+
(StatusCode::INTERNAL_SERVER_ERROR, format!("Error: {:?}", e)).into_response()
82+
}
83+
Self::NotFound(resource) => (
84+
StatusCode::NOT_FOUND,
85+
format!("resource '{resource}' not found"),
86+
)
87+
.into_response(),
88+
}
7789
}
7890
}
7991

80-
// This enables using `?` on functions that return `Result<_, anyhow::Error>` to turn them into
81-
// `Result<_, AppError>`. That way you don't need to do that manually.
92+
/// This enables using `?` on functions that return `Result<_, anyhow::Error>` to turn them into
93+
/// `Result<_, AppError>`. That way you don't need to do that manually.
8294
impl<E> From<E> for AppError
8395
where
8496
E: Into<anyhow::Error>,
8597
{
8698
fn from(err: E) -> Self {
87-
Self(err.into())
99+
Self::Internal(err.into())
88100
}
89101
}
90102

91-
pub async fn epoch_settings_handler(State(state): State<SharedState>) -> Result<String, AppError> {
92-
let app_state = state.read().await;
93-
let epoch_settings = app_state.get_epoch_settings().await?;
94-
95-
Ok(epoch_settings.into())
96-
}
97-
98-
pub async fn snapshot_handler(
99-
Path(key): Path<String>,
100-
State(state): State<SharedState>,
101-
) -> StdResult<Json<String>> {
102-
todo!()
103-
}
104-
105-
pub async fn snapshots_handler(State(state): State<SharedState>) -> Result<String, AppError> {
106-
let app_state = state.read().await;
107-
let snapshots = app_state.get_snapshots().await?;
108-
109-
Ok(snapshots)
110-
}
111-
112-
pub async fn msds_handler(State(state): State<SharedState>) -> Result<Json<String>, AppError> {
113-
todo!()
114-
}
115-
116-
pub async fn msd_handler(
117-
Path(key): Path<String>,
118-
State(state): State<SharedState>,
119-
) -> Result<Json<String>, AppError> {
120-
todo!()
121-
}
122-
123-
pub async fn certificates_handler(
124-
State(state): State<SharedState>,
125-
) -> Result<Json<String>, AppError> {
126-
todo!()
127-
}
128-
129-
pub async fn certificate_handler(
130-
Path(key): Path<String>,
131-
State(state): State<SharedState>,
132-
) -> Result<Json<String>, AppError> {
133-
todo!()
134-
}
135-
136103
pub struct OsSignalHandler;
137104

138105
impl OsSignalHandler {
@@ -173,15 +140,19 @@ async fn main() -> StdResult<()> {
173140

174141
trace!("configuring router…");
175142
let app = Router::new()
176-
.route("/aggregator/epoch-settings", get(epoch_settings_handler))
177-
.route("/aggregator/artifact/snapshots", get(snapshots_handler))
143+
.route("/aggregator/epoch-settings", get(handlers::epoch_settings))
144+
.route("/aggregator/artifact/snapshots", get(handlers::snapshots))
178145
.route(
179146
"/aggregator/artifact/mithril-stake-distributions",
180-
get(msds_handler),
147+
get(handlers::msds),
181148
)
182149
.route(
183150
"/aggregator/artifact/mithril-stake-distribution/:digest",
184-
get(msd_handler),
151+
get(handlers::msd),
152+
)
153+
.route(
154+
"/aggregator/artifact/snapshot/:digest",
155+
get(handlers::snapshot),
185156
)
186157
.with_state(shared_state.clone())
187158
.layer(

mithril-fake-aggregator/src/shared_state.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,22 @@ impl AppState {
104104
.with_context(|| "could not JSON serialize the snapshots list.")
105105
}
106106

107+
pub async fn get_msds(&self) -> StdResult<String> {
108+
let values: Vec<Value> = self.msds.iter().map(|(_k, v)| v.to_owned()).collect();
109+
110+
serde_json::to_string(&Value::Array(values))
111+
.map_err(|e| anyhow!(e))
112+
.with_context(|| "could not JSON serialize the mithril stake distributions list.")
113+
}
114+
115+
pub async fn get_snapshot(&self, key: &str) -> StdResult<Option<String>> {
116+
self.snapshots
117+
.get(key)
118+
.map(|v| serde_json::to_string(v))
119+
.transpose()
120+
.map_err(|e| e.into())
121+
}
122+
107123
pub async fn get_epoch_settings(&self) -> StdResult<String> {
108124
Ok(self.epoch_settings.to_owned())
109125
}

0 commit comments

Comments
 (0)