2022-03-29 17:32:31 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2022-03-30 08:14:39 +00:00
|
|
|
|
|
|
|
use clap::Parser;
|
|
|
|
|
2022-04-19 07:56:51 +00:00
|
|
|
use crate::constants::{APP_NAME, CLIENTS_LIST_FILE, USERS_LIST_FILE};
|
2022-03-29 17:32:31 +00:00
|
|
|
|
2022-03-30 08:14:39 +00:00
|
|
|
/// Basic OIDC provider
|
2022-04-03 13:48:45 +00:00
|
|
|
#[derive(Parser, Debug, Clone)]
|
2022-03-30 08:14:39 +00:00
|
|
|
#[clap(author, version, about, long_about = None)]
|
2022-03-29 17:32:31 +00:00
|
|
|
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,
|
|
|
|
|
2022-04-03 13:48:45 +00:00
|
|
|
/// Website origin
|
|
|
|
#[clap(short, long, env, default_value = "http://localhost:8000")]
|
|
|
|
pub website_origin: String,
|
2022-04-03 15:33:01 +00:00
|
|
|
|
|
|
|
/// Proxy IP, might end with a star "*"
|
|
|
|
#[clap(short, long, env)]
|
|
|
|
pub proxy_ip: Option<String>,
|
2022-03-29 17:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AppConfig {
|
2022-04-03 13:48:45 +00:00
|
|
|
pub fn secure_cookie(&self) -> bool {
|
|
|
|
self.website_origin.starts_with("https:")
|
|
|
|
}
|
|
|
|
|
2022-03-29 17:32:31 +00:00
|
|
|
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-03-29 17:32:31 +00:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
2022-04-19 07:56:51 +00:00
|
|
|
|
|
|
|
pub fn domain_name(&self) -> &str {
|
|
|
|
self.website_origin.split('/').skip(2).next().unwrap_or(APP_NAME)
|
|
|
|
}
|
2022-04-03 13:50:49 +00:00
|
|
|
}
|