Skip to content

Commit cb233ec

Browse files
committed
refactor(aggregator): introduce RouterState and RouterConfig
Those structs will allow to have a validated configuration in the http server router while keeping only one dependency to pass arround.
1 parent 38ff571 commit cb233ec

File tree

14 files changed

+479
-302
lines changed

14 files changed

+479
-302
lines changed

mithril-aggregator/src/dependency_injection/builder.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,10 @@ use crate::{
6363
},
6464
entities::AggregatorEpochSettings,
6565
event_store::{EventMessage, EventStore, TransmitterService},
66-
http_server::routes::router,
66+
http_server::routes::{
67+
router,
68+
router::{RouterConfig, RouterState},
69+
},
6770
services::{
6871
AggregatorSignableSeedBuilder, AggregatorUpkeepService, BufferedCertifierService,
6972
CardanoTransactionsImporter, CertifierService, MessageService, MithrilCertifierService,
@@ -1491,8 +1494,9 @@ impl DependenciesBuilder {
14911494
&mut self,
14921495
) -> Result<impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone> {
14931496
let dependency_container = Arc::new(self.build_dependency_container().await?);
1497+
let router_state = RouterState::new(dependency_container.clone(), RouterConfig {});
14941498

1495-
Ok(router::routes(dependency_container))
1499+
Ok(router::routes(Arc::new(router_state)))
14961500
}
14971501

14981502
/// Create a [CardanoTransactionsPreloader] instance.

mithril-aggregator/src/http_server/routes/artifact_routes/cardano_stake_distribution.rs

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,47 @@
11
use crate::http_server::routes::middlewares;
2-
use crate::DependencyContainer;
2+
use crate::http_server::routes::router::RouterState;
33
use warp::Filter;
44

55
pub fn routes(
6-
dependency_manager: &DependencyContainer,
6+
router_state: &RouterState,
77
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
8-
artifact_cardano_stake_distributions(dependency_manager)
9-
.or(artifact_cardano_stake_distribution_by_id(
10-
dependency_manager,
11-
))
12-
.or(artifact_cardano_stake_distribution_by_epoch(
13-
dependency_manager,
14-
))
8+
artifact_cardano_stake_distributions(router_state)
9+
.or(artifact_cardano_stake_distribution_by_id(router_state))
10+
.or(artifact_cardano_stake_distribution_by_epoch(router_state))
1511
}
1612

1713
/// GET /artifact/cardano-stake-distributions
1814
fn artifact_cardano_stake_distributions(
19-
dependency_manager: &DependencyContainer,
15+
router_state: &RouterState,
2016
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
2117
warp::path!("artifact" / "cardano-stake-distributions")
2218
.and(warp::get())
23-
.and(middlewares::with_logger(dependency_manager))
24-
.and(middlewares::with_http_message_service(dependency_manager))
19+
.and(middlewares::with_logger(router_state))
20+
.and(middlewares::with_http_message_service(router_state))
2521
.and_then(handlers::list_artifacts)
2622
}
2723

2824
/// GET /artifact/cardano-stake-distribution/:id
2925
fn artifact_cardano_stake_distribution_by_id(
30-
dependency_manager: &DependencyContainer,
26+
router_state: &RouterState,
3127
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
3228
warp::path!("artifact" / "cardano-stake-distribution" / String)
3329
.and(warp::get())
34-
.and(middlewares::with_logger(dependency_manager))
35-
.and(middlewares::with_http_message_service(dependency_manager))
36-
.and(middlewares::with_metrics_service(dependency_manager))
30+
.and(middlewares::with_logger(router_state))
31+
.and(middlewares::with_http_message_service(router_state))
32+
.and(middlewares::with_metrics_service(router_state))
3733
.and_then(handlers::get_artifact_by_signed_entity_id)
3834
}
3935

4036
/// GET /artifact/cardano-stake-distribution/epoch/:epoch
4137
fn artifact_cardano_stake_distribution_by_epoch(
42-
dependency_manager: &DependencyContainer,
38+
router_state: &RouterState,
4339
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
4440
warp::path!("artifact" / "cardano-stake-distribution" / "epoch" / String)
4541
.and(warp::get())
46-
.and(middlewares::with_logger(dependency_manager))
47-
.and(middlewares::with_http_message_service(dependency_manager))
48-
.and(middlewares::with_metrics_service(dependency_manager))
42+
.and(middlewares::with_logger(router_state))
43+
.and(middlewares::with_http_message_service(router_state))
44+
.and(middlewares::with_metrics_service(router_state))
4945
.and_then(handlers::get_artifact_by_epoch)
5046
}
5147

@@ -170,7 +166,7 @@ pub mod tests {
170166
use super::*;
171167

172168
fn setup_router(
173-
dependency_manager: Arc<DependencyContainer>,
169+
state: RouterState,
174170
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
175171
let cors = warp::cors()
176172
.allow_any_origin()
@@ -179,7 +175,7 @@ pub mod tests {
179175

180176
warp::any()
181177
.and(warp::path(SERVER_BASE_PATH))
182-
.and(routes(&dependency_manager).with(cors))
178+
.and(routes(&state).with(cors))
183179
}
184180

185181
#[tokio::test]
@@ -199,7 +195,9 @@ pub mod tests {
199195
let response = request()
200196
.method(method)
201197
.path(&format!("/{SERVER_BASE_PATH}{path}"))
202-
.reply(&setup_router(Arc::new(dependency_manager)))
198+
.reply(&setup_router(RouterState::new_with_dummy_config(Arc::new(
199+
dependency_manager,
200+
))))
203201
.await;
204202

205203
APISpec::verify_conformity(
@@ -230,7 +228,9 @@ pub mod tests {
230228
let response = request()
231229
.method(method)
232230
.path(&format!("/{SERVER_BASE_PATH}{path}"))
233-
.reply(&setup_router(Arc::new(dependency_manager)))
231+
.reply(&setup_router(RouterState::new_with_dummy_config(Arc::new(
232+
dependency_manager,
233+
))))
234234
.await;
235235

236236
APISpec::verify_conformity(
@@ -260,7 +260,9 @@ pub mod tests {
260260
request()
261261
.method(method)
262262
.path(&format!("/{SERVER_BASE_PATH}{path}"))
263-
.reply(&setup_router(dependency_manager.clone()))
263+
.reply(&setup_router(RouterState::new_with_dummy_config(
264+
dependency_manager.clone(),
265+
)))
264266
.await;
265267

266268
assert_eq!(
@@ -278,7 +280,9 @@ pub mod tests {
278280
request()
279281
.method(method)
280282
.path(&format!("/{SERVER_BASE_PATH}{base_path}/123"))
281-
.reply(&setup_router(dependency_manager.clone()))
283+
.reply(&setup_router(RouterState::new_with_dummy_config(
284+
dependency_manager.clone(),
285+
)))
282286
.await;
283287

284288
assert_eq!(
@@ -308,7 +312,9 @@ pub mod tests {
308312
let response = request()
309313
.method(method)
310314
.path(&format!("/{SERVER_BASE_PATH}{path}"))
311-
.reply(&setup_router(Arc::new(dependency_manager)))
315+
.reply(&setup_router(RouterState::new_with_dummy_config(Arc::new(
316+
dependency_manager,
317+
))))
312318
.await;
313319

314320
APISpec::verify_conformity(
@@ -339,7 +345,9 @@ pub mod tests {
339345
let response = request()
340346
.method(method)
341347
.path(&format!("/{SERVER_BASE_PATH}{path}"))
342-
.reply(&setup_router(Arc::new(dependency_manager)))
348+
.reply(&setup_router(RouterState::new_with_dummy_config(Arc::new(
349+
dependency_manager,
350+
))))
343351
.await;
344352

345353
APISpec::verify_conformity(
@@ -370,7 +378,9 @@ pub mod tests {
370378
let response = request()
371379
.method(method)
372380
.path(&format!("/{SERVER_BASE_PATH}{path}"))
373-
.reply(&setup_router(Arc::new(dependency_manager)))
381+
.reply(&setup_router(RouterState::new_with_dummy_config(Arc::new(
382+
dependency_manager,
383+
))))
374384
.await;
375385

376386
APISpec::verify_conformity(
@@ -402,7 +412,9 @@ pub mod tests {
402412
let response = request()
403413
.method(method)
404414
.path(&format!("/{SERVER_BASE_PATH}{base_path}/123"))
405-
.reply(&setup_router(Arc::new(dependency_manager)))
415+
.reply(&setup_router(RouterState::new_with_dummy_config(Arc::new(
416+
dependency_manager,
417+
))))
406418
.await;
407419

408420
APISpec::verify_conformity(
@@ -429,7 +441,9 @@ pub mod tests {
429441
let response = request()
430442
.method(method)
431443
.path(&format!("/{SERVER_BASE_PATH}{base_path}/invalid-epoch"))
432-
.reply(&setup_router(Arc::new(dependency_manager)))
444+
.reply(&setup_router(RouterState::new_with_dummy_config(Arc::new(
445+
dependency_manager,
446+
))))
433447
.await;
434448

435449
APISpec::verify_conformity(
@@ -460,7 +474,9 @@ pub mod tests {
460474
let response = request()
461475
.method(method)
462476
.path(&format!("/{SERVER_BASE_PATH}{base_path}/123"))
463-
.reply(&setup_router(Arc::new(dependency_manager)))
477+
.reply(&setup_router(RouterState::new_with_dummy_config(Arc::new(
478+
dependency_manager,
479+
))))
464480
.await;
465481

466482
APISpec::verify_conformity(
@@ -491,7 +507,9 @@ pub mod tests {
491507
let response = request()
492508
.method(method)
493509
.path(&format!("/{SERVER_BASE_PATH}{base_path}/123"))
494-
.reply(&setup_router(Arc::new(dependency_manager)))
510+
.reply(&setup_router(RouterState::new_with_dummy_config(Arc::new(
511+
dependency_manager,
512+
))))
495513
.await;
496514

497515
APISpec::verify_conformity(

mithril-aggregator/src/http_server/routes/artifact_routes/cardano_transaction.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
11
use crate::http_server::routes::middlewares;
2-
use crate::DependencyContainer;
2+
use crate::http_server::routes::router::RouterState;
33
use warp::Filter;
44

55
pub fn routes(
6-
dependency_manager: &DependencyContainer,
6+
router_state: &RouterState,
77
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
8-
artifact_cardano_transactions(dependency_manager)
9-
.or(artifact_cardano_transaction_by_id(dependency_manager))
8+
artifact_cardano_transactions(router_state).or(artifact_cardano_transaction_by_id(router_state))
109
}
1110

1211
/// GET /artifact/cardano-transactions
1312
fn artifact_cardano_transactions(
14-
dependency_manager: &DependencyContainer,
13+
router_state: &RouterState,
1514
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
1615
warp::path!("artifact" / "cardano-transactions")
1716
.and(warp::get())
18-
.and(middlewares::with_logger(dependency_manager))
19-
.and(middlewares::with_http_message_service(dependency_manager))
17+
.and(middlewares::with_logger(router_state))
18+
.and(middlewares::with_http_message_service(router_state))
2019
.and_then(handlers::list_artifacts)
2120
}
2221

2322
/// GET /artifact/cardano-transaction/:id
2423
fn artifact_cardano_transaction_by_id(
25-
dependency_manager: &DependencyContainer,
24+
router_state: &RouterState,
2625
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
2726
warp::path!("artifact" / "cardano-transaction" / String)
2827
.and(warp::get())
29-
.and(middlewares::with_logger(dependency_manager))
30-
.and(middlewares::with_http_message_service(dependency_manager))
31-
.and(middlewares::with_metrics_service(dependency_manager))
28+
.and(middlewares::with_logger(router_state))
29+
.and(middlewares::with_http_message_service(router_state))
30+
.and(middlewares::with_metrics_service(router_state))
3231
.and_then(handlers::get_artifact_by_signed_entity_id)
3332
}
3433

@@ -117,7 +116,7 @@ pub mod tests {
117116
use super::*;
118117

119118
fn setup_router(
120-
dependency_manager: Arc<DependencyContainer>,
119+
state: RouterState,
121120
) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
122121
let cors = warp::cors()
123122
.allow_any_origin()
@@ -126,7 +125,7 @@ pub mod tests {
126125

127126
warp::any()
128127
.and(warp::path(SERVER_BASE_PATH))
129-
.and(routes(&dependency_manager).with(cors))
128+
.and(routes(&state).with(cors))
130129
}
131130

132131
#[tokio::test]
@@ -150,7 +149,9 @@ pub mod tests {
150149
let response = request()
151150
.method(method)
152151
.path(&format!("/{SERVER_BASE_PATH}{path}"))
153-
.reply(&setup_router(Arc::new(dependency_manager)))
152+
.reply(&setup_router(RouterState::new_with_dummy_config(Arc::new(
153+
dependency_manager,
154+
))))
154155
.await;
155156

156157
APISpec::verify_conformity(
@@ -181,7 +182,9 @@ pub mod tests {
181182
let response = request()
182183
.method(method)
183184
.path(&format!("/{SERVER_BASE_PATH}{path}"))
184-
.reply(&setup_router(Arc::new(dependency_manager)))
185+
.reply(&setup_router(RouterState::new_with_dummy_config(Arc::new(
186+
dependency_manager,
187+
))))
185188
.await;
186189

187190
APISpec::verify_conformity(
@@ -210,7 +213,9 @@ pub mod tests {
210213
request()
211214
.method(method)
212215
.path(&format!("/{SERVER_BASE_PATH}{path}"))
213-
.reply(&setup_router(dependency_manager.clone()))
216+
.reply(&setup_router(RouterState::new_with_dummy_config(
217+
dependency_manager.clone(),
218+
)))
214219
.await;
215220

216221
assert_eq!(
@@ -246,7 +251,9 @@ pub mod tests {
246251
let response = request()
247252
.method(method)
248253
.path(&format!("/{SERVER_BASE_PATH}{path}"))
249-
.reply(&setup_router(Arc::new(dependency_manager)))
254+
.reply(&setup_router(RouterState::new_with_dummy_config(Arc::new(
255+
dependency_manager,
256+
))))
250257
.await;
251258

252259
APISpec::verify_conformity(
@@ -277,7 +284,9 @@ pub mod tests {
277284
let response = request()
278285
.method(method)
279286
.path(&format!("/{SERVER_BASE_PATH}{path}"))
280-
.reply(&setup_router(Arc::new(dependency_manager)))
287+
.reply(&setup_router(RouterState::new_with_dummy_config(Arc::new(
288+
dependency_manager,
289+
))))
281290
.await;
282291

283292
APISpec::verify_conformity(
@@ -308,7 +317,9 @@ pub mod tests {
308317
let response = request()
309318
.method(method)
310319
.path(&format!("/{SERVER_BASE_PATH}{path}"))
311-
.reply(&setup_router(Arc::new(dependency_manager)))
320+
.reply(&setup_router(RouterState::new_with_dummy_config(Arc::new(
321+
dependency_manager,
322+
))))
312323
.await;
313324

314325
APISpec::verify_conformity(

0 commit comments

Comments
 (0)