Skip to content

Commit

Permalink
refactor: use Mesagisto Config Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
itsusinn committed Nov 15, 2021
1 parent 6c5e788 commit e0d3d9f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
10 changes: 6 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,26 @@ impl Config {
pub struct NatsConfig {
// pattern: "nats://{host}:{port}"
#[educe(Default = "nats://itsusinn.site:4222")]
pub address: String,
pub address: ArcStr,
}

#[basic_derive]
pub struct ProxyConfig {
#[educe(Default = false)]
pub enable: bool,
#[educe(Default = true)]
pub enable_for_mesagisto: bool,
// pattern: "http://{username}:{password}@{host}:{port}"
#[educe(Default = "http://127.0.0.1:7890")]
pub address: String,
pub address: ArcStr,
}

#[basic_derive]
pub struct CipherConfig {
#[educe(Default = false)]
#[educe(Default = true)]
pub enable: bool,
#[educe(Default = "this-is-an-example-key")]
pub key: String,
pub key: ArcStr,
#[educe(Default = true)]
pub refuse_plain: bool,
}
Expand Down
43 changes: 23 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
#![feature(backtrace, capture_disjoint_fields)]

use std::sync::Arc;

use arcstr::ArcStr;
use futures::FutureExt;
use mesagisto_client::{cache::CACHE, cipher::CIPHER, db::DB, res::RES, server::SERVER, OptionExt};
use mesagisto_client::MesagistoConfig;
use teloxide::{prelude::*, Bot};

use bot::TG_BOT;
Expand Down Expand Up @@ -57,25 +55,30 @@ async fn run() -> Result<(), anyhow::Error> {
log::warn!("若要启用,请修改配置文件。");
return Ok(());
}
CACHE.init();
if CONFIG.cipher.enable {
CIPHER.init(&CONFIG.cipher.key, &CONFIG.cipher.refuse_plain);
} else {
CIPHER.deinit();
}
MesagistoConfig::builder()
.name("tg")
.cipher_enable(CONFIG.cipher.enable)
.cipher_key(CONFIG.cipher.key.clone())
.cipher_refuse_plain(CONFIG.cipher.refuse_plain)
.nats_address(CONFIG.nats.address.clone())
.proxy(if CONFIG.proxy.enable_for_mesagisto {
Some(CONFIG.proxy.address.clone())
} else {
None
})
.photo_url_resolver(|id_pair| {
async {
let file = String::from_utf8_lossy(&id_pair.1);
let file_path = TG_BOT.get_file(file).await.unwrap().file_path;
Ok(TG_BOT.get_url_by_path(file_path))
}
.boxed()
})
.build()
.apply().await;

log::info!("Mesagisto-Bot is starting up");
log::info!("Mesagisto-Bot正在启动");
DB.init(ArcStr::from("tg").some());
RES.init().await;
RES.resolve_photo_url(|id_pair| {
async {
let file = String::from_utf8_lossy(&id_pair.1);
let file_path = TG_BOT.get_file(file).await.unwrap().file_path;
Ok(TG_BOT.get_url_by_path(file_path))
}
.boxed()
});
SERVER.init(&CONFIG.nats.address).await;

let bot = Bot::with_client(CONFIG.telegram.token.clone(), net::client_from_config()).auto_send();

Expand Down
2 changes: 1 addition & 1 deletion src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn client_from_config() -> reqwest::Client {
let builder = default_reqwest_settings().use_rustls_tls();
if CONFIG.proxy.enable {
builder
.proxy(reqwest::Proxy::all(&CONFIG.proxy.address).expect("reqwest::Proxy creation failed"))
.proxy(reqwest::Proxy::all(CONFIG.proxy.address.as_str()).expect("reqwest::Proxy creation failed"))
} else {
builder
}
Expand Down

0 comments on commit e0d3d9f

Please sign in to comment.