Skip to content

Commit a90cf36

Browse files
committed
Code optimization, added postgres db
1 parent f3f3d49 commit a90cf36

1 file changed

Lines changed: 83 additions & 9 deletions

File tree

src/main.rs

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
1-
use std::fs::File;
2-
use std::io::Read;
1+
use crate::database::Database;
2+
use crate::logger::LOGGER;
33
use colored::Colorize;
4+
use config::Config;
5+
use std::env;
6+
use std::fs::{create_dir_all, File};
7+
use std::io::{Read, Write};
8+
use std::sync::Arc;
9+
use colored::control::set_override;
410
use teloxide::Bot;
11+
use tokio::fs::write;
512

613
mod fetcher;
714
mod bot;
815
mod data;
916
mod parser;
17+
mod database;
18+
mod logger;
19+
20+
const CONFIG: &[u8] = include_bytes!("../config.yml");
1021

1122
#[tokio::main]
1223
async fn main() {
13-
println!("{}", "Fetching .env file...".bright_blue());
24+
let token = fetch_token();
25+
load_config_file().await;
26+
27+
LOGGER.executing("Starting bot...");
28+
let teloxide_bot = Bot::new(token);
29+
30+
let db = Arc::new(connect_to_db().await);
31+
32+
db.init().await.expect("Database initialization failed");
33+
34+
// start bot
35+
LOGGER.success("Bot started successfully!");
36+
bot::start(teloxide_bot, Arc::clone(&db)).await;
37+
}
38+
39+
fn fetch_token() -> String {
40+
LOGGER.executing("Fetching .env file...");
1441
let mut file = File::open(".env").unwrap();
1542
let mut bytes = [0u8;512];
1643

@@ -19,12 +46,59 @@ async fn main() {
1946
let slice = &bytes[..size];
2047

2148
let token = std::str::from_utf8(slice).unwrap();
22-
println!("{}", ".env successfully fetched!".bright_blue());
49+
LOGGER.success(".env successfully fetched!");
50+
token.to_string()
51+
}
2352

24-
println!("{}", "Starting bot...".bright_blue());
25-
let teloxide_bot = Bot::new(token);
53+
async fn connect_to_db() -> Database {
54+
let source = config::File::with_name(get_config_file_path().as_str());
2655

27-
// start bot
28-
println!("{}", "Bot started successfully!".bright_blue());
29-
bot::start(teloxide_bot).await;
56+
let config = Config::builder()
57+
.add_source(source)
58+
.build()
59+
.expect("Configuration error");
60+
61+
let url = config.get_string("database.url").unwrap();
62+
let port = config.get_int("database.port").unwrap();
63+
let password = config.get_string("database.password").unwrap();
64+
let username = config.get_string("database.user").unwrap();
65+
let database = config.get_string("database.database").unwrap();
66+
67+
let mut db = Database::new(
68+
url,
69+
port as u16,
70+
password,
71+
username,
72+
database
73+
);
74+
75+
db.try_connect().await;
76+
77+
db
3078
}
79+
80+
fn get_config_file_path() -> String {
81+
let dir = env::current_dir().unwrap().to_str().unwrap().to_string();
82+
83+
let mut work_file: String = "config.yml".to_string();
84+
85+
if cfg!(windows) {
86+
work_file = format!("{}\\config\\config.yml", dir);
87+
} else {
88+
work_file = format!("{}/config/config.yml", dir);
89+
}
90+
91+
work_file
92+
}
93+
94+
async fn load_config_file() {
95+
let work_file = get_config_file_path();
96+
97+
let file = File::open(&work_file);
98+
if file.is_err() {
99+
create_dir_all(work_file.as_str().replace("config.yml", "")).unwrap();
100+
write(work_file, CONFIG)
101+
.await
102+
.expect("Could not write to config.yml");
103+
}
104+
}

0 commit comments

Comments
 (0)