|
| 1 | +mod handlers; |
1 | 2 | mod shared_state;
|
2 | 3 |
|
3 | 4 | use std::{
|
@@ -62,77 +63,43 @@ impl CliArguments {
|
62 | 63 | }
|
63 | 64 |
|
64 | 65 | /// 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 | +} |
66 | 73 |
|
67 | 74 | /// Tell axum how to convert `AppError` into a response.
|
68 | 75 | impl IntoResponse for AppError {
|
69 | 76 | fn into_response(self) -> Response<Body> {
|
70 |
| - error!("{}", self.0); |
| 77 | + match self { |
| 78 | + Self::Internal(e) => { |
| 79 | + error!("{}", e); |
71 | 80 |
|
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 | + } |
77 | 89 | }
|
78 | 90 | }
|
79 | 91 |
|
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. |
82 | 94 | impl<E> From<E> for AppError
|
83 | 95 | where
|
84 | 96 | E: Into<anyhow::Error>,
|
85 | 97 | {
|
86 | 98 | fn from(err: E) -> Self {
|
87 |
| - Self(err.into()) |
| 99 | + Self::Internal(err.into()) |
88 | 100 | }
|
89 | 101 | }
|
90 | 102 |
|
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 |
| - |
136 | 103 | pub struct OsSignalHandler;
|
137 | 104 |
|
138 | 105 | impl OsSignalHandler {
|
@@ -173,15 +140,19 @@ async fn main() -> StdResult<()> {
|
173 | 140 |
|
174 | 141 | trace!("configuring router…");
|
175 | 142 | 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)) |
178 | 145 | .route(
|
179 | 146 | "/aggregator/artifact/mithril-stake-distributions",
|
180 |
| - get(msds_handler), |
| 147 | + get(handlers::msds), |
181 | 148 | )
|
182 | 149 | .route(
|
183 | 150 | "/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), |
185 | 156 | )
|
186 | 157 | .with_state(shared_state.clone())
|
187 | 158 | .layer(
|
|
0 commit comments