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 axum_extra::extract::cookie::Key;
use dotenvy::dotenv; use dotenvy::dotenv;
@ -13,6 +13,8 @@ pub struct Config {
pub zauth_client_secret: String, pub zauth_client_secret: String,
pub database_uri: String, pub database_uri: String,
pub cookies_key: Key, pub cookies_key: Key,
pub listen_address: IpAddr,
pub port: u16,
} }
impl Config { impl Config {
@ -39,6 +41,13 @@ impl Config {
.expect("COOKIES_KEY not present") .expect("COOKIES_KEY not present")
.as_ref(), .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 models;
mod routes; mod routes;
use std::net::SocketAddr;
use appstate::Appstate; use appstate::Appstate;
use auth::{callback, login}; use auth::{callback, login};
use axum::{ use axum::{
@ -68,7 +70,10 @@ async fn main() {
.with_state(state); .with_state(state);
// run our app with hyper, listening globally on port 3000 // run our app with hyper, listening globally on port 3000
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000") let listener = tokio::net::TcpListener::bind(SocketAddr::from((
Config::get().listen_address,
Config::get().port,
)))
.await .await
.unwrap(); .unwrap();
axum::serve(listener, app).await.unwrap(); axum::serve(listener, app).await.unwrap();