1
Fork 0

Allow customizing the listen port and interface

This commit is contained in:
Jake Howard 2023-09-03 16:40:06 +01:00
parent 1d7b09f92a
commit 88f13d585a
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 17 additions and 2 deletions

View file

@ -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())

View file

@ -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),
}
}