mirror of
https://github.com/DefectingCat/rua-list
synced 2025-07-15 16:51:31 +00:00
Add mutex to config
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
use clap::Parser;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{fs, path::PathBuf, process::exit};
|
||||
use std::{fs, path::PathBuf, process::exit, sync::Arc};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::arg::Args;
|
||||
|
||||
@ -19,6 +20,8 @@ pub struct Config {
|
||||
pub list: List,
|
||||
}
|
||||
|
||||
pub type RConfig = Arc<Mutex<Config>>;
|
||||
|
||||
impl Config {
|
||||
pub fn build() -> Self {
|
||||
let args = Args::parse();
|
||||
|
@ -1,7 +1,7 @@
|
||||
use std::path::PathBuf;
|
||||
use std::process::exit;
|
||||
use std::{io::Write, path::Path};
|
||||
|
||||
use crate::config::Config;
|
||||
use anyhow::Result;
|
||||
use chrono::Local;
|
||||
use env_logger::{Builder, Env};
|
||||
@ -10,6 +10,8 @@ use tokio::{
|
||||
io::AsyncWriteExt,
|
||||
};
|
||||
|
||||
use crate::config::RConfig;
|
||||
|
||||
pub async fn create_folder(file_path: &Path) -> Result<()> {
|
||||
if file_path.exists() {
|
||||
return Ok(());
|
||||
@ -24,13 +26,20 @@ pub async fn create_folder(file_path: &Path) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn init_logger(config: &Config) -> Result<()> {
|
||||
let log_path = if let Some(path) = &config.log_path {
|
||||
pub async fn init_logger(config: RConfig) -> Result<()> {
|
||||
let config = config.lock().await;
|
||||
let log_path = if let Some(path) = config.log_path.clone() {
|
||||
path
|
||||
} else {
|
||||
panic!("Can not read log path from config")
|
||||
eprintln!("Can not read log path from config");
|
||||
exit(1);
|
||||
};
|
||||
let log_level = if let Some(level) = config.log_level.clone() {
|
||||
level
|
||||
} else {
|
||||
eprintln!("Can not read log level from config");
|
||||
exit(1);
|
||||
};
|
||||
let log_level = config.log_level.clone().unwrap();
|
||||
|
||||
let now = Local::now();
|
||||
let formatted = format!("{}.log", now.format("%Y-%m-%d"));
|
||||
|
11
src/main.rs
11
src/main.rs
@ -1,7 +1,8 @@
|
||||
use std::process::exit;
|
||||
use std::{process::exit, sync::Arc};
|
||||
|
||||
use anyhow::Result;
|
||||
use log::{error, info};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::config::Config;
|
||||
|
||||
@ -12,15 +13,15 @@ mod logger;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
let config = Config::build();
|
||||
if let Err(err) = logger::init_logger(&config).await {
|
||||
let config = Arc::new(Mutex::new(Config::build()));
|
||||
|
||||
let config = config.clone();
|
||||
if let Err(err) = logger::init_logger(config).await {
|
||||
error!("Failed to create logger; {}", err.to_string());
|
||||
exit(1);
|
||||
}
|
||||
|
||||
info!("Server starting");
|
||||
|
||||
println!("{config:?}");
|
||||
|
||||
Ok(())
|
||||
}
|
Reference in New Issue
Block a user