1
0
mirror of https://github.com/actix/examples synced 2024-11-27 16:02:57 +01:00

update deps

This commit is contained in:
Rob Ede 2022-10-26 16:51:27 +01:00
parent 83e987ed0c
commit ddda86784a
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
25 changed files with 672 additions and 858 deletions

1380
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -11,7 +11,7 @@ members = [
"basics/nested-routing", "basics/nested-routing",
"basics/state", "basics/state",
"basics/static-files", "basics/static-files",
"basics/todo", # "basics/todo",
"cors/backend", "cors/backend",
"data-factory", "data-factory",
"databases/diesel", "databases/diesel",

View File

@ -5,6 +5,8 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
actix-identity = "0.4" actix-identity = "0.5"
actix-session = { version = "0.7", features = ["cookie-session"] }
env_logger = "0.9" env_logger = "0.9"
rand = "0.8" rand = "0.8"

View File

@ -1,23 +1,28 @@
use actix_identity::{CookieIdentityPolicy, Identity, IdentityService}; use actix_identity::{Identity, IdentityMiddleware};
use actix_web::{middleware, web, App, HttpResponse, HttpServer}; use actix_session::{storage::CookieSessionStore, SessionMiddleware};
use actix_web::{
cookie::Key, middleware, web, App, HttpMessage as _, HttpRequest, HttpResponse, HttpServer,
};
use rand::Rng; use rand::Rng;
async fn index(id: Identity) -> String { async fn index(id: Identity) -> String {
format!( format!(
"Hello {}", "Hello {}",
id.identity().unwrap_or_else(|| "Anonymous".to_owned()) id.id().unwrap_or_else(|_| "Anonymous".to_owned())
) )
} }
async fn login(id: Identity) -> HttpResponse { async fn login(req: HttpRequest) -> HttpResponse {
id.remember("user1".to_owned()); Identity::login(&req.extensions(), "user1".to_owned()).unwrap();
HttpResponse::Found() HttpResponse::Found()
.insert_header(("location", "/")) .insert_header(("location", "/"))
.finish() .finish()
} }
async fn logout(id: Identity) -> HttpResponse { async fn logout(id: Identity) -> HttpResponse {
id.forget(); id.logout();
HttpResponse::Found() HttpResponse::Found()
.insert_header(("location", "/")) .insert_header(("location", "/"))
.finish() .finish()
@ -32,13 +37,16 @@ async fn main() -> std::io::Result<()> {
// private key for every project. Anyone with access to the key can generate // private key for every project. Anyone with access to the key can generate
// authentication cookies for any user! // authentication cookies for any user!
let private_key = rand::thread_rng().gen::<[u8; 32]>(); let private_key = rand::thread_rng().gen::<[u8; 32]>();
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.wrap(IdentityService::new( .wrap(IdentityMiddleware::default())
CookieIdentityPolicy::new(&private_key) .wrap(
.name("auth-example") SessionMiddleware::builder(CookieSessionStore::default(), Key::from(&private_key))
.secure(false), .cookie_name("auth-example".to_owned())
)) .cookie_secure(false)
.build(),
)
// enable logger - always register Actix Web Logger middleware last // enable logger - always register Actix Web Logger middleware last
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.service(web::resource("/login").route(web::post().to(login))) .service(web::resource("/login").route(web::post().to(login)))

View File

@ -5,7 +5,8 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
actix-identity = "0.4" actix-identity = "0.5"
actix-session = { version = "0.7", features = ["cookie-session"] }
chrono = { version = "0.4", features = ["serde"] } chrono = { version = "0.4", features = ["serde"] }
derive_more = "0.99.5" derive_more = "0.99.5"

View File

@ -1,7 +1,9 @@
use std::future::{ready, Ready}; use std::future::{ready, Ready};
use actix_identity::Identity; use actix_identity::Identity;
use actix_web::{dev::Payload, web, Error, FromRequest, HttpRequest, HttpResponse}; use actix_web::{
dev::Payload, web, Error, FromRequest, HttpMessage as _, HttpRequest, HttpResponse,
};
use diesel::prelude::*; use diesel::prelude::*;
use serde::Deserialize; use serde::Deserialize;
@ -27,7 +29,7 @@ impl FromRequest for LoggedUser {
fn from_request(req: &HttpRequest, pl: &mut Payload) -> Self::Future { fn from_request(req: &HttpRequest, pl: &mut Payload) -> Self::Future {
if let Ok(identity) = Identity::from_request(req, pl).into_inner() { if let Ok(identity) = Identity::from_request(req, pl).into_inner() {
if let Some(user_json) = identity.identity() { if let Ok(user_json) = identity.id() {
if let Ok(user) = serde_json::from_str(&user_json) { if let Ok(user) = serde_json::from_str(&user_json) {
return ready(Ok(user)); return ready(Ok(user));
} }
@ -39,21 +41,21 @@ impl FromRequest for LoggedUser {
} }
pub async fn logout(id: Identity) -> HttpResponse { pub async fn logout(id: Identity) -> HttpResponse {
id.forget(); id.logout();
HttpResponse::Ok().finish() HttpResponse::NoContent().finish()
} }
pub async fn login( pub async fn login(
req: HttpRequest,
auth_data: web::Json<AuthData>, auth_data: web::Json<AuthData>,
id: Identity,
pool: web::Data<Pool>, pool: web::Data<Pool>,
) -> Result<HttpResponse, actix_web::Error> { ) -> Result<HttpResponse, actix_web::Error> {
let user = web::block(move || query(auth_data.into_inner(), pool)).await??; let user = web::block(move || query(auth_data.into_inner(), pool)).await??;
let user_string = serde_json::to_string(&user).unwrap(); let user_string = serde_json::to_string(&user).unwrap();
id.remember(user_string); Identity::login(&req.extensions(), user_string).unwrap();
Ok(HttpResponse::Ok().finish()) Ok(HttpResponse::NoContent().finish())
} }
pub async fn get_me(logged_user: LoggedUser) -> HttpResponse { pub async fn get_me(logged_user: LoggedUser) -> HttpResponse {

View File

@ -1,8 +1,9 @@
#[macro_use] #[macro_use]
extern crate diesel; extern crate diesel;
use actix_identity::{CookieIdentityPolicy, IdentityService}; use actix_identity::IdentityMiddleware;
use actix_web::{middleware, web, App, HttpServer}; use actix_session::{config::PersistentSession, storage::CookieSessionStore, SessionMiddleware};
use actix_web::{cookie::Key, middleware, web, App, HttpServer};
use diesel::{ use diesel::{
prelude::*, prelude::*,
r2d2::{self, ConnectionManager}, r2d2::{self, ConnectionManager},
@ -39,17 +40,21 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.app_data(web::Data::new(pool.clone())) .app_data(web::Data::new(pool.clone()))
.wrap(IdentityMiddleware::default())
.wrap(
SessionMiddleware::builder(
CookieSessionStore::default(),
Key::from(utils::SECRET_KEY.as_bytes()),
)
.session_lifecycle(PersistentSession::default().session_ttl(Duration::days(1)))
.cookie_name("auth-example".to_owned())
.cookie_secure(false)
.cookie_domain(Some(domain.clone()))
.cookie_path("/".to_owned())
.build(),
)
// enable logger // enable logger
.wrap(middleware::Logger::default()) .wrap(middleware::Logger::default())
.wrap(IdentityService::new(
CookieIdentityPolicy::new(utils::SECRET_KEY.as_bytes())
.name("auth")
.path("/")
.domain(domain.as_str())
.max_age(Duration::days(1))
.secure(false), // this can only be true if you have https
))
.app_data(web::JsonConfig::default().limit(4096))
// everything under '/api/' route // everything under '/api/' route
.service( .service(
web::scope("/api") web::scope("/api")

View File

@ -7,7 +7,7 @@ edition = "2021"
actix-files = "0.6" actix-files = "0.6"
actix-session = { version = "0.7", features = ["cookie-session"] } actix-session = { version = "0.7", features = ["cookie-session"] }
actix-web = "4" actix-web = "4"
actix-web-lab = "0.17" actix-web-lab = "0.18"
dotenv = "0.15" dotenv = "0.15"
env_logger = "0.9" env_logger = "0.9"

View File

@ -10,7 +10,7 @@ env_logger = "0.9"
futures-util = { version = "0.3", default-features = false, features = ["std"] } futures-util = { version = "0.3", default-features = false, features = ["std"] }
log = "0.4" log = "0.4"
r2d2 = "0.8" r2d2 = "0.8"
r2d2_sqlite = "0.20" r2d2_sqlite = "0.20" # 0.21.0 blocked on sqlx sqlite update
rusqlite = "0.27" rusqlite = "0.27" # 0.28.0 blocked on sqlx sqlite update
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"

View File

@ -6,10 +6,10 @@ edition = "2021"
[dependencies] [dependencies]
actix-multipart = "0.4" actix-multipart = "0.4"
actix-web = "4" actix-web = "4"
actix-web-lab = "0.17" actix-web-lab = "0.18"
aws-config = "0.46" aws-config = "0.49"
aws-sdk-s3 = "0.16" aws-sdk-s3 = "0.19"
dotenv = "0.15" dotenv = "0.15"
env_logger = "0.9" env_logger = "0.9"

View File

@ -6,7 +6,7 @@ edition = "2021"
[dependencies] [dependencies]
actix = "0.13" actix = "0.13"
actix-web = "4" actix-web = "4"
actix-web-lab = "0.17" actix-web-lab = "0.18"
actix-cors = "0.6" actix-cors = "0.6"
async-graphql = "4" async-graphql = "4"

View File

@ -5,14 +5,14 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
actix-web-lab = "0.17" actix-web-lab = "0.18"
actix-cors = "0.6" actix-cors = "0.6"
juniper = "0.15" juniper = "0.15"
mysql = "21" mysql = "22"
r2d2 = "0.8" r2d2 = "0.8"
r2d2_mysql = "21" r2d2_mysql = "22"
dotenv = "0.15" dotenv = "0.15"
env_logger = "0.9" env_logger = "0.9"

View File

@ -5,7 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
actix-web-lab = "0.17" actix-web-lab = "0.18"
actix-cors = "0.6" actix-cors = "0.6"
juniper = "0.15" juniper = "0.15"

View File

@ -7,7 +7,7 @@ edition = "2021"
actix-web = { version = "4", features = ["openssl"] } actix-web = { version = "4", features = ["openssl"] }
awc = "3" awc = "3"
clap = { version = "3", features = ["derive"] } clap = { version = "4", features = ["derive"] }
env_logger = "0.9" env_logger = "0.9"
log = "0.4" log = "0.4"
url = "2.2" url = "2.2"

View File

@ -2,7 +2,7 @@ use std::net::ToSocketAddrs;
use actix_web::{error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer}; use actix_web::{error, middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
use awc::Client; use awc::Client;
use clap::StructOpt; use clap::Parser;
use url::Url; use url::Url;
async fn forward( async fn forward(

View File

@ -9,7 +9,7 @@ path = "src/main.rs"
[dependencies] [dependencies]
actix-web = { version = "4", features = ["rustls"] } actix-web = { version = "4", features = ["rustls"] }
actix-web-lab = "0.17" actix-web-lab = "0.18"
actix-files = "0.6" actix-files = "0.6"
env_logger = "0.9" env_logger = "0.9"

View File

@ -12,4 +12,4 @@ futures-util = { version = "0.3.17", default-features = false, features = ["std"
log = "0.4" log = "0.4"
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }
serde_json = "1" serde_json = "1"
validator = { version = "0.15", features = ["derive"] } validator = { version = "0.16", features = ["derive"] }

View File

@ -5,7 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = { version = "4", features = ["rustls"] } actix-web = { version = "4", features = ["rustls"] }
actix-web-lab = "0.17" actix-web-lab = "0.18"
env_logger = "0.9" env_logger = "0.9"
futures-util = { version = "0.3.17", default-features = false, features = ["std"] } futures-util = { version = "0.3.17", default-features = false, features = ["std"] }

View File

@ -4,10 +4,10 @@ version = "1.0.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
actix-protobuf = "0.8" actix-protobuf = "0.9"
actix-web = "4" actix-web = "4"
env_logger = "0.9" env_logger = "0.9"
log = "0.4" log = "0.4"
prost = "0.10" prost = "0.11"
prost-derive = "0.10" prost-derive = "0.11"

View File

@ -5,7 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
actix-web-lab = "0.16.9" actix-web-lab = "0.18"
env_logger = "0.9" env_logger = "0.9"
futures-util = { version = "0.3.17", default-features = false, features = ["std"] } futures-util = { version = "0.3.17", default-features = false, features = ["std"] }
log = "0.4" log = "0.4"

View File

@ -1,7 +1,7 @@
use std::{sync::Arc, time::Duration}; use std::{sync::Arc, time::Duration};
use actix_web::rt::time::interval; use actix_web::rt::time::interval;
use actix_web_lab::sse::{sse, Sse, SseData, SseMessage, SseSender}; use actix_web_lab::sse::{self, ChannelStream, Sse};
use futures_util::future; use futures_util::future;
use parking_lot::Mutex; use parking_lot::Mutex;
@ -11,7 +11,7 @@ pub struct Broadcaster {
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
struct BroadcasterInner { struct BroadcasterInner {
clients: Vec<SseSender>, clients: Vec<sse::Sender>,
} }
impl Broadcaster { impl Broadcaster {
@ -47,7 +47,7 @@ impl Broadcaster {
for client in clients { for client in clients {
if client if client
.send(SseMessage::Comment("ping".into())) .send(sse::Event::Comment("ping".into()))
.await .await
.is_ok() .is_ok()
{ {
@ -59,10 +59,10 @@ impl Broadcaster {
} }
/// Registers client with broadcaster, returning an SSE response body. /// Registers client with broadcaster, returning an SSE response body.
pub async fn new_client(&self) -> Sse { pub async fn new_client(&self) -> Sse<ChannelStream> {
let (tx, rx) = sse(10); let (tx, rx) = sse::channel(10);
tx.send(SseData::new("connected")).await.unwrap(); tx.send(sse::Data::new("connected")).await.unwrap();
self.inner.lock().clients.push(tx); self.inner.lock().clients.push(tx);
@ -73,7 +73,9 @@ impl Broadcaster {
pub async fn broadcast(&self, msg: &str) { pub async fn broadcast(&self, msg: &str) {
let clients = self.inner.lock().clients.clone(); let clients = self.inner.lock().clients.clone();
let send_futures = clients.iter().map(|client| client.send(SseData::new(msg))); let send_futures = clients
.iter()
.map(|client| client.send(sse::Data::new(msg)));
// try to send to all clients, ignoring failures // try to send to all clients, ignoring failures
// disconnected clients will get swept up by `remove_stale_clients` // disconnected clients will get swept up by `remove_stale_clients`

View File

@ -6,7 +6,7 @@ description = "Send a request to the server to shut it down"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
actix-web-lab = "0.17" actix-web-lab = "0.18"
env_logger = "0.9" env_logger = "0.9"
log = "0.4" log = "0.4"

View File

@ -5,7 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
actix-web-lab = "0.17" actix-web-lab = "0.18"
fluent-templates = { version = "0.8", features = ["handlebars"] } fluent-templates = { version = "0.8", features = ["handlebars"] }
handlebars = { version = "4.3", features = ["dir_source"] } handlebars = { version = "4.3", features = ["dir_source"] }
serde = { version = "1", features = ["derive"] } serde = { version = "1", features = ["derive"] }

View File

@ -10,5 +10,5 @@ actix-utils = "3"
env_logger = "0.9" env_logger = "0.9"
log = "0.4" log = "0.4"
minijinja = { version = "0.23", features = ["source"] } minijinja = { version = "0.24", features = ["source"] }
minijinja-autoreload = "0.23" minijinja-autoreload = "0.24"

View File

@ -5,8 +5,8 @@ edition = "2021"
[dependencies] [dependencies]
actix-web = "4" actix-web = "4"
actix-web-lab = "0.17" actix-web-lab = "0.18"
env_logger = "0.9" env_logger = "0.9"
log = "0.4" log = "0.4"
sailfish = "0.4" sailfish = "0.5"