Skip to content

Commit ff7eb0c

Browse files
ghubertpaloDamien LACHAUME / PALO-IT
authored andcommitted
add default JSON headers
1 parent c12629b commit ff7eb0c

File tree

3 files changed

+85
-20
lines changed

3 files changed

+85
-20
lines changed
Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use axum::{
2+
body::Body,
23
extract::{Path, State},
3-
response::{IntoResponse, Response},
4-
Json,
4+
http::Response,
5+
response::IntoResponse,
56
};
67

7-
use crate::shared_state::{AppState, SharedState};
8-
use crate::{AppError, StdResult};
8+
use crate::shared_state::SharedState;
9+
use crate::AppError;
910

1011
pub async fn epoch_settings(State(state): State<SharedState>) -> Result<String, AppError> {
1112
let app_state = state.read().await;
@@ -17,7 +18,7 @@ pub async fn epoch_settings(State(state): State<SharedState>) -> Result<String,
1718
pub async fn snapshot(
1819
Path(key): Path<String>,
1920
State(state): State<SharedState>,
20-
) -> Result<Response, AppError> {
21+
) -> Result<Response<Body>, AppError> {
2122
let app_state = state.read().await;
2223

2324
app_state
@@ -34,24 +35,42 @@ pub async fn snapshots(State(state): State<SharedState>) -> Result<String, AppEr
3435
Ok(snapshots)
3536
}
3637

37-
pub async fn msds(State(state): State<SharedState>) -> Result<Json<String>, AppError> {
38-
todo!()
38+
pub async fn msds(State(state): State<SharedState>) -> Result<String, AppError> {
39+
let app_state = state.read().await;
40+
let msds = app_state.get_msds().await?;
41+
42+
Ok(msds)
3943
}
4044

4145
pub async fn msd(
4246
Path(key): Path<String>,
4347
State(state): State<SharedState>,
44-
) -> Result<Json<String>, AppError> {
45-
todo!()
48+
) -> Result<Response<Body>, AppError> {
49+
let app_state = state.read().await;
50+
51+
app_state
52+
.get_msd(&key)
53+
.await?
54+
.map(|s| s.into_response())
55+
.ok_or_else(|| AppError::NotFound(format!("mithril stake distribution epoch={key}")))
4656
}
4757

48-
pub async fn certificates(State(state): State<SharedState>) -> Result<Json<String>, AppError> {
49-
todo!()
58+
pub async fn certificates(State(state): State<SharedState>) -> Result<String, AppError> {
59+
let app_state = state.read().await;
60+
let certificates = app_state.get_certificates().await?;
61+
62+
Ok(certificates)
5063
}
5164

5265
pub async fn certificate(
5366
Path(key): Path<String>,
5467
State(state): State<SharedState>,
55-
) -> Result<Json<String>, AppError> {
56-
todo!()
68+
) -> Result<Response<Body>, AppError> {
69+
let app_state = state.read().await;
70+
71+
app_state
72+
.get_certificate(&key)
73+
.await?
74+
.map(|s| s.into_response())
75+
.ok_or_else(|| AppError::NotFound(format!("certificate hash={key}")))
5776
}

mithril-fake-aggregator/src/main.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
mod handlers;
22
mod shared_state;
33

4-
use std::{
5-
future::IntoFuture,
6-
path::{Path as StdPath, PathBuf},
7-
};
4+
use std::{future::IntoFuture, path::PathBuf};
85

96
use anyhow::{anyhow, Context};
107
use axum::{
118
body::Body,
12-
extract::{Path, State},
13-
http::{Response, StatusCode},
9+
extract::Request,
10+
http::{HeaderValue, Response, StatusCode},
11+
middleware::{self, Next},
1412
response::IntoResponse,
1513
routing::get,
16-
Json, Router,
14+
Router,
1715
};
1816
use clap::Parser;
1917
use futures::stream::StreamExt;
@@ -154,7 +152,10 @@ async fn main() -> StdResult<()> {
154152
"/aggregator/artifact/snapshot/:digest",
155153
get(handlers::snapshot),
156154
)
155+
.route("/aggregator/certificates", get(handlers::certificates))
156+
.route("/aggregator/certificate/:hash", get(handlers::certificate))
157157
.with_state(shared_state.clone())
158+
.layer(middleware::from_fn(set_json_app_header))
158159
.layer(
159160
TraceLayer::new_for_http()
160161
.make_span_with(
@@ -194,3 +195,20 @@ async fn main() -> StdResult<()> {
194195

195196
result
196197
}
198+
199+
async fn set_json_app_header(
200+
req: Request,
201+
next: Next,
202+
) -> Result<impl IntoResponse, (StatusCode, String)> {
203+
let mut res = next.run(req).await;
204+
205+
if res.status() == StatusCode::OK {
206+
let headers = res.headers_mut();
207+
headers.insert(
208+
"Content-Type",
209+
HeaderValue::from_static("application/json; charset=utf-8"),
210+
);
211+
}
212+
213+
Ok(res)
214+
}

mithril-fake-aggregator/src/shared_state.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,37 @@ impl AppState {
120120
.map_err(|e| e.into())
121121
}
122122

123+
pub async fn get_msd(&self, key: &str) -> StdResult<Option<String>> {
124+
self.msds
125+
.get(key)
126+
.map(|v| serde_json::to_string(v))
127+
.transpose()
128+
.map_err(|e| e.into())
129+
}
130+
123131
pub async fn get_epoch_settings(&self) -> StdResult<String> {
124132
Ok(self.epoch_settings.to_owned())
125133
}
134+
135+
pub async fn get_certificates(&self) -> StdResult<String> {
136+
let values: Vec<Value> = self
137+
.certificates
138+
.iter()
139+
.map(|(_k, v)| v.to_owned())
140+
.collect();
141+
142+
serde_json::to_string(&Value::Array(values))
143+
.map_err(|e| anyhow!(e))
144+
.with_context(|| "could not JSON serialize the certificates list.")
145+
}
146+
147+
pub async fn get_certificate(&self, key: &str) -> StdResult<Option<String>> {
148+
self.certificates
149+
.get(key)
150+
.map(|v| serde_json::to_string(v))
151+
.transpose()
152+
.map_err(|e| e.into())
153+
}
126154
}
127155
/*
128156
/// Return the content of a file in a directory if it exists.

0 commit comments

Comments
 (0)