GeneIT/geneit_backend/src/app_config.rs

272 lines
7.3 KiB
Rust
Raw Normal View History

2023-05-24 12:38:18 +00:00
use clap::Parser;
2023-08-05 12:49:17 +00:00
use s3::creds::Credentials;
use s3::{Bucket, Region};
2023-05-24 12:38:18 +00:00
/// 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-08-07 12:53:44 +00:00
/// Secret key, used to sign some resources. Must be randomly generated
#[clap(long, env, default_value = "")]
secret: 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
2023-06-06 13:47:30 +00:00
#[clap(long, env, default_value = "APP_ORIGIN/reset_password#TOKEN")]
2023-05-30 13:12:58 +00:00
pub reset_password_url: String,
2023-06-06 07:47:52 +00:00
/// Delete account URL
2023-06-06 13:47:30 +00:00
#[clap(long, env, default_value = "APP_ORIGIN/delete_account#TOKEN")]
2023-06-06 07:47:52 +00:00
pub delete_account_url: String,
/// URL where the OpenID configuration can be found
2023-06-02 07:48:51 +00:00
#[arg(
long,
env,
default_value = "http://localhost:9001/.well-known/openid-configuration"
)]
pub oidc_configuration_url: String,
/// Disable OpenID authentication
#[arg(long, env)]
pub disable_oidc: bool,
/// OpenID provider name
#[arg(long, env, default_value = "3rd party provider")]
pub oidc_provider_name: String,
/// OpenID client ID
2023-06-02 07:48:51 +00:00
#[arg(long, env, default_value = "foo")]
pub oidc_client_id: String,
/// OpenID client secret
2023-06-02 07:48:51 +00:00
#[arg(long, env, default_value = "bar")]
pub oidc_client_secret: String,
2023-06-02 09:49:18 +00:00
/// OpenID login redirect URL
2023-06-06 13:47:30 +00:00
#[arg(long, env, default_value = "APP_ORIGIN/oidc_cb")]
oidc_redirect_url: String,
2023-08-05 12:49:17 +00:00
/// S3 Bucket name
#[arg(long, env, default_value = "geneit-data")]
s3_bucket_name: String,
/// S3 region (if not using Minio)
#[arg(long, env, default_value = "eu-central-1")]
s3_region: String,
/// S3 API endpoint
#[arg(long, env, default_value = "http://localhost:9000")]
s3_endpoint: String,
/// S3 access key
#[arg(long, env, default_value = "topsecret")]
s3_access_key: String,
/// S3 secret key
#[arg(long, env, default_value = "topsecret")]
s3_secret_key: String,
/// S3 skip auto create bucket if not existing
#[arg(long, env)]
pub s3_skip_auto_create_bucket: bool,
2023-08-05 17:15:52 +00:00
/// Directory where temporary files are stored
#[arg(long, env, default_value = "/tmp")]
pub temp_dir: 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
2023-08-07 12:53:44 +00:00
/// Get app secret
pub fn secret(&self) -> &str {
let mut secret = self.secret.as_str();
if cfg!(debug_assertions) && secret.is_empty() {
secret = "DEBUGKEYDEBUGKEYDEBUGKEYDEBUGKEYDEBUGKEY";
}
if secret.is_empty() {
panic!("SECRET is undefined or too short (min 30 chars)!")
}
secret
}
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 {
2023-06-06 13:47:30 +00:00
self.reset_password_url
.replace("APP_ORIGIN", &self.website_origin)
.replace("TOKEN", token)
2023-05-30 13:12:58 +00:00
}
2023-06-06 07:47:52 +00:00
/// Get account delete URL
pub fn get_account_delete_url(&self, token: &str) -> String {
2023-06-06 13:47:30 +00:00
self.delete_account_url
.replace("APP_ORIGIN", &self.website_origin)
.replace("TOKEN", token)
2023-06-06 07:47:52 +00:00
}
/// Get OpenID providers configuration
2023-06-02 09:49:18 +00:00
pub fn openid_providers(&self) -> Vec<OIDCProvider<'_>> {
if self.disable_oidc {
return vec![];
}
2023-06-02 09:49:18 +00:00
vec![OIDCProvider {
id: "first_prov",
client_id: self.oidc_client_id.as_str(),
client_secret: self.oidc_client_secret.as_str(),
configuration_url: self.oidc_configuration_url.as_str(),
name: self.oidc_provider_name.as_str(),
}]
}
2023-06-06 13:47:30 +00:00
/// Get OIDC callback URL
pub fn oidc_redirect_url(&self) -> String {
self.oidc_redirect_url
.replace("APP_ORIGIN", &self.website_origin)
}
2023-08-05 12:49:17 +00:00
/// Get s3 credentials
pub fn s3_credentials(&self) -> anyhow::Result<Credentials> {
Ok(Credentials::new(
Some(&self.s3_access_key),
Some(&self.s3_secret_key),
None,
None,
None,
)?)
}
/// Get S3 bucket
pub fn s3_bucket(&self) -> anyhow::Result<Bucket> {
Ok(Bucket::new(
&self.s3_bucket_name,
Region::Custom {
region: self.s3_region.to_string(),
endpoint: self.s3_endpoint.to_string(),
},
self.s3_credentials()?,
)?
.with_path_style())
}
}
#[derive(Debug, Clone, serde::Serialize)]
2023-06-02 09:49:18 +00:00
pub struct OIDCProvider<'a> {
pub id: &'a str,
#[serde(skip_serializing)]
2023-06-02 09:49:18 +00:00
pub client_id: &'a str,
#[serde(skip_serializing)]
2023-06-02 09:49:18 +00:00
pub client_secret: &'a str,
#[serde(skip_serializing)]
2023-06-02 09:49:18 +00:00
pub configuration_url: &'a str,
pub name: &'a str,
2023-05-24 14:19:46 +00:00
}