diff --git a/api_routes/src/lib.rs b/api_routes/src/lib.rs index e100869..48e3d34 100644 --- a/api_routes/src/lib.rs +++ b/api_routes/src/lib.rs @@ -36,12 +36,14 @@ impl Search { #[derive(Deserialize, Serialize, Clone, Debug, Eq, PartialEq)] pub struct Introducer { pub list: &'static str, + pub introduce: &'static str, } impl Introducer { const fn new() -> Introducer { let list = "/api/v1/introducer/list"; - Introducer { list } + let introduce = "/api/v1/introducer/new"; + Introducer { list, introduce } } } diff --git a/src/api.rs b/src/api.rs index 8fff35a..0c9c73b 100644 --- a/src/api.rs +++ b/src/api.rs @@ -1,6 +1,6 @@ /* * ForgeFlux StarChart - A federated software forge spider - * Copyright (C) 2022 Aravinth Manivannan + * Copyright (C) 2023 Aravinth Manivannan * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as @@ -44,6 +44,7 @@ pub async fn lastest(federate: WebFederate) -> ServiceResult { Ok(HttpResponse::Ok().json(latest)) } + pub fn services(cfg: &mut web::ServiceConfig) { cfg.service(lastest); cfg.service(forges); diff --git a/src/introduce.rs b/src/introduce.rs index daae672..417a1ee 100644 --- a/src/introduce.rs +++ b/src/introduce.rs @@ -20,6 +20,7 @@ use std::collections::HashSet; use actix_web::web; use actix_web::{HttpResponse, Responder}; use actix_web_codegen_const_routes::get; +use actix_web_codegen_const_routes::post; use url::Url; pub use api_routes::*; @@ -81,8 +82,19 @@ pub async fn list_introductions( Ok(HttpResponse::Ok().json(starcharts)) } +#[post(path = "ROUTES.introducer.introduce")] +pub async fn new_introduction( + db: WebDB, + payload: web::Json, +) -> ServiceResult { + db.add_starchart_to_introducer(&Url::parse(&payload.instance_url)?) + .await?; + Ok(HttpResponse::Ok()) +} + pub fn services(cfg: &mut web::ServiceConfig) { cfg.service(list_introductions); + cfg.service(new_introduction); } #[cfg(test)] @@ -93,7 +105,6 @@ mod tests { use actix_web::http::StatusCode; use actix_web::test; - use db_core::prelude::*; use url::Url; #[actix_rt::test] @@ -105,6 +116,22 @@ mod tests { db.add_starchart_to_introducer(&Url::parse(STARCHART_URL).unwrap()) .await .unwrap(); + + let payload = Starchart { + instance_url: STARCHART_URL.into(), + }; + + let resp = test::call_service( + &app, + post_request!(&payload, ROUTES.introducer.introduce).to_request(), + ) + .await; + if resp.status() != StatusCode::OK { + let resp_err: ErrorToResponse = test::read_body_json(resp).await; + panic!("{}", resp_err.error); + } + assert_eq!(resp.status(), StatusCode::OK); + let introductions_resp = get_request!(&app, ROUTES.introducer.list); assert_eq!(introductions_resp.status(), StatusCode::OK); let introductions: Vec = test::read_body_json(introductions_resp).await;