From 03da1cb967f5441f1a4ce2301e097d78ffdbcbe0 Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Mon, 12 May 2025 06:40:45 +0100 Subject: [PATCH] refactor: improve basics example --- Cargo.lock | 26 +++++++++++++------------- Cargo.toml | 1 + basics/basics/Cargo.toml | 15 +++++++-------- basics/basics/README.md | 6 +++--- basics/basics/src/main.rs | 8 ++++---- examples-common/src/lib.rs | 2 +- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ae3448a2..f12fec44 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1705,19 +1705,6 @@ dependencies = [ "serde", ] -[[package]] -name = "basics" -version = "0.0.0" -dependencies = [ - "actix-files", - "actix-session", - "actix-web", - "actix-web-lab", - "async-stream", - "env_logger", - "log", -] - [[package]] name = "bb8" version = "0.8.6" @@ -3142,6 +3129,19 @@ version = "2.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" +[[package]] +name = "example-basics" +version = "0.0.0" +dependencies = [ + "actix-files", + "actix-session", + "actix-web", + "actix-web-lab", + "async-stream", + "examples-common", + "tracing", +] + [[package]] name = "example-cert-watch" version = "0.0.0" diff --git a/Cargo.toml b/Cargo.toml index 137adead..3d73afa9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -98,6 +98,7 @@ actix-web-lab = "0.24" actix-ws = "0.3" awc = "3.7" +async-stream = "0.3" chrono = { version = "0.4.30", features = ["serde"] } color-eyre = "0.6" derive_more = "2" diff --git a/basics/basics/Cargo.toml b/basics/basics/Cargo.toml index 4fc43dd7..a183d9ab 100644 --- a/basics/basics/Cargo.toml +++ b/basics/basics/Cargo.toml @@ -1,14 +1,13 @@ [package] -name = "basics" +name = "example-basics" edition.workspace = true rust-version.workspace = true [dependencies] -actix-files.workspace = true +actix-files = { workspace = true } actix-session = { workspace = true, features = ["cookie-session"] } -actix-web.workspace = true -actix-web-lab.workspace = true - -async-stream = "0.3" -env_logger.workspace = true -log.workspace = true +actix-web = { workspace = true } +actix-web-lab = { workspace = true } +async-stream = { workspace = true } +examples-common = { workspace = true } +tracing = { workspace = true } diff --git a/basics/basics/README.md b/basics/basics/README.md index b0f71767..a2138a80 100644 --- a/basics/basics/README.md +++ b/basics/basics/README.md @@ -1,8 +1,8 @@ -# basics +# Basics ## Usage -### server +### Server ```sh cd basics/basics @@ -10,7 +10,7 @@ cargo run # Started http server: 127.0.0.1:8080 ``` -### web client +### Browser - [http://localhost:8080/async-body/bob](http://localhost:8080/async-body/bob) - [http://localhost:8080/user/bob/](http://localhost:8080/user/bob) text/plain download diff --git a/basics/basics/src/main.rs b/basics/basics/src/main.rs index 54a840f6..56c18735 100644 --- a/basics/basics/src/main.rs +++ b/basics/basics/src/main.rs @@ -78,12 +78,12 @@ async fn with_param(req: HttpRequest, Path((name,)): Path<(String,)>) -> HttpRes #[actix_web::main] async fn main() -> io::Result<()> { - env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); + examples_common::init_standard_logger(); // random key means that restarting server will invalidate existing session cookies let key = actix_web::cookie::Key::from(SESSION_SIGNING_KEY); - log::info!("starting HTTP server at http://localhost:8080"); + tracing::info!("Starting HTTP server at http://localhost:8080"); HttpServer::new(move || { App::new() @@ -96,7 +96,7 @@ async fn main() -> io::Result<()> { .build(), ) // enable logger - always register Actix Web Logger middleware last - .wrap(middleware::Logger::default()) + .wrap(middleware::Logger::default().log_target("@")) // register favicon .service(favicon) // register simple route, handle all methods @@ -114,7 +114,7 @@ async fn main() -> io::Result<()> { ) .service(web::resource("/error").to(|| async { error::InternalError::new( - io::Error::new(io::ErrorKind::Other, "test"), + io::Error::other("test"), StatusCode::INTERNAL_SERVER_ERROR, ) })) diff --git a/examples-common/src/lib.rs b/examples-common/src/lib.rs index 9bbd1015..9d9e6cbd 100644 --- a/examples-common/src/lib.rs +++ b/examples-common/src/lib.rs @@ -9,7 +9,7 @@ pub fn init_standard_logger() { .with_default_directive(LevelFilter::INFO.into()) .from_env_lossy(), ) - .with(tracing_subscriber::fmt::layer()) + .with(tracing_subscriber::fmt::layer().without_time()) .init(); }