Spaces:
Sleeping
Sleeping
Update src/http.rs
Browse files- src/http.rs +42 -0
src/http.rs
CHANGED
|
@@ -62,6 +62,8 @@ pub fn router(state: AppState) -> Router {
|
|
| 62 |
.route("/get/:uuid/blocks/:index", get(blocks_part))
|
| 63 |
.route("/get/:uuid/parts", get(parts_info))
|
| 64 |
.route("/get/:uuid/parts/:index", get(parts_part))
|
|
|
|
|
|
|
| 65 |
// тело /upload — одна ссылка
|
| 66 |
.layer(DefaultBodyLimit::max(16 * 1024))
|
| 67 |
.with_state(state)
|
|
@@ -97,6 +99,7 @@ async fn index() -> Json<Value> {
|
|
| 97 |
"GET /get/{uuid}/blocks/{index}",
|
| 98 |
"GET /get/{uuid}/parts",
|
| 99 |
"GET /get/{uuid}/parts/{index}",
|
|
|
|
| 100 |
],
|
| 101 |
}))
|
| 102 |
}
|
|
@@ -176,6 +179,21 @@ async fn parts_part(
|
|
| 176 |
Ok(Json(json!({ "index": i, "data": chunk })))
|
| 177 |
}
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
#[cfg(test)]
|
| 180 |
mod tests {
|
| 181 |
use super::*;
|
|
@@ -272,6 +290,30 @@ mod tests {
|
|
| 272 |
}
|
| 273 |
}
|
| 274 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
#[tokio::test]
|
| 276 |
async fn bad_index_is_400() {
|
| 277 |
let (app, state) = app();
|
|
|
|
| 62 |
.route("/get/:uuid/blocks/:index", get(blocks_part))
|
| 63 |
.route("/get/:uuid/parts", get(parts_info))
|
| 64 |
.route("/get/:uuid/parts/:index", get(parts_part))
|
| 65 |
+
.route("/:uuid/status", get(status))
|
| 66 |
+
.route("/get/:uuid/status", get(status))
|
| 67 |
// тело /upload — одна ссылка
|
| 68 |
.layer(DefaultBodyLimit::max(16 * 1024))
|
| 69 |
.with_state(state)
|
|
|
|
| 99 |
"GET /get/{uuid}/blocks/{index}",
|
| 100 |
"GET /get/{uuid}/parts",
|
| 101 |
"GET /get/{uuid}/parts/{index}",
|
| 102 |
+
"GET /{uuid}/status",
|
| 103 |
],
|
| 104 |
}))
|
| 105 |
}
|
|
|
|
| 179 |
Ok(Json(json!({ "index": i, "data": chunk })))
|
| 180 |
}
|
| 181 |
|
| 182 |
+
/// Схематика разбирается прямо в `/upload`, поэтому любая найденная — готова.
|
| 183 |
+
async fn status(
|
| 184 |
+
State(state): State<AppState>,
|
| 185 |
+
Path(uuid): Path<String>,
|
| 186 |
+
) -> Result<Json<Value>, ApiError> {
|
| 187 |
+
let s = find(&state, &uuid)?;
|
| 188 |
+
Ok(Json(json!({
|
| 189 |
+
"status": "ready",
|
| 190 |
+
"unique_blocks": s.unique_blocks,
|
| 191 |
+
"blocks_parts": s.block_parts.len(),
|
| 192 |
+
"parts": encoder::part_count(s.text.len(), PART_CHARS),
|
| 193 |
+
"text_chars": s.text.len(),
|
| 194 |
+
})))
|
| 195 |
+
}
|
| 196 |
+
|
| 197 |
#[cfg(test)]
|
| 198 |
mod tests {
|
| 199 |
use super::*;
|
|
|
|
| 290 |
}
|
| 291 |
}
|
| 292 |
|
| 293 |
+
#[tokio::test]
|
| 294 |
+
async fn status_endpoint() {
|
| 295 |
+
let (app, state) = app();
|
| 296 |
+
let id = sample_id(&state);
|
| 297 |
+
for uri in [format!("/{id}/status"), format!("/get/{id}/status")] {
|
| 298 |
+
let (st, v) = get_json(&app, &uri).await;
|
| 299 |
+
assert_eq!(st, StatusCode::OK, "{uri}");
|
| 300 |
+
assert_eq!(
|
| 301 |
+
v,
|
| 302 |
+
json!({
|
| 303 |
+
"status": "ready",
|
| 304 |
+
"unique_blocks": 2,
|
| 305 |
+
"blocks_parts": 1,
|
| 306 |
+
"parts": 1,
|
| 307 |
+
"text_chars": 44,
|
| 308 |
+
})
|
| 309 |
+
);
|
| 310 |
+
}
|
| 311 |
+
let (st, _) = get_json(&app, &format!("/{}/status", Uuid::new_v4())).await;
|
| 312 |
+
assert_eq!(st, StatusCode::NOT_FOUND);
|
| 313 |
+
let (st, _) = get_json(&app, "/not-a-uuid/status").await;
|
| 314 |
+
assert_eq!(st, StatusCode::BAD_REQUEST);
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
#[tokio::test]
|
| 318 |
async fn bad_index_is_400() {
|
| 319 |
let (app, state) = app();
|