98 lines
2.9 KiB
Rust
98 lines
2.9 KiB
Rust
use poise::serenity_prelude::prelude::TypeMapKey;
|
|
use serde::Deserialize;
|
|
use std::{env, fs::read_to_string, path::PathBuf};
|
|
use tool_tracing::tracing_kind::Tracing;
|
|
|
|
use crate::dotenv;
|
|
|
|
const BOT_NAME: &str = "BOT_NAME";
|
|
const BOT_TOKEN: &str = "BOT_TOKEN";
|
|
const BOT_PREFIX: &str = "BOT_PREFIX";
|
|
const BOT_ENV: &str = "ENV";
|
|
const BOT_PORT: &str = "PORT";
|
|
|
|
const PERSISTENCE_HOST: &str = "PERSISTENCE_HOST";
|
|
const PERSISTENCE_PORT: &str = "PERSISTENCE_PORT";
|
|
const PERSISTENCE_USER: &str = "PERSISTENCE_USER";
|
|
const PERSISTENCE_PASSWORD: &str = "PERSISTENCE_PASSWORD";
|
|
const PERSISTENCE_DATABASE: &str = "PERSISTENCE_DATABASE";
|
|
|
|
pub struct ConfigGlobal;
|
|
|
|
impl TypeMapKey for ConfigGlobal {
|
|
type Value = Config;
|
|
}
|
|
|
|
#[derive(Deserialize, Clone)]
|
|
pub struct Config {
|
|
pub bot_name: String,
|
|
pub env: String,
|
|
pub port: u16,
|
|
pub token: String,
|
|
pub prefix: String,
|
|
pub tracing: Vec<Tracing>,
|
|
pub persistence: PersistenceConfig,
|
|
}
|
|
|
|
// Clickhouse https://github.com/ranger-finance/clickhouse-pool/blob/master/examples/simple-clickhouse/src/main.rs
|
|
#[derive(Deserialize, Clone)]
|
|
pub struct PersistenceConfig {
|
|
pub host: String,
|
|
pub port: u16,
|
|
pub user: String,
|
|
pub password: String,
|
|
pub database: String,
|
|
}
|
|
|
|
pub fn parse_local_config() -> Config {
|
|
let mut d = PathBuf::from(env::current_dir().unwrap());
|
|
d.push("resources/config.toml");
|
|
dotenv::load_dot_env();
|
|
parse_config(d)
|
|
}
|
|
|
|
pub fn parse_config(path_buff: PathBuf) -> Config {
|
|
let config_file_string =
|
|
read_to_string(path_buff.clone().into_os_string().into_string().unwrap()).expect(&format!(
|
|
"Failed to read config file: {}",
|
|
path_buff.display()
|
|
));
|
|
let mut config: Config =
|
|
toml::from_str(&config_file_string).expect("Failed to parse config file");
|
|
override_config_with_env_vars(&mut config);
|
|
config
|
|
}
|
|
|
|
fn override_config_with_env_vars(config: &mut Config) {
|
|
if let Ok(bot_name) = env::var(BOT_NAME) {
|
|
config.bot_name = bot_name;
|
|
}
|
|
if let Ok(env) = env::var(BOT_ENV) {
|
|
config.env = env;
|
|
}
|
|
if let Ok(port) = env::var(BOT_PORT) {
|
|
config.port = port.parse().unwrap();
|
|
}
|
|
if let Ok(token) = env::var(BOT_TOKEN) {
|
|
config.token = token;
|
|
}
|
|
if let Ok(prefix) = env::var(BOT_PREFIX) {
|
|
config.prefix = prefix;
|
|
}
|
|
if let Ok(persistence_host) = env::var(PERSISTENCE_HOST) {
|
|
config.persistence.host = persistence_host;
|
|
}
|
|
if let Ok(persistence_port) = env::var(PERSISTENCE_PORT) {
|
|
config.persistence.port = persistence_port.parse().unwrap();
|
|
}
|
|
if let Ok(persistence_user) = env::var(PERSISTENCE_USER) {
|
|
config.persistence.user = persistence_user;
|
|
}
|
|
if let Ok(persistence_password) = env::var(PERSISTENCE_PASSWORD) {
|
|
config.persistence.password = persistence_password;
|
|
}
|
|
if let Ok(persistence_database) = env::var(PERSISTENCE_DATABASE) {
|
|
config.persistence.database = persistence_database;
|
|
}
|
|
}
|