add redis into project
This commit is contained in:
parent
747e4bc1e6
commit
e8735843bb
84
Cargo.lock
generated
84
Cargo.lock
generated
@ -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"
|
||||
|
@ -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"] }
|
||||
|
11
src/main.rs
11
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<String>,
|
||||
|
||||
#[clap(short, long, value_parser, env, default_value = "8888")]
|
||||
port: u16,
|
||||
|
||||
@ -66,6 +71,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
}
|
||||
|
||||
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<dyn std::error::Error>> {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
web::run(database, args).await
|
||||
web::run(MiddlewareConnections { database, redis }, args).await
|
||||
}
|
||||
|
@ -12,15 +12,23 @@ mod model;
|
||||
mod view;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct AppState {
|
||||
pub struct MiddlewareConnections {
|
||||
pub database: DatabaseConnection,
|
||||
pub redis: Option<redis::Client>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct Controllers {
|
||||
user: UserController,
|
||||
}
|
||||
|
||||
pub(crate) async fn run(
|
||||
database: DatabaseConnection,
|
||||
conn: MiddlewareConnections,
|
||||
opts: Opts,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
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()))
|
||||
|
@ -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<usize>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct Body {}
|
||||
|
||||
#[actix_web::get("/")]
|
||||
async fn index_page(
|
||||
req: HttpRequest,
|
||||
data: actix_web::web::Data<AppState>,
|
||||
data: actix_web::web::Data<Controllers>,
|
||||
) -> Result<impl Responder, Box<dyn std::error::Error>> {
|
||||
let params = actix_web::web::Query::<Params>::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))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user