From 5ef22d47537bff1eeedc62f00b0dce8e8453e8e4 Mon Sep 17 00:00:00 2001 From: dianhsu Date: Sun, 31 Dec 2023 00:17:34 +0800 Subject: [PATCH] Update version and add log file configuration --- .gitignore | 3 ++- Cargo.lock | 2 +- Cargo.toml | 2 +- src/config.rs | 8 ++++++-- src/main.rs | 39 +++++++++++++++++++++++++++++++++------ 5 files changed, 43 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index 8a04a48..9cdf07e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ AtCoder*/ Codeforces*/ .env AcWing/ -Luogu/ \ No newline at end of file +Luogu/ +*.log \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 5691c43..5f7b828 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -209,7 +209,7 @@ dependencies = [ [[package]] name = "ccs" -version = "0.1.4" +version = "0.1.5" dependencies = [ "axum", "chrono", diff --git a/Cargo.toml b/Cargo.toml index 5a9689d..c66a06e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ccs" -version = "0.1.4" +version = "0.1.5" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/config.rs b/src/config.rs index 1ee94fc..9c1dcf3 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,12 +3,12 @@ use clap::Parser; #[command(author, version, about)] pub struct ServerConfig { /// store the workspace path - #[arg(short, long, default_value = "")] + #[arg(short, long, default_value = ".")] pub workspace: String, /// templates: src/main.rs, src/model.rs, src/template.rs #[arg(short, long, default_value = "")] - pub templates: Vec, + pub templates: String, /// allow open by vscode #[arg(short, long, default_value_t = false)] @@ -21,6 +21,10 @@ pub struct ServerConfig { /// verbose mode #[arg(short, long, default_value_t = false)] pub verbose: bool, + + /// log to file + #[arg(short, long, default_value = "stderr")] + pub log_file: String, } lazy_static! { pub static ref SERVER_CONFIG: ServerConfig = ServerConfig::parse(); diff --git a/src/main.rs b/src/main.rs index f45d208..e6d996e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use axum::http::StatusCode; use axum::routing::{get, post}; use axum::{Json, Router}; +use std::fs::File; use std::path::Path; use tokio::fs; mod config; @@ -55,7 +56,19 @@ async fn main_post(Json(task): Json) -> StatusCode { } } // create template files - for template in &SERVER_CONFIG.templates { + let templates: Vec<&str> = SERVER_CONFIG + .templates + .split(',') + .into_iter() + .filter_map(|x| { + if x.trim().is_empty() { + None + } else { + Some(x.trim()) + } + }) + .collect(); + for template in templates { let source_path = Path::new(&template); let src_filename = match source_path.file_name() { Some(filename) => filename, @@ -105,7 +118,9 @@ async fn main_post(Json(task): Json) -> StatusCode { } #[tokio::main] async fn main() { - env_logger::builder() + // setup logs + let mut builder = env_logger::builder(); + builder .format(|buf, record| { writeln!( buf, @@ -122,14 +137,26 @@ async fn main() { if SERVER_CONFIG.verbose { LevelFilter::Debug } else { - LevelFilter::Info + LevelFilter::Warn }, ) - .write_style(env_logger::WriteStyle::Auto) - .init(); + .write_style(env_logger::WriteStyle::Auto); + match SERVER_CONFIG.log_file.as_str() { + "stderr" => {} + "stdout" => { + builder.target(env_logger::Target::Stdout); + } + file => { + let target = Box::new(File::create(file).expect("Failed to create log file.")); + builder.target(env_logger::Target::Pipe(target)); + } + } + builder.init(); + + // start server log::info!("Server started."); let app = Router::new() - .route("/", get(|| async { "Hello, World!" })) + .route("/", get(|| async { "Hello, CCS!" })) .route("/", post(main_post)); let listener = match tokio::net::TcpListener::bind(format!("127.0.0.1:{}", SERVER_CONFIG.port)).await {