Skip to content

Commit 407a49c

Browse files
authored
Directory cli fixes (payjoin#1157)
2 parents b97ffb7 + 44e74b0 commit 407a49c

3 files changed

Lines changed: 33 additions & 32 deletions

File tree

payjoin-directory/src/cli.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::env;
22
use std::path::PathBuf;
33

4-
use clap::{value_parser, Parser};
4+
use clap::Parser;
55

66
#[derive(Debug, Parser)]
77
#[command(
@@ -10,44 +10,30 @@ use clap::{value_parser, Parser};
1010
long_about = None,
1111
)]
1212
pub struct Cli {
13-
#[arg(
14-
long,
15-
short = 'p',
16-
env = "PJ_DIR_PORT",
17-
default_value = "8080",
18-
help = "The port to bind"
19-
)]
20-
pub port: u16, // TODO tokio_listener::ListenerAddressLFlag
13+
#[arg(long, short = 'p', env = "PJ_DIR_PORT", help = "The port to bind [default: 8080]")]
14+
pub port: Option<u16>, // TODO tokio_listener::ListenerAddressLFlag
2115

22-
#[arg(
23-
long,
24-
env = "PJ_METRIC_PORT",
25-
default_value = "9090",
26-
help = "The port to bind for prometheus metrics export"
27-
)]
28-
pub metrics_port: u16, // TODO tokio_listener::ListenerAddressLFlag
16+
#[arg(long, env = "PJ_METRIC_PORT", help = "The port to bind for prometheus metrics export")]
17+
pub metrics_port: Option<u16>, // TODO tokio_listener::ListenerAddressLFlag
2918

3019
#[arg(
3120
long,
3221
env = "PJ_DIR_TIMEOUT_SECS",
33-
default_value = "30",
34-
help = "The timeout for long polling operations"
22+
help = "The timeout for long polling operations [default: 30]"
3523
)]
36-
pub timeout: u64,
24+
pub timeout: Option<u64>,
3725

3826
#[arg(
3927
long = "storage-dir",
4028
env = "PJ_STORAGE_DIR",
4129
help = "A directory for writing mailbox data."
4230
)]
43-
pub storage_dir: PathBuf,
31+
pub storage_dir: Option<PathBuf>,
4432

4533
#[arg(
4634
long = "ohttp-keys",
4735
env = "PJ_OHTTP_KEY_DIR",
48-
help = "The ohttp key config file path",
49-
default_value = "ohttp_keys",
50-
value_parser = value_parser!(PathBuf)
36+
help = "The ohttp key config file path [default: ohttp_keys]"
5137
)]
52-
pub ohttp_keys: PathBuf,
38+
pub ohttp_keys: Option<PathBuf>,
5339
}

payjoin-directory/src/config.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use crate::cli::Cli;
1212

1313
#[derive(Debug, Clone, Deserialize)]
1414
pub struct Config {
15-
pub listen_addr: String, // TODO tokio_listener::ListenerAddressLFlag
16-
pub metrics_listen_addr: String, // TODO tokio_listener::ListenerAddressLFlag
15+
pub listen_addr: String, // TODO tokio_listener::ListenerAddressLFlag
16+
pub metrics_listen_addr: Option<String>, // TODO tokio_listener::ListenerAddressLFlag
1717
pub timeout: Duration,
1818
pub storage_dir: PathBuf,
1919
pub ohttp_keys: PathBuf, // TODO OhttpConfig struct with rotation params, etc
@@ -31,7 +31,7 @@ impl Config {
3131

3232
Ok(Config {
3333
listen_addr: built_config.get("listen_addr")?,
34-
metrics_listen_addr: built_config.get("metrics_listen_addr")?,
34+
metrics_listen_addr: built_config.get("metrics_listen_addr").ok(),
3535
timeout: Duration::from_secs(built_config.get("timeout")?),
3636
storage_dir: built_config.get("storage_dir")?,
3737
ohttp_keys: built_config.get("ohttp_keys")?,
@@ -41,12 +41,22 @@ impl Config {
4141

4242
fn add_defaults(config: Builder, cli: &Cli) -> Result<Builder, ConfigError> {
4343
config
44-
.set_override_option("listen_addr", Some(format!("[::]:{}", cli.port)))?
44+
.set_default("listen_addr", "[::]:8080")?
45+
.set_override_option("listen_addr", cli.port.map(|port| format!("[::]:{}", port)))?
46+
.set_default("metrics_listen_addr", Option::<String>::None)?
4547
.set_override_option(
4648
"metrics_listen_addr",
47-
Some(format!("localhost:{}", cli.metrics_port)),
49+
cli.metrics_port.map(|port| format!("localhost:{}", port)),
4850
)?
49-
.set_override_option("timeout", Some(cli.timeout))?
50-
.set_override_option("ohttp_keys", Some(cli.ohttp_keys.to_string_lossy().into_owned()))?
51-
.set_override_option("storage_dir", Some(cli.storage_dir.to_string_lossy().into_owned()))
51+
.set_default("timeout", Some(30))?
52+
.set_override_option("timeout", cli.timeout)?
53+
.set_default("ohttp_keys", "ohttp_keys")?
54+
.set_override_option(
55+
"ohttp_keys",
56+
cli.ohttp_keys.clone().map(|s| s.to_string_lossy().into_owned()),
57+
)?
58+
.set_override_option(
59+
"storage_dir",
60+
cli.storage_dir.clone().map(|s| s.to_string_lossy().into_owned()),
61+
)
5262
}

payjoin-directory/src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ async fn main() -> Result<(), BoxError> {
3232

3333
let service = Service::new(db, ohttp.into(), metrics);
3434

35+
if let Some(addr) = config.metrics_listen_addr {
36+
let metrics_listener = TcpListener::bind(addr).await?;
37+
service.serve_metrics_tcp(metrics_listener).await?;
38+
}
39+
3540
let listener = TcpListener::bind(config.listen_addr).await?;
3641
service.serve_tcp(listener).await
3742
}

0 commit comments

Comments
 (0)