From e8735843bbf58021c947c0acdde2cf5419c32093 Mon Sep 17 00:00:00 2001 From: guochao Date: Tue, 23 Aug 2022 16:17:22 +0800 Subject: [PATCH] add redis into project --- Cargo.lock | 84 ++++++++++++++++++++++++++++++++++++++++++++----- Cargo.toml | 3 ++ src/main.rs | 11 ++++++- src/web/mod.rs | 14 +++++++-- src/web/view.rs | 12 +++++-- 5 files changed, 109 insertions(+), 15 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 51a052b..7be29f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -49,14 +49,14 @@ dependencies = [ "http", "httparse", "httpdate", - "itoa", + "itoa 1.0.2", "language-tags", "local-channel", "mime", "percent-encoding", "pin-project-lite", "rand", - "sha1", + "sha1 0.10.1", "smallvec", "tracing", "zstd", @@ -159,7 +159,7 @@ dependencies = [ "encoding_rs", "futures-core", "futures-util", - "itoa", + "itoa 1.0.2", "language-tags", "log", "mime", @@ -670,6 +670,16 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "combine" +version = "4.6.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35ed6e9d84f0b51a7f52daf1c7d71dd136fd7a3f41a8462b8cdb8c78d920fad4" +dependencies = [ + "bytes", + "memchr", +] + [[package]] name = "concurrent-queue" version = "1.2.4" @@ -842,6 +852,12 @@ version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" +[[package]] +name = "dtoa" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56899898ce76aaf4a0f24d914c97ea6ed976d42fec6ad33fcbb0a1103e07b2b0" + [[package]] name = "either" version = "1.7.0" @@ -1185,7 +1201,7 @@ checksum = "75f43d41e26995c17e71ee126451dd3941010b0514a81a9d11f3b341debc2399" dependencies = [ "bytes", "fnv", - "itoa", + "itoa 1.0.2", ] [[package]] @@ -1251,6 +1267,12 @@ dependencies = [ "either", ] +[[package]] +name = "itoa" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" + [[package]] name = "itoa" version = "1.0.2" @@ -1879,6 +1901,23 @@ dependencies = [ "getrandom", ] +[[package]] +name = "redis" +version = "0.21.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a80b5f38d7f5a020856a0e16e40a9cfabf88ae8f0e4c2dcd8a3114c1e470852" +dependencies = [ + "async-trait", + "combine", + "dtoa", + "itoa 0.4.8", + "native-tls", + "percent-encoding", + "sha1 0.6.1", + "tokio-native-tls", + "url", +] + [[package]] name = "redox_syscall" version = "0.2.16" @@ -1955,10 +1994,12 @@ dependencies = [ "clap_complete", "env_logger", "log", + "redis", "sea-orm", "sea-orm-migration", "serde", "sqlx", + "tokio", ] [[package]] @@ -2222,7 +2263,7 @@ version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "82c2c1fdcd807d1098552c5b9a36e425e42e9fbd7c6a37a8425f390f781f7fa7" dependencies = [ - "itoa", + "itoa 1.0.2", "ryu", "serde", ] @@ -2234,7 +2275,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" dependencies = [ "form_urlencoded", - "itoa", + "itoa 1.0.2", "ryu", "serde", ] @@ -2250,6 +2291,15 @@ dependencies = [ "digest", ] +[[package]] +name = "sha1" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1da05c97445caa12d05e848c4a4fcbbea29e748ac28f7e80e9b010392063770" +dependencies = [ + "sha1_smol", +] + [[package]] name = "sha1" version = "0.10.1" @@ -2261,6 +2311,12 @@ dependencies = [ "digest", ] +[[package]] +name = "sha1_smol" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae1a47186c03a32177042e55dbc5fd5aee900b8e0069a8d70fba96a9375cd012" + [[package]] name = "sha2" version = "0.10.2" @@ -2388,7 +2444,7 @@ dependencies = [ "hashlink", "hex", "indexmap", - "itoa", + "itoa 1.0.2", "libc", "libsqlite3-sys", "log", @@ -2556,7 +2612,7 @@ version = "0.3.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "74b7cc93fc23ba97fde84f7eea56c55d1ba183f495c6715defdfc7b9cb8c870f" dependencies = [ - "itoa", + "itoa 1.0.2", "js-sys", "libc", "num_threads", @@ -2601,9 +2657,21 @@ dependencies = [ "pin-project-lite", "signal-hook-registry", "socket2", + "tokio-macros", "winapi", ] +[[package]] +name = "tokio-macros" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9724f9a975fb987ef7a3cd9be0350edcbe130698af5b8f7a631e23d42d052484" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tokio-native-tls" version = "0.3.0" diff --git a/Cargo.toml b/Cargo.toml index e3eb8a4..c254abc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,9 @@ sea-orm = { version = "^0.9.0", features = [ ] } actix-web = "4" +tokio = {version = "1", features = ["full"]} +redis = { version = "0.21", features = ["tokio-native-tls-comp"] } + askama_actix = {version = "0.13"} askama = "0.11" serde = { version = "1", features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index 062034d..9743669 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use crate::web::MiddlewareConnections; + use clap::{CommandFactory, Parser, Subcommand}; use clap_complete::Shell; use sea_orm::Database; @@ -13,6 +15,9 @@ struct Opts { #[clap(short, long, value_parser, env, default_value = "0.0.0.0")] address: String, + #[clap(short, long, value_parser, env)] + redis_url: Option, + #[clap(short, long, value_parser, env, default_value = "8888")] port: u16, @@ -66,6 +71,10 @@ async fn main() -> Result<(), Box> { } let database = Database::connect(args.clone().database_url).await?; + let redis = match args.clone().redis_url { + Some(redis_url) => Some(redis::Client::open(redis_url)?), + None => None, + }; if let Some(command) = args.commands.clone() { match command { @@ -81,5 +90,5 @@ async fn main() -> Result<(), Box> { return Ok(()); } - web::run(database, args).await + web::run(MiddlewareConnections { database, redis }, args).await } diff --git a/src/web/mod.rs b/src/web/mod.rs index c86b600..8da7b3e 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -12,15 +12,23 @@ mod model; mod view; #[derive(Debug, Clone)] -struct AppState { +pub struct MiddlewareConnections { + pub database: DatabaseConnection, + pub redis: Option, +} + +#[derive(Debug, Clone)] +struct Controllers { user: UserController, } pub(crate) async fn run( - database: DatabaseConnection, + conn: MiddlewareConnections, opts: Opts, ) -> Result<(), Box> { - let data = actix_web::web::Data::new(AppState { user: UserController::new(database.clone()) }); + let data = actix_web::web::Data::new(Controllers { + user: UserController::new(conn.database.clone()), + }); let server = HttpServer::new(move || App::new().configure(urls::configure).app_data(data.clone())) diff --git a/src/web/view.rs b/src/web/view.rs index f9ac8d4..2db70f2 100644 --- a/src/web/view.rs +++ b/src/web/view.rs @@ -2,7 +2,7 @@ use actix_web::{HttpRequest, Responder}; use sea_orm::*; use serde::{Deserialize, Serialize}; -use super::AppState; +use super::Controllers; use super::model::prelude::*; @@ -12,14 +12,20 @@ pub struct Params { posts_per_page: Option, } +#[derive(Serialize, Deserialize)] +struct Body {} + #[actix_web::get("/")] async fn index_page( req: HttpRequest, - data: actix_web::web::Data, + data: actix_web::web::Data, ) -> Result> { let params = actix_web::web::Query::::from_query(req.query_string()).unwrap(); - let users = data.user.list_all(params.page.unwrap_or(0), params.posts_per_page.unwrap_or(5)).await?; + let users = data + .user + .list_all(params.page.unwrap_or(0), params.posts_per_page.unwrap_or(5)) + .await?; Ok(actix_web::web::Json(users)) }