Skip to content

Commit

Permalink
fix(httpd): replace the setup endpoint with a cmd argument
Browse files Browse the repository at this point in the history
Signed-off-by: Tarek <tareknaser360@gmail.com>
  • Loading branch information
tareknaser committed Jul 10, 2023
1 parent b4dc533 commit 5b4b3be
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 42 deletions.
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
17 changes: 0 additions & 17 deletions coffee_httpd/src/httpd/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ pub async fn run_httpd<T: ToSocketAddrs>(
.wrap_api()
.service(swagger_api)
.service(coffee_help)
.service(coffee_setup)
.service(coffee_install)
.service(coffee_remove)
.service(coffee_list)
Expand All @@ -79,22 +78,6 @@ async fn coffee_help(
Ok(body)
}

#[api_v2_operation]
#[post("/setup")]
async fn coffee_setup(data: web::Data<AppState>, body: Json<Setup>) -> Result<HttpResponse, Error> {
let cln_dir = &body.cln_dir;

let mut coffee = data.coffee.lock().await;
let result = coffee.setup(cln_dir).await;

match result {
Ok(_) => Ok(HttpResponse::Ok().body(format!("Coffee setup completed successfully"))),
Err(err) => Err(actix_web::error::ErrorInternalServerError(format!(
"Failed to perform Coffee setup: {err}"
))),
}
}

#[api_v2_operation]
#[post("/install")]
async fn coffee_install(
Expand Down
5 changes: 3 additions & 2 deletions coffee_httpd/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::Parser;
use log;

use coffee_core::coffee::CoffeeManager;
use coffee_lib::plugin_manager::PluginManager;

mod cmd;
pub mod httpd;
Expand All @@ -14,7 +14,8 @@ async fn main() {
if let Err(err) = &coffee {
println!("{err}");
}
let coffee = coffee.unwrap();
let mut coffee = coffee.unwrap();
coffee.setup(&cmd.cln_path).await.unwrap();

let port = cmd.port.unwrap_or(8080) as u16;
log::info!("Running on port 127.0.0.1:{port}");
Expand Down
15 changes: 12 additions & 3 deletions coffee_testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub mod macros {

#[macro_export]
macro_rules! httpd {
($dir:expr, $port:expr, $($opt_args:tt)*) => {
($dir:expr, $port:expr, $cln_path:expr, $($opt_args:tt)*) => {
async {
use tokio::process::Command;

Expand All @@ -57,6 +57,7 @@ pub mod macros {
.args(&args_tok)
.arg("--host=127.0.0.1")
.arg(format!("--port={}", $port))
.arg(format!("--cln-path={}", $cln_path))
.arg(format!("--data-dir={}", $dir.path().to_str().unwrap()))
.spawn()
}.await
Expand Down Expand Up @@ -154,6 +155,7 @@ pub struct CoffeeHTTPDTesting {
root_path: Arc<TempDir>,
httpd_pid: tokio::process::Child,
httpd_port: u64,
cln_path: String,
}

impl Drop for CoffeeHTTPDTesting {
Expand All @@ -172,22 +174,29 @@ impl Drop for CoffeeHTTPDTesting {

impl CoffeeHTTPDTesting {
/// init coffee httpd in a tmp directory.
pub async fn tmp() -> anyhow::Result<Self> {
pub async fn tmp(cln_path: String) -> anyhow::Result<Self> {
let dir = tempfile::tempdir()?;
let port = port::random_free_port().unwrap();
let child = httpd!(dir, port, "{}", format!("--network=regtest"))?;
let child = httpd!(dir, port, cln_path, "{}", format!("--network=regtest"))?;
wait_for!(async { reqwest::get(format!("http://127.0.0.1:{}/list", port)).await });
Ok(Self {
root_path: Arc::new(dir),
httpd_pid: child,
httpd_port: port.into(),
cln_path,
})
}

// get the root path of coffee.
pub fn root_path(&self) -> Arc<TempDir> {
self.root_path.clone()
}

// get the path of core lightning.
pub fn cln_path(&self) -> String {
self.cln_path.clone()
}

/// run the httpd daemon as process and return the URL
/// this should allow to make integration testing to the httpd.
pub fn url(&self) -> String {
Expand Down
2 changes: 1 addition & 1 deletion docs/docs-book/src/using-coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Please note that the server runs on `localhost` with port `8080` where you can f
To start the Coffee server, run the following command:

```shell
coffee_httpd --network <network>
coffee_httpd --cln-path <core_lightning_path> --network <network>
```

Make sure the `coffee_httpd` binary is in your system PATH or in the current working directory.
22 changes: 3 additions & 19 deletions tests/src/coffee_httpd_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,16 @@ pub async fn httpd_init_add_remote() {
init();

let mut cln = Node::tmp().await.unwrap();
let manager = CoffeeHTTPDTesting::tmp().await.unwrap();
let lightning_dir = cln.rpc().getinfo().unwrap().ligthning_dir;
let lightning_dir = lightning_dir.strip_suffix("/regtest").unwrap();
let manager = CoffeeHTTPDTesting::tmp(lightning_dir.to_string())
.await
.unwrap();
log::info!("lightning path: {lightning_dir}");
let url = manager.url();
log::info!("base url: {url}");
let client = reqwest::Client::new();

// Define the request body to be sent to the /setup endpoint
let setup_request = Setup {
cln_dir: lightning_dir.to_string(),
};

// Send the request to set up coffee
let response = client
.post(format!("{}/setup", url))
.json(&setup_request)
.send()
.await
.unwrap();

// Check the response status code, log the body.
assert!(response.status().is_success());
let body = response.text().await.unwrap();
log::info!("/setup response: {}", body);

// Define the request body to be sent to the /remote/add endpoint
let remote_add_request = RemoteAdd {
repository_name: "lightningd".to_string(),
Expand Down

0 comments on commit 5b4b3be

Please sign in to comment.