Removes the TUI in favor of event based logging #15
1 changed files with 38 additions and 46 deletions
84
src/main.rs
84
src/main.rs
|
@ -1,11 +1,12 @@
|
||||||
use chrono::{DateTime, Duration, Utc};
|
use chrono::{DateTime, Duration, Utc};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use reqwest::{Client};
|
use reqwest::{Client};
|
||||||
use std::{collections::HashMap, vec};
|
use std::{collections::HashMap};
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::sync::{Arc};
|
use std::sync::{Arc};
|
||||||
|
use log::{error, warn, info, LevelFilter};
|
||||||
use tokio::sync::{RwLock};
|
use tokio::sync::{RwLock};
|
||||||
use strum_macros::Display;
|
use systemd_journal_logger::{connected_to_journal, JournalLog};
|
||||||
use tokio::time::sleep;
|
use tokio::time::sleep;
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::post_history::{SeriesHistory};
|
use crate::post_history::{SeriesHistory};
|
||||||
|
@ -14,9 +15,29 @@ mod config;
|
||||||
mod jnovel;
|
mod jnovel;
|
||||||
mod bot;
|
mod bot;
|
||||||
mod lemmy;
|
mod lemmy;
|
||||||
mod tui;
|
|
||||||
mod post_history;
|
mod post_history;
|
||||||
|
|
||||||
|
pub (crate) fn write_error(err_msg: String) {
|
||||||
|
match connected_to_journal() {
|
||||||
|
true => error!("[ERROR] {err_msg}"),
|
||||||
|
false => println!("[ERROR] {err_msg}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub (crate) fn write_warn(warn_msg: String) {
|
||||||
|
match connected_to_journal() {
|
||||||
|
true => warn!("[WARN] {warn_msg}"),
|
||||||
|
false => println!("[WARN] {warn_msg}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub (crate) fn write_info(info_msg: String) {
|
||||||
|
match connected_to_journal() {
|
||||||
|
true => info!("[INFO] {info_msg}"),
|
||||||
|
false => println!("[INFO] {info_msg}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub static HTTP_CLIENT: Lazy<Client> = Lazy::new(|| {
|
pub static HTTP_CLIENT: Lazy<Client> = Lazy::new(|| {
|
||||||
Client::builder()
|
Client::builder()
|
||||||
.timeout(Duration::seconds(30).to_std().unwrap())
|
.timeout(Duration::seconds(30).to_std().unwrap())
|
||||||
|
@ -27,7 +48,6 @@ pub static HTTP_CLIENT: Lazy<Client> = Lazy::new(|| {
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub(crate) struct SharedData {
|
pub(crate) struct SharedData {
|
||||||
messages: Vec<Message>,
|
|
||||||
config: Config,
|
config: Config,
|
||||||
post_history: SeriesHistory,
|
post_history: SeriesHistory,
|
||||||
start: DateTime<Utc>,
|
start: DateTime<Utc>,
|
||||||
|
@ -36,69 +56,41 @@ pub(crate) struct SharedData {
|
||||||
impl SharedData {
|
impl SharedData {
|
||||||
pub(crate) fn new() -> Self {
|
pub(crate) fn new() -> Self {
|
||||||
SharedData {
|
SharedData {
|
||||||
messages: vec![],
|
config: Config::default(),
|
||||||
config: Config {
|
|
||||||
instance: "".to_owned(),
|
|
||||||
username: "".to_owned(),
|
|
||||||
password: "".to_owned(),
|
|
||||||
status_post_url: None,
|
|
||||||
config_reload_seconds: 0,
|
|
||||||
protected_communities: vec![],
|
|
||||||
series: vec![],
|
|
||||||
},
|
|
||||||
post_history: SeriesHistory {
|
post_history: SeriesHistory {
|
||||||
series: HashMap::new(),
|
series: HashMap::new(),
|
||||||
},
|
},
|
||||||
start: Utc::now(),
|
start: Utc::now(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn get_messages(&self, errors: bool, warnings: bool, infos: bool) -> Vec<Message> {
|
|
||||||
self.messages.iter().filter(|msg| {
|
|
||||||
match msg {
|
|
||||||
Message::Error(_) => errors,
|
|
||||||
Message::Warning(_) => warnings,
|
|
||||||
Message::Info(_) => infos,
|
|
||||||
}
|
|
||||||
}).cloned().collect()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, Display, PartialEq)]
|
|
||||||
pub(crate) enum Message {
|
|
||||||
Info(String),
|
|
||||||
Warning(String),
|
|
||||||
Error(String),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Message {
|
|
||||||
pub(crate) fn content(&self) -> String {
|
|
||||||
match self {
|
|
||||||
Message::Info(msg) => msg.clone(),
|
|
||||||
Message::Warning(msg) => msg.clone(),
|
|
||||||
Message::Error(msg) => msg.clone(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let mut data = SharedData::new();
|
JournalLog::new().expect("Systemd-Logger crate error").install().expect("Systemd-Logger crate error");
|
||||||
|
log::set_max_level(LevelFilter::Info);
|
||||||
|
|
||||||
|
let mut data = SharedData::new();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let write_data = Arc::new(RwLock::new(data.clone()));
|
let write_data = Arc::new(RwLock::new(data.clone()));
|
||||||
let read_data = write_data.clone();
|
//let read_data = write_data.clone();
|
||||||
let persistent_data = write_data.clone();
|
let persistent_data = write_data.clone();
|
||||||
|
|
||||||
let tui_thread = tokio::spawn(async move { tui::run(read_data).await });
|
|
||||||
let bot_thread = tokio::spawn(async move { bot::run(write_data).await });
|
let bot_thread = tokio::spawn(async move { bot::run(write_data).await });
|
||||||
|
|
||||||
let _ = bot_thread.await;
|
let _ = bot_thread.await;
|
||||||
tui_thread.abort();
|
|
||||||
|
|
||||||
data = persistent_data.read().await.clone();
|
data = persistent_data.read().await.clone();
|
||||||
data.messages.push(Message::Error("Bot crashed due to unknown Error, restarting thread after wait...".to_owned()));
|
|
||||||
|
{
|
||||||
|
let err_msg = "Bot crashed due to unknown Error, restarting thread after wait...";
|
||||||
|
match connected_to_journal() {
|
||||||
|
true => error!("[ERROR] {err_msg}"),
|
||||||
|
false => println!("[ERROR] {err_msg}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
sleep(Duration::seconds(5).to_std().expect("Conversion should always work since static")).await;
|
sleep(Duration::seconds(5).to_std().expect("Conversion should always work since static")).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue