Skip to content

Commit 7447b8d

Browse files
committed
Added bind argument to the serve subcommand. This allows the application to separate the bind address from the root address for deployment behind a reverse proxy.
1 parent d0f7405 commit 7447b8d

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

crates/cli/src/lib.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,15 @@ pub enum Command {
232232
/// The hrefs of collections, items, and item collections to load into the API on startup.
233233
hrefs: Vec<String>,
234234

235-
/// The address of the server.
235+
/// The address of the server. Defaults to `127.0.0.1:7822`.
236+
/// Either a URL `https://some-host.io/stac` or a local address like `127.0.0.1:7822`.
236237
#[arg(short = 'a', long = "addr", default_value = "127.0.0.1:7822")]
237238
addr: String,
238239

240+
/// The address to bind the server to, if different from `--addr`.
241+
#[arg(short = 'b', long = "bind")]
242+
bind: Option<String>,
243+
239244
/// The pgstac connection string, e.g. `postgresql://username:password@localhost:5432/postgis`
240245
///
241246
/// If not provided an in-process memory backend will be used.
@@ -385,17 +390,20 @@ impl Rustac {
385390
Command::Serve {
386391
ref hrefs,
387392
ref addr,
393+
ref bind,
388394
ref pgstac,
389395
use_duckdb,
390396
load_collection_items,
391397
create_collections,
392398
} => {
399+
let bind = bind.as_deref().unwrap_or(&addr);
393400
if matches!(use_duckdb, Some(true))
394401
|| (use_duckdb.is_none() && hrefs.len() == 1 && hrefs[0].ends_with("parquet"))
395402
{
396403
let backend = stac_server::DuckdbBackend::new(&hrefs[0]).await?;
397404
eprintln!("Backend: duckdb");
398405
return load_and_serve(
406+
bind,
399407
addr,
400408
backend,
401409
Vec::new(),
@@ -451,7 +459,7 @@ impl Rustac {
451459
let backend =
452460
stac_server::PgstacBackend::new_from_stringlike(pgstac).await?;
453461
eprintln!("Backend: pgstac");
454-
load_and_serve(addr, backend, collections, items, create_collections).await
462+
load_and_serve(bind, addr, backend, collections, items, create_collections).await
455463
}
456464
#[cfg(not(feature = "pgstac"))]
457465
{
@@ -460,7 +468,7 @@ impl Rustac {
460468
} else {
461469
let backend = stac_server::MemoryBackend::new();
462470
eprintln!("Backend: memory");
463-
load_and_serve(addr, backend, collections, items, create_collections).await
471+
load_and_serve(bind, addr, backend, collections, items, create_collections).await
464472
}
465473
}
466474
Command::Crawl {
@@ -678,6 +686,7 @@ impl FromStr for KeyValue {
678686
}
679687

680688
async fn load_and_serve(
689+
bind: &str,
681690
addr: &str,
682691
mut backend: impl Backend,
683692
collections: Vec<Collection>,
@@ -714,10 +723,11 @@ async fn load_and_serve(
714723
"items don't have a collection and `create_collections` is false"
715724
));
716725
}
717-
let root = format!("http://{addr}");
726+
727+
let root = Url::parse(addr).map(|url| url.to_string()).unwrap_or(format!("http://{addr}"));
718728
let api = stac_server::Api::new(backend, &root)?;
719729
let router = stac_server::routes::from_api(api);
720-
let listener = TcpListener::bind(&addr).await?;
730+
let listener = TcpListener::bind(&bind).await?;
721731
eprintln!("Serving a STAC API at {root}");
722732
axum::serve(listener, router).await.map_err(Error::from)
723733
}

0 commit comments

Comments
 (0)