make listen address and port env

This commit is contained in:
Xander Bil 2024-11-12 20:01:34 +01:00
parent d6fa0cf59a
commit ac1c466866
No known key found for this signature in database
GPG key ID: EC9706B54A278598
2 changed files with 18 additions and 4 deletions

View file

@ -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::<u16>().expect("PORT is invalid"))
.unwrap_or(8080),
}
})
}

View file

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