improve environment variable parsing

This commit is contained in:
mos 2024-03-28 22:36:47 +01:00
parent b6249cf12f
commit 206aee7772
2 changed files with 6 additions and 11 deletions

View File

@ -29,12 +29,13 @@ async fn read_req(sock: &mut TcpStream) -> Result<bool, Box<dyn Error + Send + S
Ok(&buf[..REQ_METHOD.len()] == REQ_METHOD) Ok(&buf[..REQ_METHOD.len()] == REQ_METHOD)
} }
pub async fn serve(port: Option<i32>, tz: Option<String>) -> Result<(), Box<dyn Error>> { pub async fn serve(port: Option<String>, tz: Option<String>) -> Result<(), Box<dyn Error>> {
let addr = format!("127.0.0.1:{}", port.unwrap_or(PORT)); let port = port.and_then(|s| s.parse::<i32>().ok()).unwrap_or(PORT);
let listen = TcpListener::bind(&addr).await?;
let tz = tz.and_then(|s| s.parse::<Tz>().ok()).unwrap_or(CET); let tz = tz.and_then(|s| s.parse::<Tz>().ok()).unwrap_or(CET);
let addr = format!("127.0.0.1:{}", port);
let listen = TcpListener::bind(&addr).await?;
loop { loop {
let (mut sock, addr) = listen.accept().await?; let (mut sock, addr) = listen.accept().await?;

View File

@ -3,11 +3,5 @@ use std::error::Error;
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> { async fn main() -> Result<(), Box<dyn Error>> {
swabd::serve( swabd::serve(env::var("SWABD_PORT").ok(), env::var("SWABD_TZ").ok()).await
env::var("SWABD_PORT")
.map(|port| port.parse::<i32>().unwrap())
.ok(),
env::var("SWABD_TZ").ok(),
)
.await
} }