Skip to content

Commit f3a3517

Browse files
gadomskiburleight
andauthored
feat: add bind argument when serving (#871)
## Closes - Closes #870 ## Description Thanks for the contribution @burleight! I cherry picked your commit and applied it as-is, since it works as expected. Do you need a release of the **rustac** crate (the cli) right away to use it? Happy to cut one if needed. --------- Co-authored-by: Tomas Burleigh <[email protected]>
1 parent d0f7405 commit f3a3517

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

crates/cli/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
99
### Added
1010

1111
- Add `parquet-max-row-group-size` parameter to commands that write parquet files with default value of `150_000` ([#846](https://github.com/stac-utils/rustac/pull/846))
12+
- `--bind` argument to `rustac serve` ([#871](https://github.com/stac-utils/rustac/pull/871))
1213

1314
## [0.1.2] - 2025-11-14
1415

crates/cli/src/lib.rs

Lines changed: 19 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,8 @@ 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)
463+
.await
455464
}
456465
#[cfg(not(feature = "pgstac"))]
457466
{
@@ -460,7 +469,8 @@ impl Rustac {
460469
} else {
461470
let backend = stac_server::MemoryBackend::new();
462471
eprintln!("Backend: memory");
463-
load_and_serve(addr, backend, collections, items, create_collections).await
472+
load_and_serve(bind, addr, backend, collections, items, create_collections)
473+
.await
464474
}
465475
}
466476
Command::Crawl {
@@ -678,6 +688,7 @@ impl FromStr for KeyValue {
678688
}
679689

680690
async fn load_and_serve(
691+
bind: &str,
681692
addr: &str,
682693
mut backend: impl Backend,
683694
collections: Vec<Collection>,
@@ -714,10 +725,13 @@ async fn load_and_serve(
714725
"items don't have a collection and `create_collections` is false"
715726
));
716727
}
717-
let root = format!("http://{addr}");
728+
729+
let root = Url::parse(addr)
730+
.map(|url| url.to_string())
731+
.unwrap_or(format!("http://{addr}"));
718732
let api = stac_server::Api::new(backend, &root)?;
719733
let router = stac_server::routes::from_api(api);
720-
let listener = TcpListener::bind(&addr).await?;
734+
let listener = TcpListener::bind(&bind).await?;
721735
eprintln!("Serving a STAC API at {root}");
722736
axum::serve(listener, router).await.map_err(Error::from)
723737
}

0 commit comments

Comments
 (0)