init with tokio

This commit is contained in:
xfy
2024-09-14 14:58:13 +08:00
parent c90e4e0dc1
commit 1d0228d5e5
4 changed files with 1265 additions and 3 deletions

1204
web/Rust/axum/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,27 @@
[package]
name = "axum"
name = "phthonus-axum"
version = "0.1.0"
edition = "2021"
[dependencies]
# server
axum = "0.7.5"
tokio = { version = "1.40.0", features = ["full"] }
tower = "0.5.1"
tower-http = { version = "0.5.2", features = ["full"] }
tracing = "0.1.40"
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
# error
anyhow = "1.0.88"
thiserror = "1.0.63"
# tools
dotenvy = "0.15.7"
serde = { version = "1.0.210", features = ["derive", "serde_derive"] }
serde_json = { version = "1.0.128" }
[profile.release]
lto = true
panic = "abort" # Strip expensive panic clean-up logic
codegen-units = 1 # Compile crates one after another so the compiler can optimize better
# opt-level = "s" # Optimize for binary size
strip = true # Remove debug symbols

View File

@ -1,3 +1,12 @@
fn main() {
println!("Hello, world!");
use dotenvy::dotenv;
use tracing::info;
use utils::init_logger;
mod utils;
#[tokio::main]
async fn main() {
dotenv().ok();
init_logger();
info!("Hello, world!");
}

View File

@ -0,0 +1,28 @@
use tracing_subscriber::{fmt, prelude::*, registry, EnvFilter};
/// Initializes the logger for tracing.
///
/// This function sets up the necessary layers for tracing using the `tracing_subscriber`
/// crate. It configures the formatting layer and environment filter based on the value
/// of the `LIMOS_LOG` environment variable (defaulting to "info" if not set).
///
/// # Example
///
/// ```rust
/// use utils::init_logger;
///
/// fn test() {
/// init_logger();
/// }
/// ```
pub fn init_logger() {
let formatting_layer = fmt::layer()
// .pretty()
.with_thread_ids(false)
.with_target(false)
.with_writer(std::io::stdout);
let env_layer = EnvFilter::try_from_env("axum").unwrap_or_else(|_| "info".into());
registry().with(env_layer).with(formatting_layer).init();
}