make utility function to parse env
This commit is contained in:
parent
caa4bc1a91
commit
0082d1feb5
2 changed files with 20 additions and 36 deletions
|
|
@ -1,9 +1,6 @@
|
||||||
use anyhow;
|
use anyhow;
|
||||||
use rust_i18n::t;
|
use rust_i18n::t;
|
||||||
use std::env;
|
|
||||||
use std::fmt;
|
|
||||||
use std::str;
|
use std::str;
|
||||||
use std::str::FromStr;
|
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use teloxide::dispatching::{dialogue, dialogue::InMemStorage, UpdateHandler};
|
use teloxide::dispatching::{dialogue, dialogue::InMemStorage, UpdateHandler};
|
||||||
use teloxide::types::{Me, MessageKind, MessageNewChatMembers, UpdateKind};
|
use teloxide::types::{Me, MessageKind, MessageNewChatMembers, UpdateKind};
|
||||||
|
|
@ -13,30 +10,20 @@ use tracing::{event, Level};
|
||||||
use super::start::handle_new_chat_member;
|
use super::start::handle_new_chat_member;
|
||||||
use super::types::*;
|
use super::types::*;
|
||||||
use crate::db::DbPool;
|
use crate::db::DbPool;
|
||||||
|
use crate::util::{unwrap_env, parse_env};
|
||||||
|
|
||||||
use super::dl::cmd_download;
|
use super::dl::cmd_download;
|
||||||
use super::op::cmd_op;
|
use super::op::cmd_op;
|
||||||
use super::request::{cmd_listrequests, cmd_request, cmd_approve, cmd_decline};
|
use super::request::{cmd_approve, cmd_decline, cmd_listrequests, cmd_request};
|
||||||
use super::request_chat::{cmd_listrequests_chat, cmd_request_chat, cmd_approve_chat, cmd_decline_chat};
|
use super::request_chat::{
|
||||||
|
cmd_approve_chat, cmd_decline_chat, cmd_listrequests_chat, cmd_request_chat,
|
||||||
|
};
|
||||||
use super::start::{cmd_start, handle_my_chat_member};
|
use super::start::{cmd_start, handle_my_chat_member};
|
||||||
|
|
||||||
fn parse_env<T>(name: &str) -> T
|
|
||||||
where
|
|
||||||
T: FromStr,
|
|
||||||
T::Err: fmt::Debug,
|
|
||||||
{
|
|
||||||
str::parse(
|
|
||||||
env::var(name)
|
|
||||||
.expect(format!("env '{}' variable not defined", name).as_str())
|
|
||||||
.as_str(),
|
|
||||||
)
|
|
||||||
.expect(format!("env '{}' parse error", name).as_str())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn bot_main(db: DbPool) -> anyhow::Result<()> {
|
pub async fn bot_main(db: DbPool) -> anyhow::Result<()> {
|
||||||
event!(Level::INFO, "start");
|
event!(Level::INFO, "start");
|
||||||
|
|
||||||
let bot = Bot::new(env::var("BOT_TOKEN")?);
|
let bot = Bot::new(unwrap_env("BOT_TOKEN"));
|
||||||
let listener = Polling::builder(bot.clone())
|
let listener = Polling::builder(bot.clone())
|
||||||
.timeout(Duration::from_secs(parse_env("POLLING_TIMEOUT")))
|
.timeout(Duration::from_secs(parse_env("POLLING_TIMEOUT")))
|
||||||
.limit(parse_env("POLLING_LIMIT"))
|
.limit(parse_env("POLLING_LIMIT"))
|
||||||
|
|
@ -121,7 +108,7 @@ enum Command {
|
||||||
Download(String),
|
Download(String),
|
||||||
#[command(alias = "op")]
|
#[command(alias = "op")]
|
||||||
OP,
|
OP,
|
||||||
|
|
||||||
#[command(alias = "request")]
|
#[command(alias = "request")]
|
||||||
Request(String),
|
Request(String),
|
||||||
#[command(alias = "listrequests")]
|
#[command(alias = "listrequests")]
|
||||||
|
|
|
||||||
29
src/util.rs
29
src/util.rs
|
|
@ -1,18 +1,15 @@
|
||||||
use std::path::Path;
|
use std::env;
|
||||||
|
use std::fmt;
|
||||||
|
use std::str::FromStr;
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
pub fn unwrap_env(name: &str) -> String {
|
||||||
pub const VAR_LIB: &str = "target/debug";
|
env::var(name).expect(format!("env '{}' variable not defined", name).as_str())
|
||||||
|
}
|
||||||
#[cfg(not(debug_assertions))]
|
|
||||||
pub const VAR_LIB: &str = "/var/lib/mk-dl-bot";
|
pub fn parse_env<T>(name: &str) -> T
|
||||||
|
where
|
||||||
// #[cfg(debug_assertions)]
|
T: FromStr,
|
||||||
// pub const VAR_LOG: &str = "target/debug";
|
T::Err: fmt::Debug,
|
||||||
|
{
|
||||||
// #[cfg(not(debug_assertions))]
|
str::parse(unwrap_env(name).as_str()).expect(format!("env '{}' parse error", name).as_str())
|
||||||
// pub const VAR_LOG: &str = "/var/log/mk-dl-bot";
|
|
||||||
|
|
||||||
pub fn make_database_url() -> String {
|
|
||||||
let path = Path::new(VAR_LIB).join("mk-dl-bot.db");
|
|
||||||
format!("sqlite://{}", path.as_os_str().to_str().unwrap()).to_string()
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue