Skip to content

Commit

Permalink
Update version and add log file configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
dianhsu committed Dec 30, 2023
1 parent e221530 commit 5ef22d4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ AtCoder*/
Codeforces*/
.env
AcWing/
Luogu/
Luogu/
*.log
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 6 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub templates: String,

/// allow open by vscode
#[arg(short, long, default_value_t = false)]
Expand All @@ -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();
Expand Down
39 changes: 33 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -55,7 +56,19 @@ async fn main_post(Json(task): Json<model::Task>) -> 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,
Expand Down Expand Up @@ -105,7 +118,9 @@ async fn main_post(Json(task): Json<model::Task>) -> StatusCode {
}
#[tokio::main]
async fn main() {
env_logger::builder()
// setup logs
let mut builder = env_logger::builder();
builder
.format(|buf, record| {
writeln!(
buf,
Expand All @@ -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 {
Expand Down

0 comments on commit 5ef22d4

Please sign in to comment.