Skip to content

Commit

Permalink
Async file_writer support
Browse files Browse the repository at this point in the history
  • Loading branch information
heydocode committed Jul 2, 2024
1 parent 4d25363 commit 9ef48bf
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 298 deletions.
37 changes: 25 additions & 12 deletions src/file_writer.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,48 @@
use std::io::prelude::*;
use std::fs::OpenOptions;
use std::path::Path;
use std::fs::File;

pub fn write_to_file(text: String, file_path: &str) -> Result<(), String> {
use async_std::fs::OpenOptions;
use async_std::path::Path;
use async_std::fs::File;
use async_std::prelude::*;

pub async fn write_to_file(text: String, file_path: &str) -> Result<(), String> {
let mut file = OpenOptions::new()
.append(true)
.create(true)
.open(file_path)
.await
.map_err(|e| e.to_string())?;

write!(file, "{} ", text).map_err(|e| e.to_string())
file.write_all(text.as_bytes()).await.map_err(|e| e.to_string())
}

pub fn update_credentials(username: String, password: String) {
pub async fn update_credentials(username: String, password: String) -> Result<(), String> {
let file_path = "config/smtp_account.txt";
let path = Path::new(file_path);

let mut content = String::new();
{
let mut file = File::open(path).expect("An error occurs when tried to open a file");
file.read_to_string(&mut content).expect("An error occurs when tried to read the file");
let mut file = File::open(path)
.await
.map_err(|e| e.to_string())?;
file.read_to_string(&mut content)
.await
.map_err(|e| e.to_string())?;
}

let updated_content = content
.replace("username:[email protected]", &format!("username:{}", username))
.replace("password:password123123", &format!("password:{}", password));

{
let mut file = OpenOptions::new().write(true).truncate(true).open(&path).expect("An error occurs when tried to open a file with OpenOptions");
file.write_all(updated_content.as_bytes()).expect("An error occurs when tried to update content");
let mut file = OpenOptions::new()
.write(true)
.truncate(true)
.open(&path)
.await
.map_err(|e| e.to_string())?;
file.write_all(updated_content.as_bytes())
.await
.map_err(|e| e.to_string())?;
}

Ok(())
}
6 changes: 3 additions & 3 deletions src/mail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use lettre::{
use crate::file_reader::smtp_account_reader;
use crate::file_writer::write_to_file;

pub fn mail_sender(title: &str, text: String) {
pub async fn mail_sender(title: &str, text: String) {
let email = Message::builder()
.from("ServerAPI <[email protected]>".parse().expect("Error parsing sender in Message Builder"))
// .reply_to("Yuin <[email protected]>".parse().unwrap())
Expand All @@ -28,14 +28,14 @@ pub fn mail_sender(title: &str, text: String) {
// Send the email
match mailer.send(&email) {
Ok(_) => {
match write_to_file(format!("Email has been sent successfully!"), "config/mail_logs.txt") {
match write_to_file(format!("Email has been sent successfully!"), "config/mail_logs.txt").await {
Ok(()) => {},
n => println!("An error occured when tried to write logs (email success): {n:?}")
}
println!("Email has been sent successfully!");
},
Err(e) => {
match write_to_file(format!("Could not send email: {e:?}"), "config/mail_logs.txt") {
match write_to_file(format!("Could not send email: {e:?}"), "config/mail_logs.txt").await {
Ok(()) => {},
n => println!("An error occured when tried to write logs (email failure): {n:?}")
}
Expand Down
19 changes: 11 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ mod mail;
mod file_reader;
mod file_writer;
mod scheduler;
mod mc_server;
// mod mc_server;
mod setup;
#[cfg(test)]
mod tests;

use scheduler::status_upload;
use mc_server::start_mc_server;
// use mc_server::start_mc_server;
use file_writer::write_to_file;
use setup::check_config_components;
use std::thread;
Expand All @@ -15,33 +17,34 @@ use tracing_subscriber;
fn main() {
tracing_subscriber::fmt::init();

let setup_handle = thread::Builder::new().name("setup".to_string()).spawn(|| {
let setup_handle = thread::Builder::new().name("setup".to_string()).spawn(|| async {
match check_config_components() {
Ok(()) => {},
n => println!("An error occured when tried to check config components: {n:?}")
}

match write_to_file(format!("All config components are OK, loading the server.."), "config/logs.txt") {
match write_to_file(format!("All config components are OK, loading the server.."), "config/logs.txt").await {
Ok(()) => {},
n => println!("An error occured when tried to write logs (setup success): {n:?}")
}
});

setup_handle.expect("Unable to join setup handle").join().unwrap();
println!("All config components are OK, loading the server..");

let mail_handle = thread::Builder::new().name("mc_server".to_string()).spawn(|| {
/*
let mc_handle = thread::Builder::new().name("mc_server".to_string()).spawn(|| {
println!("MC Server has been enabled");
start_mc_server();
println!("MC Server has been disabled");
});
*/

let mc_handle = thread::Builder::new().name("mail_server".to_string()).spawn(|| {
let mail_handle = thread::Builder::new().name("mail_server".to_string()).spawn(|| {
println!("Mail Server has been enabled");
status_upload();
println!("Mail Server has been disabled");
});

mail_handle.expect("Unable to join mail server handle").join().unwrap();
mc_handle.expect("Unable to join mc server handle").join().unwrap();
// mc_handle.expect("Unable to join mc server handle").join().unwrap();
}
Loading

0 comments on commit 9ef48bf

Please sign in to comment.