diff options
| -rw-r--r-- | Cargo.lock | 118 | ||||
| -rw-r--r-- | Cargo.toml | 4 | ||||
| -rw-r--r-- | src/config.rs | 4 | ||||
| -rw-r--r-- | src/errors.rs | 10 | ||||
| -rw-r--r-- | src/lib.rs | 8 | ||||
| -rw-r--r-- | src/trace.rs | 7 |
6 files changed, 148 insertions, 3 deletions
@@ -18,6 +18,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" [[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] name = "anstream" version = "0.6.21" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -505,6 +514,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" [[package]] +name = "lazy_static" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] name = "libc" version = "0.2.175" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -527,6 +542,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34080505efa8e45a4b816c349525ebe327ceaa8559756f0356cba97ef3bf7432" [[package]] +name = "matchers" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" +dependencies = [ + "regex-automata", +] + +[[package]] name = "matchit" version = "0.8.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -575,6 +599,15 @@ dependencies = [ ] [[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys 0.60.2", +] + +[[package]] name = "object" version = "0.36.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -664,6 +697,23 @@ dependencies = [ ] [[package]] +name = "regex-automata" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "722166aa0d7438abbaa4d5cc2c649dac844e8c56d82fb3d33e9c34b5cd268fc6" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3160422bbd54dd5ecfdca71e5fd59b7b8fe2b1697ab2baf64f6d05dcc66d298" + +[[package]] name = "rustc-demangle" version = "0.1.26" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -701,6 +751,8 @@ dependencies = [ "tokio", "toml", "tower-http", + "tracing", + "tracing-subscriber", ] [[package]] @@ -785,6 +837,15 @@ dependencies = [ ] [[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] name = "signal-hook-registry" version = "1.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -839,6 +900,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" [[package]] +name = "thread_local" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185" +dependencies = [ + "cfg-if", +] + +[[package]] name = "tokio" version = "1.47.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -983,16 +1053,58 @@ checksum = "784e0ac535deb450455cbfa28a6f0df145ea1bb7ae51b821cf5e7927fdcfbdd0" dependencies = [ "log", "pin-project-lite", + "tracing-attributes", "tracing-core", ] [[package]] +name = "tracing-attributes" +version = "0.1.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81383ab64e72a7a8b8e13130c49e3dab29def6d0c7d76a03087b3cf71c5c6903" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] name = "tracing-core" version = "0.1.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b9d12581f227e93f094d3af2ae690a574abb8a2b9b7a96e7cfe9647b2b617678" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2054a14f5307d601f88daf0553e1cbf472acc4f2c51afab632431cdcd72124d5" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex-automata", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", ] [[package]] @@ -1014,6 +1126,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + +[[package]] name = "wasi" version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -18,4 +18,6 @@ serde = { version = "1.0.223", features = ["derive"] } serde_json = "1.0.145" tokio = { version = "1.47.1", features = ["full"] } toml = "0.9.5" -tower-http = { version = "0.6.6", features = ["fs"] } +tower-http = { version = "0.6.6", features = ["fs", "trace"] } +tracing = "0.1.41" +tracing-subscriber = { version = "0.3.20", features = ["env-filter"] } diff --git a/src/config.rs b/src/config.rs index 2f1bbb7..31d8489 100644 --- a/src/config.rs +++ b/src/config.rs @@ -25,6 +25,10 @@ impl Config { let mut config = if config_path.exists() { Config::load_config(config_path)? } else { + tracing::info!( + "Couldn't load configuration from {}, generating defaults", + config_path.display() + ); Config::generate_defaults(config_home)? }; diff --git a/src/errors.rs b/src/errors.rs index 6b53c2d..dc5a034 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -31,6 +31,9 @@ pub enum Error { #[from] ParseIntError(std::num::ParseIntError), + + #[from] + TracingSubEnvError(tracing_subscriber::filter::FromEnvError), } impl core::fmt::Display for Error { @@ -48,6 +51,7 @@ impl core::fmt::Display for Error { Error::TomlDeError(e) => write!(fmt, "TOML deserialization error: {e}"), Error::TomlSerError(e) => write!(fmt, "TOML serialization error: {e}"), Error::ParseIntError(e) => write!(fmt, "Parsing error: {e}"), + Error::TracingSubEnvError(e) => write!(fmt, "Environment variable error: {e}"), } } } @@ -88,9 +92,13 @@ impl IntoResponse for Error { StatusCode::INTERNAL_SERVER_ERROR, format!("Parsing error: {e}"), ), + Error::TracingSubEnvError(ref e) => ( + StatusCode::INTERNAL_SERVER_ERROR, + format!("Environment variable error: {e}"), + ), }; - println!("{} {}", &status, &error_message); + tracing::error!("{} {}", &status, &error_message); (status, error_message).into_response() } } @@ -3,6 +3,7 @@ mod config; mod errors; mod handlers; mod models; +mod trace; pub use errors::{Error, Result}; @@ -11,7 +12,7 @@ use std::net::{IpAddr, Ipv6Addr, SocketAddr}; use axum::{Router, routing::get}; use clap::Parser; use tokio::net::TcpListener; -use tower_http::services::ServeFile; +use tower_http::{services::ServeFile, trace::TraceLayer}; use args::{Cli, Command}; use config::Config; @@ -21,6 +22,7 @@ pub fn app(config: Config) -> Router { .nest_service("/static/style.css", ServeFile::new(&config.style_file)) .nest_service("/static/favicon.svg", ServeFile::new(&config.favicon_file)) .route("/", get(handlers::handler)) + .layer(TraceLayer::new_for_http()) .with_state(config) } @@ -31,8 +33,12 @@ pub async fn run() -> Result<()> { Command::Serv(args) => { let config = config::Config::new(args)?; + trace::tracing_registry(); + let socket = SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), config.port); + let listener = TcpListener::bind(socket).await?; + tracing::info!("listening on {}", listener.local_addr()?); axum::serve(listener, app(config.clone())).await?; } diff --git a/src/trace.rs b/src/trace.rs new file mode 100644 index 0000000..0b0eb23 --- /dev/null +++ b/src/trace.rs @@ -0,0 +1,7 @@ +use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; + +pub fn tracing_registry() { + tracing_subscriber::registry() + .with(tracing_subscriber::fmt::layer().without_time()) + .init(); +} |
