BasicOIDC/src/data/app_config.rs

98 lines
2.4 KiB
Rust
Raw Normal View History

use std::path::{Path, PathBuf};
2022-03-30 08:14:39 +00:00
use clap::Parser;
use crate::constants::{APP_NAME, CLIENTS_LIST_FILE, USERS_LIST_FILE};
2022-03-30 08:14:39 +00:00
/// Basic OIDC provider
#[derive(Parser, Debug, Clone)]
2022-03-30 08:14:39 +00:00
#[clap(author, version, about, long_about = None)]
pub struct AppConfig {
2022-03-30 08:14:39 +00:00
/// Listen address
#[clap(short, long, env, default_value = "0.0.0.0:8000")]
pub listen_address: String,
/// Storage path
#[clap(short, long, env)]
pub storage_path: String,
2022-03-30 14:58:00 +00:00
/// App token token
#[clap(short, long, env, default_value = "")]
pub token_key: String,
/// Website origin
#[clap(short, long, env, default_value = "http://localhost:8000")]
pub website_origin: String,
/// Proxy IP, might end with a star "*"
#[clap(short, long, env)]
pub proxy_ip: Option<String>,
2022-11-12 16:01:45 +00:00
/// IP location service API
///
/// Up instance of IP location service : https://gitlab.com/pierre42100/iplocationserver
///
/// Example: "https://api.geoip.rs"
#[arg(long, short, env)]
pub ip_location_service: Option<String>,
}
lazy_static::lazy_static! {
static ref ARGS: AppConfig = {
let mut config = AppConfig::parse();
// In debug mode only, use dummy token
if cfg!(debug_assertions) && config.token_key.is_empty() {
config.token_key = String::from_utf8_lossy(&[32; 64]).to_string();
}
config
};
}
impl AppConfig {
2022-11-12 16:01:45 +00:00
/// Get parsed command line arguments
pub fn get() -> &'static AppConfig {
&ARGS
}
pub fn secure_cookie(&self) -> bool {
self.website_origin.starts_with("https:")
}
pub fn storage_path(&self) -> &Path {
self.storage_path.as_ref()
}
pub fn users_file(&self) -> PathBuf {
2022-03-30 08:14:39 +00:00
self.storage_path().join(USERS_LIST_FILE)
}
2022-04-06 15:18:06 +00:00
pub fn clients_file(&self) -> PathBuf {
self.storage_path().join(CLIENTS_LIST_FILE)
}
2022-04-08 16:53:57 +00:00
pub fn full_url(&self, uri: &str) -> String {
2022-04-08 16:54:22 +00:00
if uri.starts_with('/') {
2022-04-08 16:53:57 +00:00
format!("{}{}", self.website_origin, uri)
} else {
format!("{}/{}", self.website_origin, uri)
}
}
pub fn domain_name(&self) -> &str {
2022-04-19 09:03:10 +00:00
self.website_origin.split('/').nth(2).unwrap_or(APP_NAME)
}
2022-04-03 13:50:49 +00:00
}
2022-11-12 16:01:45 +00:00
#[cfg(test)]
mod test {
use crate::data::app_config::AppConfig;
#[test]
fn verify_cli() {
use clap::CommandFactory;
AppConfig::command().debug_assert()
}
}