diff --git a/src/main.rs b/src/main.rs index 3328f36..280565b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -120,6 +120,7 @@ fn main() -> Result<()> { .service(routes::delete_object) .service(routes::api_refresh) .service(routes::list_hier) + .service(routes::list_hier_roots) .service(routes::latest_files) .service(routes::get_file) .service(routes::get_jobs) diff --git a/src/routes.rs b/src/routes.rs index 5057693..c099745 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -174,35 +174,38 @@ pub async fn list_hier( path: web::Path, ) -> Result { let connection = state.db_pool.get().map_err(ErrorInternalServerError)?; - let upath: UHierPath = path.into_inner().parse().map_err(ErrorBadRequest)?; - trace!("Listing path \"{}\"", upath); - - let (addr, data) = if upath.0.is_empty() { - ( - None, - list_roots(&connection) - .map_err(ErrorInternalServerError)? - .into_iter() - .map(|root| { - retrieve_object(&connection, root) - }) - .collect::>>>().map_err(ErrorInternalServerError)? - .concat(), - ) + if path.is_empty() { + Ok(HttpResponse::MovedPermanently() + .header("Location", "/api/hier_roots") + .finish()) } else { + let upath: UHierPath = path.into_inner().parse().map_err(ErrorBadRequest)?; + trace!("Listing path \"{}\"", upath); + // todo: 500 if actual error occurs let path = resolve_path(&connection, &upath, false).map_err(ErrorNotFound)?; - let last = path.last().unwrap().clone(); - ( - Some(last.clone()), - retrieve_object(&connection, last).map_err(ErrorInternalServerError)?, - ) - }; + match path.last() { + Some(addr) => Ok(HttpResponse::Found() + .header("Location", format!("/api/obj/{}", addr)) + .finish()), + None => Ok(HttpResponse::NotFound().finish()), + } + } +} - Ok(HttpResponse::Ok().json(json!({ - "addr": addr, - "data": data - }))) +#[get("/api/hier_roots")] +pub async fn list_hier_roots(state: web::Data) -> Result { + let connection = state.db_pool.get().map_err(ErrorInternalServerError)?; + + let result = list_roots(&connection) + .map_err(ErrorInternalServerError)? + .into_iter() + .map(|root| retrieve_object(&connection, root)) + .collect::>>>() + .map_err(ErrorInternalServerError)? + .concat(); + + Ok(HttpResponse::Ok().json(result.as_hash().map_err(ErrorInternalServerError)?)) } #[post("/api/refresh")]