Use clap arguments

This commit is contained in:
Bogdan-Ștefan Neacșu
2023-08-24 13:23:56 +03:00
parent a7c71ab3d4
commit 14a9991fa4
6 changed files with 51 additions and 8 deletions
Generated
+3
View File
@@ -6578,6 +6578,9 @@ name = "nym-payment-manager"
version = "0.1.0"
dependencies = [
"anyhow",
"bip39",
"clap 4.3.21",
"lazy_static",
"log",
"nym-bin-common",
"nym-network-defaults",
+3
View File
@@ -7,6 +7,9 @@ edition = "2021"
[dependencies]
anyhow = "1.0"
bip39 = { workspace = true }
clap = { version = "4.0", features = ["cargo", "derive"] }
lazy_static = "1.4.0"
log = { workspace = true }
schemars = { version = "0.8", features = ["preserve_order"] }
rocket = { version = "0.5.0-rc.2", features = ["json"] }
+31
View File
@@ -0,0 +1,31 @@
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use clap::Parser;
use lazy_static::lazy_static;
use nym_bin_common::bin_info;
lazy_static! {
pub static ref PRETTY_BUILD_INFORMATION: String = bin_info!().pretty_print();
}
// Helper for passing LONG_VERSION to clap
fn pretty_build_info_static() -> &'static str {
&PRETTY_BUILD_INFORMATION
}
#[derive(Parser)]
#[clap(author = "Nymtech", version, long_version = pretty_build_info_static(), about)]
pub(crate) struct CliArgs {
/// Path pointing to an env file that configures the Payment Manager.
#[clap(short, long)]
pub(crate) config_env_file: Option<std::path::PathBuf>,
/// Mnemonic of the Nym account holding the funds to be converted after successful payments
#[clap(short, long)]
pub(crate) mnemonic: bip39::Mnemonic,
/// Path pointing to the SQLite database containing payment data.
#[clap(short, long)]
pub(crate) db_path: std::path::PathBuf,
}
+5 -2
View File
@@ -16,7 +16,10 @@ impl Clone for Client {
}
impl Client {
pub(crate) fn new(details: &NymNetworkDetails) -> Result<Self, Error> {
pub(crate) fn new(
details: &NymNetworkDetails,
mnemonic: &bip39::Mnemonic,
) -> Result<Self, Error> {
let client_config = nyxd::Config::try_from_nym_network_details(details)?;
let endpoint = details
.endpoints
@@ -28,7 +31,7 @@ impl Client {
let inner = DirectSigningHttpRpcNyxdClient::connect_with_mnemonic(
client_config,
endpoint,
"".parse().unwrap(),
mnemonic.clone(),
)?;
Ok(Client(Arc::new(RwLock::new(inner))))
+4 -4
View File
@@ -1,6 +1,7 @@
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use crate::cli::CliArgs;
use crate::client::Client;
use crate::state::{Config, State};
use crate::storage::Storage;
@@ -13,7 +14,6 @@ use rocket_okapi::okapi::openapi3::OpenApi;
use rocket_okapi::settings::OpenApiSettings;
use rocket_okapi::swagger_ui::make_swagger_ui;
use rocket_okapi::{mount_endpoints_and_merged_docs, openapi_get_routes_spec};
use std::path::PathBuf;
pub(crate) mod openapi;
pub(crate) mod routes;
@@ -40,10 +40,10 @@ fn setup_cors() -> Result<Cors> {
Ok(cors)
}
pub(crate) async fn setup_rocket() -> Result<Rocket<Ignite>> {
pub(crate) async fn setup_rocket(args: &CliArgs) -> Result<Rocket<Ignite>> {
let openapi_settings = rocket_okapi::settings::OpenApiSettings::default();
let mut rocket = rocket::build();
let storage = Storage::init(PathBuf::from("/tmp/payments/payments.db")).await?;
let storage = Storage::init(&args.db_path).await?;
mount_endpoints_and_merged_docs! {
rocket,
@@ -54,7 +54,7 @@ pub(crate) async fn setup_rocket() -> Result<Rocket<Ignite>> {
}
let details = NymNetworkDetails::new_from_env();
let client = Client::new(&details)?;
let client = Client::new(&details, &args.mnemonic)?;
let config = Config::new(details.chain_details.mix_denom.base.clone());
let rocket = rocket
.manage(State::new(storage, client, config))
+5 -2
View File
@@ -1,11 +1,13 @@
// Copyright 2023 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use clap::Parser;
use nym_bin_common::logging::setup_logging;
use nym_network_defaults::setup_env;
use nym_task::TaskManager;
use std::error::Error;
mod cli;
mod client;
mod error;
mod http;
@@ -15,10 +17,11 @@ mod storage;
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
setup_logging();
setup_env(None);
let args = cli::CliArgs::parse();
setup_env(args.config_env_file.as_ref());
// let's build our rocket!
let rocket = http::setup_rocket().await?;
let rocket = http::setup_rocket(&args).await?;
// setup shutdowns
let shutdown = TaskManager::new(10);