mithril_aggregator/http_server/routes/
middlewares.rs1use slog::{debug, Logger};
2use std::convert::Infallible;
3use std::sync::Arc;
4use warp::Filter;
5
6use mithril_common::api_version::APIVersionProvider;
7
8use crate::database::repository::SignerGetter;
9use crate::dependency_injection::EpochServiceWrapper;
10use crate::event_store::{EventMessage, TransmitterService};
11use crate::http_server::routes::http_server_child_logger;
12use crate::http_server::routes::router::{RouterConfig, RouterState};
13use crate::services::{CertifierService, MessageService, ProverService, SignedEntityService};
14use crate::{
15 MetricsService, SignerRegisterer, SingleSignatureAuthenticator, VerificationKeyStorer,
16};
17
18pub fn extract_config<D: Clone + Send>(
20 state: &RouterState,
21 extract: fn(&RouterConfig) -> D,
22) -> impl Filter<Extract = (D,), Error = Infallible> + Clone {
23 let config_value = extract(&state.configuration);
24 warp::any().map(move || config_value.clone())
25}
26
27pub(crate) fn with_logger(
29 router_state: &RouterState,
30) -> impl Filter<Extract = (Logger,), Error = Infallible> + Clone {
31 let logger = http_server_child_logger(&router_state.dependencies.root_logger);
32 warp::any().map(move || logger.clone())
33}
34
35pub(crate) fn log_route_call(
39 router_state: &RouterState,
40) -> warp::log::Log<impl Fn(warp::log::Info<'_>) + Clone> {
41 let logger = http_server_child_logger(&router_state.dependencies.root_logger);
42 warp::log::custom(move |info| {
43 debug!(
44 logger,
45 "{} {} {}",
46 info.method(),
47 info.path(),
48 info.status()
49 )
50 })
51}
52
53pub fn with_signer_registerer(
55 router_state: &RouterState,
56) -> impl Filter<Extract = (Arc<dyn SignerRegisterer>,), Error = Infallible> + Clone {
57 let signer_register = router_state.dependencies.signer_registerer.clone();
58 warp::any().map(move || signer_register.clone())
59}
60
61pub fn with_signer_getter(
63 router_state: &RouterState,
64) -> impl Filter<Extract = (Arc<dyn SignerGetter>,), Error = Infallible> + Clone {
65 let signer_getter = router_state.dependencies.signer_getter.clone();
66 warp::any().map(move || signer_getter.clone())
67}
68
69pub fn with_event_transmitter(
71 router_state: &RouterState,
72) -> impl Filter<Extract = (Arc<TransmitterService<EventMessage>>,), Error = Infallible> + Clone {
73 let event_transmitter = router_state.dependencies.event_transmitter.clone();
74 warp::any().map(move || event_transmitter.clone())
75}
76
77pub fn with_certifier_service(
79 router_state: &RouterState,
80) -> impl Filter<Extract = (Arc<dyn CertifierService>,), Error = Infallible> + Clone {
81 let certifier_service = router_state.dependencies.certifier_service.clone();
82 warp::any().map(move || certifier_service.clone())
83}
84
85pub fn with_epoch_service(
87 router_state: &RouterState,
88) -> impl Filter<Extract = (EpochServiceWrapper,), Error = Infallible> + Clone {
89 let epoch_service = router_state.dependencies.epoch_service.clone();
90 warp::any().map(move || epoch_service.clone())
91}
92
93pub fn with_signed_entity_service(
95 router_state: &RouterState,
96) -> impl Filter<Extract = (Arc<dyn SignedEntityService>,), Error = Infallible> + Clone {
97 let signed_entity_service = router_state.dependencies.signed_entity_service.clone();
98 warp::any().map(move || signed_entity_service.clone())
99}
100
101pub fn with_verification_key_store(
103 router_state: &RouterState,
104) -> impl Filter<Extract = (Arc<dyn VerificationKeyStorer>,), Error = Infallible> + Clone {
105 let verification_key_store = router_state.dependencies.verification_key_store.clone();
106 warp::any().map(move || verification_key_store.clone())
107}
108
109pub fn with_api_version_provider(
111 router_state: &RouterState,
112) -> impl Filter<Extract = (Arc<APIVersionProvider>,), Error = Infallible> + Clone {
113 let api_version_provider = router_state.dependencies.api_version_provider.clone();
114 warp::any().map(move || api_version_provider.clone())
115}
116
117pub fn with_http_message_service(
119 router_state: &RouterState,
120) -> impl Filter<Extract = (Arc<dyn MessageService>,), Error = Infallible> + Clone {
121 let message_service = router_state.dependencies.message_service.clone();
122 warp::any().map(move || message_service.clone())
123}
124
125pub fn with_prover_service(
127 router_state: &RouterState,
128) -> impl Filter<Extract = (Arc<dyn ProverService>,), Error = Infallible> + Clone {
129 let prover_service = router_state.dependencies.prover_service.clone();
130 warp::any().map(move || prover_service.clone())
131}
132
133pub fn with_single_signature_authenticator(
135 router_state: &RouterState,
136) -> impl Filter<Extract = (Arc<SingleSignatureAuthenticator>,), Error = Infallible> + Clone {
137 let single_signer_authenticator = router_state
138 .dependencies
139 .single_signer_authenticator
140 .clone();
141 warp::any().map(move || single_signer_authenticator.clone())
142}
143
144pub fn with_metrics_service(
146 router_state: &RouterState,
147) -> impl Filter<Extract = (Arc<MetricsService>,), Error = Infallible> + Clone {
148 let metrics_service = router_state.dependencies.metrics_service.clone();
149 warp::any().map(move || metrics_service.clone())
150}
151
152pub mod validators {
153 use crate::http_server::validators::ProverTransactionsHashValidator;
154
155 use super::*;
156
157 pub fn with_prover_transactions_hash_validator(
159 router_state: &RouterState,
160 ) -> impl Filter<Extract = (ProverTransactionsHashValidator,), Error = Infallible> + Clone {
161 let max_hashes = router_state
162 .configuration
163 .cardano_transactions_prover_max_hashes_allowed_by_request;
164
165 warp::any().map(move || ProverTransactionsHashValidator::new(max_hashes))
166 }
167}