From 38eb9c74ec1024b65074736d3a6e8f323950a2c4 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sat, 11 Feb 2023 19:42:48 +0530 Subject: [PATCH] feat: get latest tarball from REST API --- src/api.rs | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 2 +- src/routes.rs | 1 + 3 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/api.rs diff --git a/src/api.rs b/src/api.rs new file mode 100644 index 0000000..7ec8da4 --- /dev/null +++ b/src/api.rs @@ -0,0 +1,60 @@ +/* + * ForgeFlux StarChart - A federated software forge spider + * Copyright (C) 2022 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 + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ +use actix_web::web; +use actix_web::{HttpResponse, Responder}; +use actix_web_codegen_const_routes::get; +use serde::{Deserialize, Serialize}; + +use crate::errors::*; +use crate::WebFederate; + +pub const ROUTES: Api = Api::new(); + +#[derive(Deserialize, Serialize, Clone, Debug, Eq, PartialEq)] +pub struct Api { + pub get_latest: &'static str, +} + +impl Api { + const fn new() -> Api { + let get_latest = "/api/v1/federated/latest"; + Api { get_latest } + } +} + +#[derive(Deserialize, Serialize, Clone, Debug, Eq, PartialEq)] +pub struct LatestResp { + pub latest: String, +} + +impl LatestResp { + pub async fn new(federate: &WebFederate) -> Self { + let latest = federate.latest_tar().await.unwrap(); + LatestResp { latest } + } +} + +#[get(path = "ROUTES.get_latest")] +pub async fn lastest(federate: WebFederate) -> ServiceResult { + let latest = LatestResp::new(&federate).await; + Ok(HttpResponse::Ok().json(latest)) +} + +pub fn services(cfg: &mut web::ServiceConfig) { + cfg.service(lastest); +} diff --git a/src/main.rs b/src/main.rs index d0b52fe..6b837ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,6 +22,7 @@ use actix_web::{middleware, web::Data, App, HttpServer}; use lazy_static::lazy_static; use tokio::sync::oneshot; +pub mod api; pub mod ctx; pub mod db; pub mod dns; @@ -82,7 +83,6 @@ async fn main() { let crawler_fut = tokio::spawn(spider::Crawler::start(crawler.clone())); let ctx = WebCtx::new(ctx); let socket_addr = settings.server.get_ip(); - HttpServer::new(move || { App::new() .wrap(middleware::Logger::default()) diff --git a/src/routes.rs b/src/routes.rs index 5cf0dba..92409ff 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -17,6 +17,7 @@ */ pub fn services(cfg: &mut actix_web::web::ServiceConfig) { + crate::api::services(cfg); crate::pages::services(cfg); crate::static_assets::services(cfg); }