From 88f13d585a250457f72c350a737057ebecebc12c Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sun, 3 Sep 2023 16:40:06 +0100 Subject: [PATCH] Allow customizing the listen port and interface --- src/main.rs | 4 ++-- src/utils.rs | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index c5fdf8c..cd9636c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,7 @@ use async_std::path::PathBuf; use async_std::stream::StreamExt; use axum::extract::State; use axum::{routing::get, Router}; -use std::net::{IpAddr, Ipv6Addr, SocketAddr}; +use std::net::SocketAddr; use tokio::task::JoinSet; mod dokku; @@ -64,7 +64,7 @@ async fn main() { let app = Router::new().route("/hosts", get(hosts)).with_state(state); - let addr = &SocketAddr::new(IpAddr::from(Ipv6Addr::UNSPECIFIED), 3000); + let addr = &SocketAddr::new(utils::get_bind_host(), utils::get_port()); println!("Listening on {addr}"); axum::Server::bind(addr) .serve(app.into_make_service()) diff --git a/src/utils.rs b/src/utils.rs index 872c5e3..e692482 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,5 @@ use std::ffi::OsString; +use std::net::{IpAddr, Ipv6Addr}; #[inline] pub fn osstring_starts_with(data: OsString, prefix: char) -> bool { @@ -7,3 +8,17 @@ pub fn osstring_starts_with(data: OsString, prefix: char) -> bool { Err(_) => false, } } + +pub fn get_port() -> u16 { + match std::env::var("PORT").ok() { + Some(s) => s.parse().unwrap(), + None => 3000, + } +} + +pub fn get_bind_host() -> IpAddr { + match std::env::var("BIND").ok() { + Some(s) => s.parse().unwrap(), + None => IpAddr::V6(Ipv6Addr::UNSPECIFIED), + } +}