Skip to content

Commit 36800c7

Browse files
feat: add 'listen' config option (closes #31)
Signed-off-by: Henry <mail@henrygressmann.de>
1 parent 942ac9d commit 36800c7

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

Cargo.lock

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config.rs

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use figment::Figment;
33
use figment::providers::{Env, Format, Toml};
44
use http::Uri;
55
use serde::{Deserialize, Serialize};
6+
use std::net::SocketAddr;
67
use std::num::NonZeroU16;
78
use std::str::FromStr;
89

@@ -14,6 +15,15 @@ fn default_port() -> u16 {
1415
9042
1516
}
1617

18+
fn default_listen() -> ListenAddr {
19+
ListenAddr::Port(default_port())
20+
}
21+
22+
#[must_use]
23+
pub fn default_maxmind_edition() -> String {
24+
"GeoLite2-City".to_string()
25+
}
26+
1727
fn default_data_dir() -> String {
1828
#[cfg(target_family = "unix")]
1929
{
@@ -31,8 +41,8 @@ pub struct Config {
3141
#[serde(default = "default_base")]
3242
pub base_url: String,
3343

34-
#[serde(default = "default_port")]
35-
pub port: u16,
44+
#[serde(default = "default_listen", alias = "port")]
45+
pub listen: ListenAddr,
3646

3747
#[serde(default)]
3848
// don't load favicons from the duckduckgo api
@@ -48,16 +58,27 @@ pub struct Config {
4858
pub duckdb: DuckdbConfig,
4959
}
5060

51-
#[must_use]
52-
pub fn default_maxmind_edition() -> String {
53-
"GeoLite2-City".to_string()
61+
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
62+
#[serde(untagged)]
63+
pub enum ListenAddr {
64+
Port(u16),
65+
Addr(String),
66+
}
67+
68+
impl ListenAddr {
69+
pub fn addr(&self) -> String {
70+
match self {
71+
ListenAddr::Port(port) => SocketAddr::from(([0, 0, 0, 0], *port)).to_string(),
72+
ListenAddr::Addr(addr) => addr.clone(),
73+
}
74+
}
5475
}
5576

5677
impl Default for Config {
5778
fn default() -> Self {
5879
Self {
5980
base_url: default_base(),
60-
port: default_port(),
81+
listen: default_listen(),
6182
data_dir: default_data_dir(),
6283
geoip: Default::default(),
6384
duckdb: Default::default(),
@@ -187,7 +208,7 @@ mod test {
187208
assert_eq!(config.geoip.maxmind_db_path, Some("test".to_string()));
188209
assert_eq!(config.base_url, "http://localhost:8081");
189210
assert_eq!(config.data_dir, "./liwan-test-data");
190-
assert_eq!(config.port, 9042);
211+
assert_eq!(config.listen, ListenAddr::Port(9042));
191212
assert_eq!(config.duckdb.memory_limit, Some("2GB".to_string()));
192213
assert_eq!(config.duckdb.threads, Some(NonZeroU16::new(4).unwrap()));
193214
Ok(())
@@ -212,7 +233,7 @@ mod test {
212233
assert!(config.geoip.maxmind_license_key.is_none());
213234
assert_eq!(config.base_url, "http://localhost:8081");
214235
assert_eq!(config.data_dir, "./liwan-test-data");
215-
assert_eq!(config.port, 9042);
236+
assert_eq!(config.listen, ListenAddr::Port(9042));
216237
Ok(())
217238
});
218239
}
@@ -261,7 +282,7 @@ mod test {
261282
assert!(config.geoip.maxmind_account_id.is_none());
262283
assert!(config.geoip.maxmind_license_key.is_none());
263284
assert_eq!(config.base_url, "http://localhost:9042");
264-
assert_eq!(config.port, 9042);
285+
assert_eq!(config.listen, ListenAddr::Port(9042));
265286
Ok(())
266287
});
267288
}

src/web/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub async fn start_webserver(app: Arc<Liwan>, events: Sender<Event>) -> Result<(
122122
tracing::info!("To see all available commands, run `liwan --help`");
123123
}
124124
_ => {
125-
tracing::info!("Liwan is running on {}", app.config.base_url);
125+
tracing::info!("Liwan is running on {} ({})", app.config.base_url, app.config.listen.addr());
126126
}
127127
}
128128

@@ -131,7 +131,7 @@ pub async fn start_webserver(app: Arc<Liwan>, events: Sender<Event>) -> Result<(
131131
#[cfg(debug_assertions)]
132132
save_spec(router.1)?;
133133

134-
let listener = tokio::net::TcpListener::bind(("0.0.0.0", app.config.port)).await.unwrap();
134+
let listener = tokio::net::TcpListener::bind(app.config.listen.addr()).await.unwrap();
135135
let service = router.0.into_make_service_with_connect_info::<SocketAddr>();
136136
axum::serve(listener, service).await.context("server exited unexpectedly")
137137
}

0 commit comments

Comments
 (0)