feat: expose mini-index via REST API

This commit is contained in:
Aravinth Manivannan 2023-03-02 17:47:44 +05:30
parent 30c245ea8f
commit 61a0a7bc11
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
3 changed files with 31 additions and 6 deletions

View file

@ -37,16 +37,27 @@ impl Search {
pub struct Introducer { pub struct Introducer {
pub list: &'static str, pub list: &'static str,
pub introduce: &'static str, pub introduce: &'static str,
pub get_mini_index: &'static str,
} }
impl Introducer { impl Introducer {
const fn new() -> Introducer { const fn new() -> Introducer {
let list = "/api/v1/introducer/list"; let list = "/api/v1/introducer/list";
let introduce = "/api/v1/introducer/new"; let introduce = "/api/v1/introducer/new";
Introducer { list, introduce } let get_mini_index = "/api/v1/introducer/mini-index";
Introducer {
list,
introduce,
get_mini_index,
}
} }
} }
#[derive(Deserialize, Serialize, Clone, Debug, Eq, PartialEq)]
pub struct MiniIndex {
pub mini_index: String,
}
#[derive(Deserialize, Serialize, Clone, Debug, Eq, PartialEq)] #[derive(Deserialize, Serialize, Clone, Debug, Eq, PartialEq)]
pub struct Api { pub struct Api {
pub get_latest: &'static str, pub get_latest: &'static str,

View file

@ -17,7 +17,7 @@
*/ */
use std::collections::HashSet; use std::collections::HashSet;
use actix_web::web; use actix_web::web::{self, get};
use actix_web::{HttpResponse, Responder}; use actix_web::{HttpResponse, Responder};
use actix_web_codegen_const_routes::get; use actix_web_codegen_const_routes::get;
use actix_web_codegen_const_routes::post; use actix_web_codegen_const_routes::post;
@ -116,6 +116,15 @@ impl Ctx {
} }
} }
#[get(path = "ROUTES.introducer.get_mini_index")]
pub async fn get_mini_index(db: WebDB) -> ServiceResult<impl Responder> {
let mini_index = db.export_mini_index().await?;
let resp = MiniIndex { mini_index };
Ok(HttpResponse::Ok().json(resp))
}
#[get(path = "ROUTES.introducer.list")] #[get(path = "ROUTES.introducer.list")]
pub async fn list_introductions( pub async fn list_introductions(
db: WebDB, db: WebDB,
@ -144,6 +153,7 @@ pub async fn new_introduction(
pub fn services(cfg: &mut web::ServiceConfig) { pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(list_introductions); cfg.service(list_introductions);
cfg.service(new_introduction); cfg.service(new_introduction);
cfg.service(get_mini_index);
} }
#[cfg(test)] #[cfg(test)]

View file

@ -35,10 +35,8 @@ pub async fn search_repository(
} else { } else {
format!("*{}*", payload.query) format!("*{}*", payload.query)
}; };
println!("search query: {}", query); let local_resp = db.search_repository(&query).await?;
let resp = db.search_repository(&query).await?; Ok(HttpResponse::Ok().json(local_resp))
println!("search_repository method: {:?}", resp);
Ok(HttpResponse::Ok().json(resp))
} }
pub fn services(cfg: &mut web::ServiceConfig) { pub fn services(cfg: &mut web::ServiceConfig) {
@ -130,6 +128,12 @@ mod tests {
assert!(!search_res.is_empty()); assert!(!search_res.is_empty());
assert_eq!(search_res.first().as_ref().unwrap().name, REPO_NAME); assert_eq!(search_res.first().as_ref().unwrap().name, REPO_NAME);
let mini_index_resp = get_request!(&app, ROUTES.introducer.get_mini_index);
assert_eq!(mini_index_resp.status(), StatusCode::OK);
let mini_index: api_routes::MiniIndex = test::read_body_json(mini_index_resp).await;
assert!(!mini_index.mini_index.is_empty());
assert!(mini_index.mini_index.contains(USERNAME));
// test ends // test ends
} }
} }