separate hier_roots endpoint, resolve via redirects

feat/vaults
Tomáš Mládek 2021-12-17 23:42:58 +01:00
parent e0a603d154
commit 64906a1049
2 changed files with 29 additions and 25 deletions

View File

@ -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)

View File

@ -174,35 +174,38 @@ pub async fn list_hier(
path: web::Path<String>,
) -> Result<HttpResponse, Error> {
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::<Result<Vec<Vec<Entry>>>>().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<State>) -> Result<HttpResponse, Error> {
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::<Result<Vec<Vec<Entry>>>>()
.map_err(ErrorInternalServerError)?
.concat();
Ok(HttpResponse::Ok().json(result.as_hash().map_err(ErrorInternalServerError)?))
}
#[post("/api/refresh")]