Skip to content

Commit 2da1123

Browse files
Pass server address as CLI argument
1 parent 4acda6f commit 2da1123

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/main.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use axum::{
2121
use serde::Serialize;
2222
use serde_json::Value;
2323
use std::{
24-
net::SocketAddr,
2524
sync::{Arc, Mutex},
2625
};
2726
use tower_http::services::ServeDir;
@@ -33,9 +32,6 @@ async fn main() {
3332
// TODO: Instrument handlers & DB code.
3433
tracing_subscriber::fmt::init();
3534

36-
// TODO: make this configurable at runtime
37-
let addr = SocketAddr::from(([127, 0, 0, 1], 3032));
38-
println!("listening on {}", addr);
3935

4036
let datastore = initialize_datastore().expect("Error initializing datastore");
4137

@@ -57,6 +53,9 @@ async fn main() {
5753
.nest("/files/", on_service(MethodFilter::GET, files_service))
5854
.layer(Extension(datastore));
5955

56+
let addr = listen_addr();
57+
println!("listening on {}", addr);
58+
6059
axum::Server::bind(&addr)
6160
.serve(app.into_make_service())
6261
.await
@@ -72,6 +71,19 @@ fn initialize_datastore() -> AppResult<DataStore> {
7271
Ok(Arc::new(Mutex::new(tiddlers)))
7372
}
7473

74+
/// Determine the network port to bind from the CLI (or use 127.0.0.1:3032 by
75+
/// default).
76+
fn listen_addr() -> std::net::SocketAddr {
77+
// TODO(nknight): Replace this with a propper CLI using something like seahorse
78+
use std::net::SocketAddr;
79+
use std::env::args;
80+
81+
if let Some(src) = args().nth(1) {
82+
src.parse::<SocketAddr>().expect(&format!("Couldn't parse a socket from {}", src))
83+
} else {
84+
SocketAddr::from(([127, 0, 0, 1], 3032)) }
85+
}
86+
7587
// -----------------------------------------------------------------------------------
7688
// Views
7789

0 commit comments

Comments
 (0)