Rebuild config

This commit is contained in:
DefectingCat
2023-04-14 22:58:03 +08:00
parent ea75ff7af1
commit 22d762c044
3 changed files with 12 additions and 15 deletions

View File

@ -1,11 +1,10 @@
use clap::Parser;
use serde::{Deserialize, Serialize};
use std::{fs, path::PathBuf, process::exit, sync::Arc};
use tokio::sync::Mutex;
use std::{fs, path::PathBuf, process::exit};
use crate::arg::Args;
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct List {
exact: Vec<String>,
wildcard: Vec<String>,
@ -20,8 +19,6 @@ pub struct Config {
pub list: List,
}
pub type RConfig = Arc<Mutex<Config>>;
impl Config {
pub fn build() -> Self {
let args = Args::parse();

View File

@ -10,7 +10,7 @@ use tokio::{
io::AsyncWriteExt,
};
use crate::config::RConfig;
use crate::config::Config;
pub async fn create_folder(file_path: &Path) -> Result<()> {
if file_path.exists() {
@ -26,8 +26,7 @@ pub async fn create_folder(file_path: &Path) -> Result<()> {
Ok(())
}
pub async fn init_logger(config: RConfig) -> Result<()> {
let config = config.lock().await;
pub async fn init_logger(config: &Config) -> Result<()> {
let log_path = if let Some(path) = config.log_path.to_owned() {
path
} else {

View File

@ -1,9 +1,8 @@
use std::{net::SocketAddr, process::exit, sync::Arc};
use std::{net::SocketAddr, process::exit};
use anyhow::Result;
use axum::{http, response, routing::get, Router, Server};
use log::{error, info};
use tokio::sync::Mutex;
use crate::{config::Config, routes::messages::get_sms_aspx};
@ -17,15 +16,14 @@ mod routes;
#[tokio::main]
async fn main() -> Result<()> {
let config = Arc::new(Mutex::new(Config::build()));
let config = Config::build();
let log_config = config.clone();
if let Err(err) = logger::init_logger(log_config).await {
if let Err(err) = logger::init_logger(&config).await {
error!("Failed to create logger; {}", err.to_string());
exit(1);
}
let port = if let Some(port) = config.clone().lock().await.port.to_owned() {
let port = if let Some(port) = config.port {
port
} else {
error!("Failed to read port from config, using default port 3000");
@ -36,7 +34,10 @@ async fn main() -> Result<()> {
let message_routes = Router::new()
.route("/sms.aspx", get(get_sms_aspx))
.route("/smsGBK.aspx", get(get_sms_aspx));
let app = Router::new().merge(message_routes).fallback(fallback);
let app = Router::new()
.merge(message_routes)
.fallback(fallback)
.with_state(config.list);
info!("Server starting");
let addr: SocketAddr = match format!("0.0.0.0:{port:?}").parse() {