Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(httpd): implement install and remove enpoints #157

Merged
merged 4 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion coffee_cmd/src/coffee_term/command_show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use radicle_term::table::TableOptions;

use coffee_lib::error;
use coffee_lib::errors::CoffeeError;
use coffee_lib::types::{CoffeeList, CoffeeRemote};
use coffee_lib::types::response::{CoffeeList, CoffeeRemote};
use term::Element;

pub fn show_list(coffee_list: Result<CoffeeList, CoffeeError>) -> Result<(), CoffeeError> {
Expand Down
2 changes: 1 addition & 1 deletion coffee_cmd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use radicle_term as term;
use coffee_core::coffee::CoffeeManager;
use coffee_lib::errors::CoffeeError;
use coffee_lib::plugin_manager::PluginManager;
use coffee_lib::types::{NurseStatus, UpgradeStatus};
use coffee_lib::types::response::{NurseStatus, UpgradeStatus};

use crate::cmd::CoffeeArgs;
use crate::cmd::CoffeeCommand;
Expand Down
2 changes: 1 addition & 1 deletion coffee_core/src/coffee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use coffee_lib::error;
use coffee_lib::errors::CoffeeError;
use coffee_lib::plugin_manager::PluginManager;
use coffee_lib::repository::Repository;
use coffee_lib::types::*;
use coffee_lib::types::response::*;
use coffee_lib::url::URL;
use coffee_storage::model::repository::{Kind, Repository as RepositoryInfo};
use coffee_storage::storage::StorageManager;
Expand Down
2 changes: 1 addition & 1 deletion coffee_github/src/repository.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::any::Any;

use async_trait::async_trait;
use coffee_lib::types::CoffeeUpgrade;
use coffee_lib::types::response::CoffeeUpgrade;
use git2;
use log::debug;
use tokio::fs::File;
Expand Down
2 changes: 1 addition & 1 deletion coffee_github/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use coffee_lib::errors::CoffeeError;
use coffee_lib::url::URL;
use log::debug;

use coffee_lib::types::UpgradeStatus;
use coffee_lib::types::response::UpgradeStatus;

pub async fn clone_recursive_fix(repo: git2::Repository, url: &URL) -> Result<(), CoffeeError> {
let repository = repo.submodules().unwrap_or_default();
Expand Down
3 changes: 2 additions & 1 deletion coffee_httpd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ actix-web = "4"
clap = "4.1.11"
tokio = { version = "1.22.0", features = ["sync"] }
coffee_core = { path = "../coffee_core" }
coffee_lib = { path = "../coffee_lib" }
coffee_lib = { path = "../coffee_lib", features = ["open-api"] }
paperclip = { version = "0.8.0", features = ["actix4"] }
log = "0.4.17"
env_logger = "0.9.3"
serde = "1"
serde_json = "1"
2 changes: 2 additions & 0 deletions coffee_httpd/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub struct HttpdArgs {
#[clap(long, value_parser)]
pub network: Option<String>,
#[clap(long, value_parser)]
pub cln_path: String,
#[clap(long, value_parser)]
pub data_dir: Option<String>,
#[clap(long, value_parser)]
pub host: Option<String>,
Expand Down
1 change: 1 addition & 0 deletions coffee_httpd/src/httpd.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod server;
pub use server::*;
pub mod macros;
15 changes: 15 additions & 0 deletions coffee_httpd/src/httpd/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// handle_httpd_response macro is the macro that handles HTTPD responses
#[macro_export]
macro_rules! handle_httpd_response {
($result:expr, $msg:expr) => {
match $result {
Ok(_) => Ok(HttpResponse::Ok().body(format!($msg))),
Err(err) => Err(actix_web::error::ErrorInternalServerError(format!(
"Error: {}",
err
))),
}
};
}

pub use handle_httpd_response;
72 changes: 43 additions & 29 deletions coffee_httpd/src/httpd/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ use std::sync::Arc;
use serde_json::Value;
use tokio::sync::Mutex;

use super::macros::handle_httpd_response;
use coffee_core::coffee::CoffeeManager;
use coffee_lib::plugin_manager::PluginManager;
use coffee_lib::types::request::*;

use actix_web::{App, HttpResponse};
use actix_web::{Error, HttpServer};
Expand Down Expand Up @@ -51,6 +53,8 @@ pub async fn run_httpd<T: ToSocketAddrs>(
.wrap_api()
.service(swagger_api)
.service(coffee_help)
.service(coffee_install)
.service(coffee_remove)
.service(coffee_list)
.service(coffee_remote_add)
.service(coffee_remote_rm)
Expand All @@ -74,6 +78,35 @@ async fn coffee_help(
Ok(body)
}

#[api_v2_operation]
#[post("/install")]
async fn coffee_install(
data: web::Data<AppState>,
body: Json<Install>,
) -> Result<HttpResponse, Error> {
let plugin = &body.plugin;
let try_dynamic = body.try_dynamic;

let mut coffee = data.coffee.lock().await;
let result = coffee.install(plugin, false, try_dynamic).await;

handle_httpd_response!(result, "Plugin '{plugin}' installed successfully")
}

#[api_v2_operation]
#[post("/remove")]
async fn coffee_remove(
data: web::Data<AppState>,
body: Json<Remove>,
) -> Result<HttpResponse, Error> {
let plugin = &body.plugin;

let mut coffee = data.coffee.lock().await;
let result = coffee.remove(plugin).await;

handle_httpd_response!(result, "Plugin '{plugin}' removed successfully")
}

#[api_v2_operation]
#[get("/list")]
async fn coffee_list(data: web::Data<AppState>) -> Result<Json<Value>, Error> {
Expand All @@ -96,51 +129,32 @@ async fn coffee_list(data: web::Data<AppState>) -> Result<Json<Value>, Error> {
#[post("/remote/add")]
async fn coffee_remote_add(
data: web::Data<AppState>,
body: Json<HashMap<String, String>>,
body: Json<RemoteAdd>,
) -> Result<HttpResponse, Error> {
let repository_name = body.get("repository_name").ok_or_else(|| {
actix_web::error::ErrorBadRequest("Missing 'repository_name' field in the request body")
})?;
let repository_url = body.get("repository_url").ok_or_else(|| {
actix_web::error::ErrorBadRequest("Missing 'repository_url' field in the request body")
})?;
let repository_name = &body.repository_name;
let repository_url = &body.repository_url;

let mut coffee = data.coffee.lock().await;
let result = coffee.add_remote(repository_name, repository_url).await;

match result {
Ok(_) => Ok(HttpResponse::Ok().body(format!(
"Repository '{}' added successfully",
repository_name
))),
Err(err) => Err(actix_web::error::ErrorInternalServerError(format!(
"Failed to add repository: {err}"
))),
}
handle_httpd_response!(result, "Repository '{repository_name}' added successfully")
}

#[api_v2_operation]
#[post("/remote/rm")]
async fn coffee_remote_rm(
data: web::Data<AppState>,
body: Json<HashMap<String, String>>,
body: Json<RemoteRm>,
) -> Result<HttpResponse, Error> {
let repository_name = body.get("repository_name").ok_or_else(|| {
actix_web::error::ErrorBadRequest("Missing 'repository_name' field in the request body")
})?;
let repository_name = &body.repository_name;

let mut coffee = data.coffee.lock().await;
let result = coffee.rm_remote(repository_name).await;

match result {
Ok(_) => Ok(HttpResponse::Ok().body(format!(
"Repository '{}' removed successfully",
repository_name
))),
Err(err) => Err(actix_web::error::ErrorInternalServerError(format!(
"Failed to remove repository: {err}"
))),
}
handle_httpd_response!(
result,
"Repository '{repository_name}' removed successfully"
)
}

#[api_v2_operation]
Expand Down
17 changes: 9 additions & 8 deletions coffee_httpd/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
use clap::Parser;
use log;

use coffee_core::coffee::CoffeeManager;
use coffee_lib::errors::CoffeeError;
use coffee_lib::macros::error;
use coffee_lib::plugin_manager::PluginManager;

mod cmd;
pub mod httpd;

#[actix_web::main]
async fn main() {
async fn main() -> Result<(), CoffeeError> {
env_logger::init();
let cmd = cmd::HttpdArgs::parse();
let coffee = CoffeeManager::new(&cmd).await;
if let Err(err) = &coffee {
println!("{err}");
}
let coffee = coffee.unwrap();
let mut coffee = CoffeeManager::new(&cmd).await?;
coffee.setup(&cmd.cln_path).await?;

let port = cmd.port.unwrap_or(8080) as u16;
log::info!("Running on port 127.0.0.1:{port}");
if let Err(err) = httpd::run_httpd(coffee, ("127.0.0.1", port)).await {
println!("{err}");
return Err(error!("Error while running the httpd: {err}"));
}

Ok(())
}
4 changes: 4 additions & 0 deletions coffee_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ git2 = "0.16.1"
log = "0.4.17"
env_logger = "0.9.3"
tokio = { version = "1.22.0", features = ["process"] }
paperclip = { version = "0.8.0", features = ["actix4"], optional = true }

[features]
open-api = ["dep:paperclip"]
3 changes: 1 addition & 2 deletions coffee_lib/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,4 @@ macro_rules! sh {
};
}

pub use error;
pub use sh;
pub use {error, sh};
2 changes: 1 addition & 1 deletion coffee_lib/src/plugin_manager.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Plugin manager module definition.
use async_trait::async_trait;

use crate::{errors::CoffeeError, types::*};
use crate::{errors::CoffeeError, types::response::*};

/// Plugin manager traits that define the API a generic
/// plugin manager.
Expand Down
2 changes: 1 addition & 1 deletion coffee_lib/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::errors::CoffeeError;
use crate::plugin::Plugin;
use crate::url::URL;

use crate::types::CoffeeUpgrade;
use crate::types::response::CoffeeUpgrade;

use async_trait::async_trait;

Expand Down
Loading
Loading