Skip to content

Commit b94a4a7

Browse files
Merge pull request #1 from m-misiura/ip_address_parsing
🚧 removing the bind address being hardcoded to `127.0.0.1`
2 parents 15526d2 + 69790be commit b94a4a7

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/main.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
use std::{env, net::SocketAddr};
2-
1+
use std::{env, net::{SocketAddr, IpAddr}};
32
use axum::{
43
routing::{get, post},
54
Router,
65
};
76
use tower_http::trace::{self, TraceLayer};
87
use tracing::Level;
9-
108
mod detectors;
11-
129
use detectors::handle_text_contents;
1310

1411
#[tokio::main]
@@ -17,7 +14,8 @@ async fn main() {
1714
.with_target(false)
1815
.compact()
1916
.init();
20-
17+
18+
// Get port from environment variable or use default
2119
let mut http_port = 8080;
2220
if let Ok(port) = env::var("HTTP_PORT") {
2321
match port.parse::<u16>() {
@@ -26,6 +24,9 @@ async fn main() {
2624
}
2725
}
2826

27+
// Get host from environment variable or use default
28+
let host = env::var("HOST").unwrap_or_else(|_| "0.0.0.0".to_string());
29+
2930
let app = Router::new()
3031
.route("/health", get(|| async { "Hello, World!" }))
3132
.route("/api/v1/text/contents", post(handle_text_contents))
@@ -35,9 +36,10 @@ async fn main() {
3536
.on_response(trace::DefaultOnResponse::new().level(Level::INFO)),
3637
);
3738

38-
let addr = SocketAddr::from(([127, 0, 0, 1], http_port));
39+
let ip: IpAddr = host.parse().expect("Failed to parse host IP address");
40+
let addr = SocketAddr::from((ip, http_port));
41+
3942
tracing::info!("listening on {}", addr);
40-
4143
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();
4244
axum::serve(listener, app).await.unwrap();
43-
}
45+
}

0 commit comments

Comments
 (0)