Skip to content

Commit f4c5e68

Browse files
committed
configurable bind address
fixes #37 (at least for constellation)
1 parent c75c06e commit f4c5e68

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

constellation/src/bin/main.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use anyhow::{bail, Result};
22
use clap::{Parser, ValueEnum};
33
use metrics_exporter_prometheus::PrometheusBuilder;
4+
use std::net::SocketAddr;
45
use std::num::NonZero;
56
use std::path::PathBuf;
67
use std::sync::{atomic::AtomicU32, Arc};
@@ -21,7 +22,14 @@ const MONITOR_INTERVAL: time::Duration = time::Duration::from_secs(15);
2122
#[derive(Parser, Debug)]
2223
#[command(version, about, long_about = None)]
2324
struct Args {
24-
#[arg(short, long)]
25+
/// constellation server's listen address
26+
#[arg(long)]
27+
#[clap(default_value = "0.0.0.0:6789")]
28+
bind: SocketAddr,
29+
/// metrics server's listen address
30+
#[arg(long)]
31+
#[clap(default_value = "0.0.0.0:8765")]
32+
bind_metrics: SocketAddr,
2533
/// Jetstream server to connect to (exclusive with --fixture). Provide either a wss:// URL, or a shorhand value:
2634
/// 'us-east-1', 'us-east-2', 'us-west-1', or 'us-west-2'
2735
#[arg(short, long)]
@@ -78,10 +86,21 @@ fn main() -> Result<()> {
7886
let stream = jetstream_url(&args.jetstream);
7987
println!("using jetstream server {stream:?}...",);
8088

89+
let bind = args.bind;
90+
let metrics_bind = args.bind_metrics;
91+
8192
let stay_alive = CancellationToken::new();
8293

8394
match args.backend {
84-
StorageBackend::Memory => run(MemStorage::new(), fixture, None, stream, stay_alive),
95+
StorageBackend::Memory => run(
96+
MemStorage::new(),
97+
fixture,
98+
None,
99+
stream,
100+
bind,
101+
metrics_bind,
102+
stay_alive,
103+
),
85104
#[cfg(feature = "rocks")]
86105
StorageBackend::Rocks => {
87106
let storage_dir = args.data.clone().unwrap_or("rocks.test".into());
@@ -96,7 +115,15 @@ fn main() -> Result<()> {
96115
rocks.start_backup(backup_dir, auto_backup, stay_alive.clone())?;
97116
}
98117
println!("rocks ready.");
99-
run(rocks, fixture, args.data, stream, stay_alive)
118+
run(
119+
rocks,
120+
fixture,
121+
args.data,
122+
stream,
123+
bind,
124+
metrics_bind,
125+
stay_alive,
126+
)
100127
}
101128
}
102129
}
@@ -106,6 +133,8 @@ fn run(
106133
fixture: Option<PathBuf>,
107134
data_dir: Option<PathBuf>,
108135
stream: String,
136+
bind: SocketAddr,
137+
metrics_bind: SocketAddr,
109138
stay_alive: CancellationToken,
110139
) -> Result<()> {
111140
ctrlc::set_handler({
@@ -150,8 +179,8 @@ fn run(
150179
.build()
151180
.expect("axum startup")
152181
.block_on(async {
153-
install_metrics_server()?;
154-
serve(readable, "0.0.0.0:6789", staying_alive).await
182+
install_metrics_server(metrics_bind)?;
183+
serve(readable, bind, staying_alive).await
155184
})
156185
.unwrap();
157186
stay_alive.drop_guard();
@@ -218,21 +247,16 @@ fn run(
218247
Ok(())
219248
}
220249

221-
fn install_metrics_server() -> Result<()> {
250+
fn install_metrics_server(metrics_bind: SocketAddr) -> Result<()> {
222251
println!("installing metrics server...");
223-
let host = [0, 0, 0, 0];
224-
let port = 8765;
225252
PrometheusBuilder::new()
226253
.set_quantiles(&[0.5, 0.9, 0.99, 1.0])?
227254
.set_bucket_duration(time::Duration::from_secs(30))?
228255
.set_bucket_count(NonZero::new(10).unwrap()) // count * duration = 5 mins. stuff doesn't happen that fast here.
229256
.set_enable_unit_suffix(true)
230-
.with_http_listener((host, port))
257+
.with_http_listener(metrics_bind)
231258
.install()?;
232-
println!(
233-
"metrics server installed! listening on http://{}.{}.{}.{}:{port}",
234-
host[0], host[1], host[2], host[3]
235-
);
259+
println!("metrics server installed! listening at {metrics_bind:?}");
236260
Ok(())
237261
}
238262

0 commit comments

Comments
 (0)