diff --git a/.vscode/settings.json b/.vscode/settings.json index 1ed1a328..514678a6 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -22,6 +22,7 @@ "reqwest", "rustls", "rustup", + "serde", "sparklepost", "sparkpost", "sqlx", diff --git a/Cargo.lock b/Cargo.lock index 097c9169..f178a3f6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1753,7 +1753,7 @@ dependencies = [ "bitflags 2.6.0", "cexpr", "clang-sys", - "itertools 0.13.0", + "itertools 0.12.1", "proc-macro2", "quote", "regex", @@ -2586,9 +2586,9 @@ version = "1.0.0" dependencies = [ "actix-web", "env_logger", - "log", - "redis 0.25.4", + "redis 0.27.6", "serde", + "tracing", ] [[package]] @@ -6045,7 +6045,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e9552f850d5f0964a4e4d0bf306459ac29323ddfbae05e35a7c0d35cb0803cc5" dependencies = [ "anyhow", - "itertools 0.13.0", + "itertools 0.12.1", "proc-macro2", "quote", "syn 2.0.90", @@ -6226,30 +6226,6 @@ dependencies = [ "bitflags 2.6.0", ] -[[package]] -name = "redis" -version = "0.25.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0d7a6955c7511f60f3ba9e86c6d02b3c3f144f8c24b288d1f4e18074ab8bbec" -dependencies = [ - "arc-swap", - "async-trait", - "bytes", - "combine", - "futures", - "futures-util", - "itoa", - "percent-encoding", - "pin-project-lite", - "ryu", - "sha1_smol", - "socket2", - "tokio", - "tokio-retry", - "tokio-util", - "url", -] - [[package]] name = "redis" version = "0.26.1" @@ -6293,6 +6269,7 @@ dependencies = [ "pin-project-lite", "ryu", "sha1_smol", + "socket2", "tokio", "tokio-util", "url", diff --git a/Cargo.toml b/Cargo.toml index 3018b54f..509928b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -109,6 +109,7 @@ openssl = { version = "0.10.60", features = ["v110"] } parking_lot = "0.12" pin-project-lite = "0.2" rand = "0.8" +redis = { version = "0.27" } reqwest = { version = "0.12", features = ["json", "stream"] } rustls = "0.23" rustls-pemfile = "2" diff --git a/databases/redis/Cargo.toml b/databases/redis/Cargo.toml index 14c7ce16..4dbe6786 100644 --- a/databases/redis/Cargo.toml +++ b/databases/redis/Cargo.toml @@ -5,8 +5,7 @@ edition = "2021" [dependencies] actix-web.workspace = true - env_logger.workspace = true -log.workspace = true -redis = { version = "0.25", features = ["tokio-comp", "connection-manager"] } +redis = { workspace = true, features = ["tokio-comp", "connection-manager"] } serde.workspace = true +tracing.workspace = true diff --git a/databases/redis/src/main.rs b/databases/redis/src/main.rs index 8b949937..bbd4b373 100644 --- a/databases/redis/src/main.rs +++ b/databases/redis/src/main.rs @@ -3,6 +3,20 @@ use std::io; use actix_web::{error, middleware, web, App, HttpResponse, HttpServer, Responder}; use serde::Deserialize; +async fn get_from_cache(redis: web::Data) -> actix_web::Result { + let mut conn = redis + .get_connection_manager() + .await + .map_err(error::ErrorInternalServerError)?; + + let res = redis::Cmd::mget(&["my_domain:one", "my_domain:two", "my_domain:three"]) + .query_async::>(&mut conn) + .await + .map_err(error::ErrorInternalServerError)?; + + Ok(HttpResponse::Ok().json(res)) +} + #[derive(Deserialize)] pub struct CacheInfo { one: String, @@ -24,7 +38,7 @@ async fn cache_stuff( ("my_domain:two", info.two), ("my_domain:three", info.three), ]) - .query_async::<_, String>(&mut conn) + .query_async::(&mut conn) .await .map_err(error::ErrorInternalServerError)?; @@ -43,7 +57,7 @@ async fn del_stuff(redis: web::Data) -> actix_web::Result(&mut conn) + .query_async::(&mut conn) .await .map_err(error::ErrorInternalServerError)?; @@ -51,7 +65,7 @@ async fn del_stuff(redis: web::Data) -> actix_web::Result) -> actix_web::Result io::Result<()> { env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); - log::info!("starting HTTP server at http://localhost:8080"); + tracing::info!("starting HTTP server at http://localhost:8080"); let redis = redis::Client::open("redis://127.0.0.1:6379").unwrap(); HttpServer::new(move || { App::new() .app_data(web::Data::new(redis.clone())) - .wrap(middleware::Logger::default()) .service( web::resource("/stuff") + .route(web::get().to(get_from_cache)) .route(web::post().to(cache_stuff)) .route(web::delete().to(del_stuff)), ) + .wrap(middleware::NormalizePath::trim()) + .wrap(middleware::Logger::default()) }) .workers(2) .bind(("127.0.0.1", 8080))?