Configurable port

This commit is contained in:
2026-10-06 08:24:08 +03:00
parent e68e9c2032
commit 58ca8ee1da
2 changed files with 13 additions and 4 deletions
+10 -3
View File
@@ -1,4 +1,4 @@
use std::{env, path::PathBuf}; use std::{env, num::ParseIntError, path::PathBuf};
use secrecy::SecretString; use secrecy::SecretString;
use thiserror::Error; use thiserror::Error;
@@ -6,12 +6,15 @@ use thiserror::Error;
pub struct Config { pub struct Config {
pub server_secret: SecretString, pub server_secret: SecretString,
pub state_dir: PathBuf, pub state_dir: PathBuf,
pub port: i32,
} }
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum ConfigError { pub enum ConfigError {
#[error("Missing SERVER_SECRET")] #[error("Missing SERVER_SECRET")]
MissingServerSecret, MissingServerSecret,
#[error("Parse error {0}")]
InvalidPort(#[from] ParseIntError),
#[error("Unknown error: #{0}")] #[error("Unknown error: #{0}")]
Unknown(anyhow::Error), Unknown(anyhow::Error),
} }
@@ -20,8 +23,12 @@ impl Config {
pub fn new() -> Result<Self, ConfigError> { pub fn new() -> Result<Self, ConfigError> {
dotenvy::dotenv().map_err(|err| ConfigError::Unknown(err.into()))?; dotenvy::dotenv().map_err(|err| ConfigError::Unknown(err.into()))?;
let server_secret = env::var("SERVER_SECRET").map(SecretString::from).map_err(|_| ConfigError::MissingServerSecret)?; let server_secret = env::var("SERVER_SECRET").map(SecretString::from).map_err(|_| ConfigError::MissingServerSecret)?;
let state_dir = env::var("RUKOSYNC_STATE_DIR").map_or_else(|_| PathBuf::from("/tmp/"), PathBuf::from); let state_dir = env::var("STATE_DIR").map_or_else(|_| PathBuf::from("/tmp/"), PathBuf::from);
Ok(Self{server_secret, state_dir}) let port = match env::var("PORT") {
Ok(p) => p.parse(),
Err(_) => Ok(3000),
}?;
Ok(Self{server_secret, state_dir, port})
} }
} }
+3 -1
View File
@@ -27,7 +27,9 @@ async fn main() -> anyhow::Result<()> {
.nest("/syncs", syncs::syncs_routes()) .nest("/syncs", syncs::syncs_routes())
.with_state(app_state) .with_state(app_state)
.layer(TraceLayer::new_for_http()); .layer(TraceLayer::new_for_http());
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?; let address = format!("0.0.0.0:{}", config.port);
tracing::info!("Listening on {:?}", address);
let listener = tokio::net::TcpListener::bind(address).await?;
axum::serve(listener, app).await?; axum::serve(listener, app).await?;
Ok(()) Ok(())
} }