mina_producer_dashboard/rpc/
filters.rs

1use warp::Filter;
2
3use crate::{storage::db_sled::Database, NodeStatus};
4
5use super::{
6    handlers::{
7        get_all_time_summary, get_current_slot, get_epoch_data, get_epoch_data_summary,
8        get_genesis_timestamp, get_latest_epoch_data, get_latest_epoch_data_summary,
9        get_node_status,
10    },
11    PaginationParams,
12};
13
14pub fn filters(
15    storage: Database,
16    node_status: NodeStatus,
17    producer_pk: String,
18) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
19    let cors = warp::cors()
20        .allow_any_origin()
21        .allow_header("content-type")
22        .allow_method("GET");
23
24    genesis_timestamp()
25        .or(latest_epoch_data(storage.clone(), node_status.clone()))
26        .or(node(node_status.clone()))
27        .or(current_slot(node_status.clone()))
28        .or(epoch_summary(storage.clone(), producer_pk.clone()))
29        .or(epoch_data(storage.clone()))
30        .or(all_time_summary(storage.clone()))
31        .or(latest_epoch_summary(storage, node_status, producer_pk))
32        .with(cors)
33}
34
35fn genesis_timestamp() -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone
36{
37    warp::path!("genesis_timestamp")
38        .and(warp::get())
39        .and_then(get_genesis_timestamp)
40}
41
42fn node(
43    node_status: NodeStatus,
44) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
45    warp::path!("node")
46        .and(warp::get())
47        .and(with_node_status(node_status))
48        .and_then(get_node_status)
49}
50
51fn current_slot(
52    node_status: NodeStatus,
53) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
54    warp::path!("node" / "current_slot")
55        .and(warp::get())
56        .and(with_node_status(node_status))
57        .and_then(get_current_slot)
58}
59
60fn latest_epoch_data(
61    storage: Database,
62    node_status: NodeStatus,
63) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
64    warp::path!("epoch" / "latest")
65        .and(warp::get())
66        .and(with_storage(storage))
67        .and(with_node_status(node_status))
68        .and_then(get_latest_epoch_data)
69}
70
71fn epoch_data(
72    storage: Database,
73) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
74    warp::path!("epoch" / u32)
75        .and(warp::get())
76        .and(with_storage(storage))
77        .and_then(get_epoch_data)
78}
79
80fn latest_epoch_summary(
81    storage: Database,
82    node_status: NodeStatus,
83    producer_pk: String,
84) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
85    warp::path!("epoch" / "summary" / "latest")
86        .and(warp::get())
87        .and(with_storage(storage))
88        .and(with_node_status(node_status))
89        .and(with_producer_pk(producer_pk))
90        .and_then(get_latest_epoch_data_summary)
91}
92
93fn epoch_summary(
94    storage: Database,
95    producer_pk: String,
96) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
97    warp::path!("epoch" / "summary" / u32)
98        .and(warp::get())
99        .and(warp::query::<PaginationParams>())
100        .and(with_storage(storage))
101        .and(with_producer_pk(producer_pk))
102        .and_then(get_epoch_data_summary)
103}
104
105fn all_time_summary(
106    storage: Database,
107) -> impl Filter<Extract = (impl warp::Reply,), Error = warp::Rejection> + Clone {
108    warp::path!("summary")
109        .and(warp::get())
110        .and(with_storage(storage))
111        .and_then(get_all_time_summary)
112}
113
114fn with_storage(
115    storage: Database,
116) -> impl Filter<Extract = (Database,), Error = std::convert::Infallible> + Clone {
117    warp::any().map(move || storage.clone())
118}
119
120fn with_node_status(
121    node_status: NodeStatus,
122) -> impl Filter<Extract = (NodeStatus,), Error = std::convert::Infallible> + Clone {
123    warp::any().map(move || node_status.clone())
124}
125
126fn with_producer_pk(
127    producer_pk: String,
128) -> impl Filter<Extract = (String,), Error = std::convert::Infallible> + Clone {
129    warp::any().map(move || producer_pk.clone())
130}