GeneIT/geneit_backend/src/app_config.rs

129 lines
3.3 KiB
Rust
Raw Normal View History

2023-05-24 12:38:18 +00:00
use clap::Parser;
/// GeneIT backend API
#[derive(Parser, Debug, Clone)]
#[clap(author, version, about, long_about = None)]
pub struct AppConfig {
/// Listen address
#[clap(short, long, env, default_value = "0.0.0.0:8000")]
pub listen_address: String,
/// Website origin
#[clap(short, long, env, default_value = "http://localhost:3000")]
pub website_origin: String,
/// Proxy IP, might end with a star "*"
#[clap(short, long, env)]
pub proxy_ip: Option<String>,
2023-05-24 14:19:46 +00:00
/// PostgreSQL database host
#[clap(long, env, default_value = "localhost")]
db_host: String,
/// PostgreSQL database port
#[clap(long, env, default_value_t = 5432)]
db_port: u16,
2023-05-24 12:38:18 +00:00
/// PostgreSQL username
#[clap(long, env, default_value = "user")]
db_username: String,
/// PostgreSQL password
2023-05-24 14:19:46 +00:00
#[clap(long, env, default_value = "pass")]
2023-05-24 12:38:18 +00:00
db_password: String,
2023-05-24 14:19:46 +00:00
/// PostgreSQL database name
#[clap(long, env, default_value = "geneit")]
db_name: String,
2023-05-26 15:55:19 +00:00
/// Redis connection hostname
#[clap(long, env, default_value = "localhost")]
redis_hostname: String,
/// Redis connection port
#[clap(long, env, default_value_t = 6379)]
redis_port: u16,
/// Redis database number
#[clap(long, env, default_value_t = 0)]
redis_db_number: i64,
/// Redis username
#[clap(long, env)]
redis_username: Option<String>,
/// Redis password
#[clap(long, env, default_value = "secretredis")]
redis_password: String,
2023-05-30 13:12:58 +00:00
/// Mail sender
#[clap(long, env, default_value = "geneit@example.com")]
pub mail_sender: String,
/// SMTP relay
#[clap(long, env, default_value = "localhost")]
pub smtp_relay: String,
/// SMTP port
#[clap(long, env, default_value_t = 1025)]
pub smtp_port: u16,
/// SMTP use TLS to connect to relay
#[clap(long, env)]
pub smtp_tls: bool,
/// SMTP username
#[clap(long, env)]
pub smtp_username: Option<String>,
/// SMTP password
#[clap(long, env)]
pub smtp_password: Option<String>,
/// Password reset URL
#[clap(
long,
env,
default_value = "http://localhost:3000/reset_password#TOKEN"
)]
pub reset_password_url: String,
2023-05-24 12:38:18 +00:00
}
lazy_static::lazy_static! {
static ref ARGS: AppConfig = {
AppConfig::parse()
};
}
impl AppConfig {
/// Get parsed command line arguments
pub fn get() -> &'static AppConfig {
&ARGS
}
2023-05-24 14:19:46 +00:00
/// Get full db connection chain
pub fn db_connection_chain(&self) -> String {
format!(
"postgres://{}:{}@{}:{}/{}",
self.db_username, self.db_password, self.db_host, self.db_port, self.db_name
)
}
2023-05-26 15:55:19 +00:00
/// Get Redis connection configuration
pub fn redis_connection_config(&self) -> redis::ConnectionInfo {
redis::ConnectionInfo {
addr: redis::ConnectionAddr::Tcp(self.redis_hostname.clone(), self.redis_port),
redis: redis::RedisConnectionInfo {
db: self.redis_db_number,
username: self.redis_username.clone(),
password: Some(self.redis_password.clone()),
},
}
}
2023-05-30 13:12:58 +00:00
/// Get password reset URL
pub fn get_password_reset_url(&self, token: &str) -> String {
self.reset_password_url.replace("TOKEN", token)
}
2023-05-24 14:19:46 +00:00
}