From ac1c466866732df23a7819a4bc24cefc76ad58d7 Mon Sep 17 00:00:00 2001 From: Xander Bil Date: Tue, 12 Nov 2024 20:01:34 +0100 Subject: [PATCH] make listen address and port env --- src/config.rs | 11 ++++++++++- src/main.rs | 11 ++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/config.rs b/src/config.rs index 5f15076..706f358 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,4 @@ -use std::{env, sync::OnceLock}; +use std::{env, net::IpAddr, sync::OnceLock}; use axum_extra::extract::cookie::Key; use dotenvy::dotenv; @@ -13,6 +13,8 @@ pub struct Config { pub zauth_client_secret: String, pub database_uri: String, pub cookies_key: Key, + pub listen_address: IpAddr, + pub port: u16, } impl Config { @@ -39,6 +41,13 @@ impl Config { .expect("COOKIES_KEY not present") .as_ref(), ), + listen_address: env::var("LISTEN_ADDRESS") + .unwrap_or("127.0.0.1".to_string()) + .parse() + .expect("LISTEN_ADDRESS is invalid"), + port: env::var("PORT") + .map(|v| v.parse::().expect("PORT is invalid")) + .unwrap_or(8080), } }) } diff --git a/src/main.rs b/src/main.rs index 60b7333..0d07225 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,8 @@ mod error; mod models; mod routes; +use std::net::SocketAddr; + use appstate::Appstate; use auth::{callback, login}; use axum::{ @@ -68,8 +70,11 @@ async fn main() { .with_state(state); // run our app with hyper, listening globally on port 3000 - let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") - .await - .unwrap(); + let listener = tokio::net::TcpListener::bind(SocketAddr::from(( + Config::get().listen_address, + Config::get().port, + ))) + .await + .unwrap(); axum::serve(listener, app).await.unwrap(); }