feat: Init Config
This commit is contained in:
parent
b3a0187c84
commit
2044a86e61
61
Cargo.lock
generated
61
Cargo.lock
generated
@ -216,7 +216,9 @@ dependencies = [
|
|||||||
"poise",
|
"poise",
|
||||||
"serde",
|
"serde",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
"toml",
|
||||||
"tool_tracing",
|
"tool_tracing",
|
||||||
|
"tracing",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@ -1894,6 +1896,15 @@ dependencies = [
|
|||||||
"syn 2.0.101",
|
"syn 2.0.101",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "serde_spanned"
|
||||||
|
version = "0.6.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "87607cb1398ed59d48732e575a4c28a7a8ebf2454b964fe3f224f2afc07909e1"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_urlencoded"
|
name = "serde_urlencoded"
|
||||||
version = "0.7.1"
|
version = "0.7.1"
|
||||||
@ -2290,6 +2301,47 @@ dependencies = [
|
|||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toml"
|
||||||
|
version = "0.8.22"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "05ae329d1f08c4d17a59bed7ff5b5a769d062e64a62d34a3261b219e62cd5aae"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
"serde_spanned",
|
||||||
|
"toml_datetime",
|
||||||
|
"toml_edit",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toml_datetime"
|
||||||
|
version = "0.6.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3da5db5a963e24bc68be8b17b6fa82814bb22ee8660f192bb182771d498f09a3"
|
||||||
|
dependencies = [
|
||||||
|
"serde",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toml_edit"
|
||||||
|
version = "0.22.26"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "310068873db2c5b3e7659d2cc35d21855dbafa50d1ce336397c666e3cb08137e"
|
||||||
|
dependencies = [
|
||||||
|
"indexmap 2.9.0",
|
||||||
|
"serde",
|
||||||
|
"serde_spanned",
|
||||||
|
"toml_datetime",
|
||||||
|
"toml_write",
|
||||||
|
"winnow",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "toml_write"
|
||||||
|
version = "0.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bfb942dfe1d8e29a7ee7fcbde5bd2b9a25fb89aa70caea2eba3bee836ff41076"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tonic"
|
name = "tonic"
|
||||||
version = "0.12.3"
|
version = "0.12.3"
|
||||||
@ -3108,6 +3160,15 @@ version = "0.53.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486"
|
checksum = "271414315aff87387382ec3d271b52d7ae78726f5d44ac98b4f4030c91880486"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "winnow"
|
||||||
|
version = "0.7.10"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "c06928c8748d81b05c9be96aad92e1b6ff01833332f281e8cfca3be4b35fc9ec"
|
||||||
|
dependencies = [
|
||||||
|
"memchr",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winreg"
|
name = "winreg"
|
||||||
version = "0.50.0"
|
version = "0.50.0"
|
||||||
|
@ -8,6 +8,8 @@ edition = "2021"
|
|||||||
poise = { workspace = true }
|
poise = { workspace = true }
|
||||||
tokio = { workspace = true }
|
tokio = { workspace = true }
|
||||||
serde = { workspace = true }
|
serde = { workspace = true }
|
||||||
|
tracing = { workspace = true }
|
||||||
tool_tracing = { path = "../../libs/tool_tracing" }
|
tool_tracing = { path = "../../libs/tool_tracing" }
|
||||||
|
toml = "0.8"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
@ -1,7 +1,22 @@
|
|||||||
use poise::serenity_prelude::prelude::TypeMapKey;
|
use poise::serenity_prelude::prelude::TypeMapKey;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
use std::{env, fs::read_to_string, path::PathBuf};
|
||||||
use tool_tracing::tracing_kind::Tracing;
|
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;
|
pub struct ConfigGlobal;
|
||||||
|
|
||||||
impl TypeMapKey for ConfigGlobal {
|
impl TypeMapKey for ConfigGlobal {
|
||||||
@ -16,13 +31,67 @@ pub struct Config {
|
|||||||
pub token: String,
|
pub token: String,
|
||||||
pub prefix: String,
|
pub prefix: String,
|
||||||
pub tracing: Vec<Tracing>,
|
pub tracing: Vec<Tracing>,
|
||||||
|
pub persistence: PersistenceConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clickhouse https://github.com/ranger-finance/clickhouse-pool/blob/master/examples/simple-clickhouse/src/main.rs
|
// Clickhouse https://github.com/ranger-finance/clickhouse-pool/blob/master/examples/simple-clickhouse/src/main.rs
|
||||||
#[derive(Deserialize, Clone)]
|
#[derive(Deserialize, Clone)]
|
||||||
pub struct PersistenceConfig {
|
pub struct PersistenceConfig {
|
||||||
pub url: String,
|
pub host: String,
|
||||||
|
pub port: u16,
|
||||||
pub user: String,
|
pub user: String,
|
||||||
pub password: String,
|
pub password: String,
|
||||||
pub database: 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,9 +1,20 @@
|
|||||||
use std::{env::set_var, io::Read};
|
use std::{
|
||||||
|
env::{self, set_var},
|
||||||
|
io::Read,
|
||||||
|
path::PathBuf,
|
||||||
|
};
|
||||||
|
|
||||||
|
use tracing::info;
|
||||||
|
|
||||||
// Load environment variables from a .env file without external dependencies
|
// Load environment variables from a .env file without external dependencies
|
||||||
pub fn load_dot_env() {
|
pub fn load_dot_env() {
|
||||||
if let Ok(mut file) = std::fs::File::open(".env") {
|
let mut d = PathBuf::from(env::current_dir().unwrap());
|
||||||
|
d.push(".env");
|
||||||
|
if !d.exists() {
|
||||||
|
info!("No .env file found");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if let Ok(mut file) = std::fs::File::open(d) {
|
||||||
let mut contents = String::new();
|
let mut contents = String::new();
|
||||||
if file.read_to_string(&mut contents).is_ok() {
|
if file.read_to_string(&mut contents).is_ok() {
|
||||||
for line in contents.lines() {
|
for line in contents.lines() {
|
||||||
@ -15,5 +26,8 @@ pub fn load_dot_env() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
info!("Loaded .env file");
|
||||||
|
} else {
|
||||||
|
info!("Failed to open .env file");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
use config::parse_local_config;
|
||||||
use poise::serenity_prelude as serenity;
|
use poise::serenity_prelude as serenity;
|
||||||
|
use tracing::info;
|
||||||
|
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod dotenv;
|
pub mod dotenv;
|
||||||
@ -21,9 +23,11 @@ async fn age(
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
dotenv::load_dot_env(); // Load environment variables from .env file
|
let config = parse_local_config();
|
||||||
let token = std::env::var("DISCORD_TOKEN").expect("missing DISCORD_TOKEN");
|
|
||||||
print!("Starting bot with token: {}", token);
|
tool_tracing::init::init_tracing(config.tracing.clone(), config.bot_name.clone());
|
||||||
|
|
||||||
|
info!("Starting bot {}", config.bot_name);
|
||||||
let intents = serenity::GatewayIntents::non_privileged();
|
let intents = serenity::GatewayIntents::non_privileged();
|
||||||
|
|
||||||
let framework = poise::Framework::builder()
|
let framework = poise::Framework::builder()
|
||||||
@ -39,7 +43,7 @@ async fn main() {
|
|||||||
})
|
})
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
let client = serenity::ClientBuilder::new(token, intents)
|
let client = serenity::ClientBuilder::new(config.token.clone(), intents)
|
||||||
.framework(framework)
|
.framework(framework)
|
||||||
.await;
|
.await;
|
||||||
client.unwrap().start().await.unwrap();
|
client.unwrap().start().await.unwrap();
|
||||||
|
39
cog.toml
Normal file
39
cog.toml
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
from_latest_tag = false
|
||||||
|
ignore_merge_commits = false
|
||||||
|
disable_changelog = false
|
||||||
|
disable_bump_commit = false
|
||||||
|
generate_mono_repository_global_tag = true
|
||||||
|
branch_whitelist = []
|
||||||
|
skip_ci = "[skip ci]"
|
||||||
|
skip_untracked = false
|
||||||
|
pre_bump_hooks = []
|
||||||
|
post_bump_hooks = []
|
||||||
|
pre_package_bump_hooks = []
|
||||||
|
post_package_bump_hooks = []
|
||||||
|
|
||||||
|
[git_hooks]
|
||||||
|
|
||||||
|
[git_hooks.commit-msg]
|
||||||
|
script = """#!/bin/sh
|
||||||
|
set -e
|
||||||
|
cog verify --file $1
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
[git_hooks.pre-commit]
|
||||||
|
script = """#!/bin/sh
|
||||||
|
set -e
|
||||||
|
git add .
|
||||||
|
gitleaks git -v
|
||||||
|
gitleaks git --pre-commit --redact --staged --verbose
|
||||||
|
"""
|
||||||
|
|
||||||
|
[commit_types]
|
||||||
|
|
||||||
|
[changelog]
|
||||||
|
path = "CHANGELOG.md"
|
||||||
|
authors = []
|
||||||
|
|
||||||
|
[bump_profiles]
|
||||||
|
|
||||||
|
[packages]
|
28
resources/config.toml
Normal file
28
resources/config.toml
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
bot_name = "WeeKit"
|
||||||
|
env = "dev-che"
|
||||||
|
port = 5437
|
||||||
|
token = ""
|
||||||
|
prefix = "!"
|
||||||
|
|
||||||
|
[persistence]
|
||||||
|
host = "localhost"
|
||||||
|
port = 9000
|
||||||
|
user = "default"
|
||||||
|
password = "default"
|
||||||
|
database = "default"
|
||||||
|
|
||||||
|
|
||||||
|
[[tracing]]
|
||||||
|
kind = "Console"
|
||||||
|
name = "console"
|
||||||
|
level = 2
|
||||||
|
|
||||||
|
[tracing.additional]
|
||||||
|
|
||||||
|
[[tracing]]
|
||||||
|
kind = "Otel"
|
||||||
|
name = "otel"
|
||||||
|
level = 2
|
||||||
|
|
||||||
|
[tracing.additional]
|
||||||
|
endpoint = "http://localhost:4317"
|
Loading…
Reference in New Issue
Block a user