mirror of
https://github.com/actix/examples
synced 2024-11-27 16:02:57 +01:00
use futures-util in more cases
This commit is contained in:
parent
f229251f0d
commit
bad25d643e
7
Cargo.lock
generated
7
Cargo.lock
generated
@ -3118,7 +3118,8 @@ version = "1.0.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-web",
|
"actix-web",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"futures",
|
"futures-util",
|
||||||
|
"log",
|
||||||
"pin-project",
|
"pin-project",
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -3136,7 +3137,9 @@ name = "middleware-http-to-https"
|
|||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"actix-web",
|
"actix-web",
|
||||||
"futures",
|
"env_logger",
|
||||||
|
"futures-util",
|
||||||
|
"log",
|
||||||
"rustls 0.20.3",
|
"rustls 0.20.3",
|
||||||
"rustls-pemfile",
|
"rustls-pemfile",
|
||||||
]
|
]
|
||||||
|
@ -5,6 +5,8 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = { version = "4.0.0-beta.21", features = ["rustls"] }
|
actix-web = { version = "4.0.0-beta.21", features = ["rustls"] }
|
||||||
|
env_logger = "0.9"
|
||||||
|
futures-util = { version = "0.3.7", default-features = false, features = ["std"] }
|
||||||
|
log = "0.4"
|
||||||
rustls = "0.20.2"
|
rustls = "0.20.2"
|
||||||
rustls-pemfile = "0.2.1"
|
rustls-pemfile = "0.2.1"
|
||||||
futures = "0.3"
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use std::{fs::File, io::BufReader};
|
use std::{fs::File, io::BufReader};
|
||||||
|
|
||||||
use actix_web::{dev::Service, get, http, App, HttpResponse, HttpServer};
|
use actix_web::{dev::Service, get, http, App, HttpResponse, HttpServer};
|
||||||
use futures::future::{self, Either, FutureExt};
|
use futures_util::future::{self, Either, FutureExt};
|
||||||
use rustls::{Certificate, PrivateKey, ServerConfig};
|
use rustls::{Certificate, PrivateKey, ServerConfig};
|
||||||
use rustls_pemfile::{certs, pkcs8_private_keys};
|
use rustls_pemfile::{certs, pkcs8_private_keys};
|
||||||
|
|
||||||
@ -12,6 +12,8 @@ async fn index() -> String {
|
|||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||||
|
|
||||||
let cert_file = &mut BufReader::new(File::open("cert.pem").unwrap());
|
let cert_file = &mut BufReader::new(File::open("cert.pem").unwrap());
|
||||||
let key_file = &mut BufReader::new(File::open("key.pem").unwrap());
|
let key_file = &mut BufReader::new(File::open("key.pem").unwrap());
|
||||||
|
|
||||||
@ -32,6 +34,8 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.with_single_cert(cert_chain, keys.remove(0))
|
.with_single_cert(cert_chain, keys.remove(0))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
log::info!("starting HTTP server at http://localhost:8080");
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
.wrap_fn(|sreq, srv| {
|
.wrap_fn(|sreq, srv| {
|
||||||
|
@ -6,5 +6,6 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4.0.0-rc.3"
|
actix-web = "4.0.0-rc.3"
|
||||||
env_logger = "0.9"
|
env_logger = "0.9"
|
||||||
futures = "0.3.7"
|
futures-util = { version = "0.3.7", default-features = false, features = ["std"] }
|
||||||
|
log = "0.4"
|
||||||
pin-project = "1"
|
pin-project = "1"
|
||||||
|
@ -30,3 +30,7 @@ A middleware demonstrating how to read out the outgoing response body.
|
|||||||
|
|
||||||
A minimal middleware demonstrating the sequence of operations in an actix middleware.
|
A minimal middleware demonstrating the sequence of operations in an actix middleware.
|
||||||
There is a second version of the same middleware using `wrap_fn` which shows how easily a middleware can be implemented in actix.
|
There is a second version of the same middleware using `wrap_fn` which shows how easily a middleware can be implemented in actix.
|
||||||
|
|
||||||
|
## See Also
|
||||||
|
|
||||||
|
- The `from_fn` middleware constructor from [`actix-web-lab`](https://crates.io/crates/actix-web-lab).
|
||||||
|
@ -1,19 +1,16 @@
|
|||||||
use actix_web::{dev::Service, web, App, HttpServer};
|
use actix_web::{dev::Service, web, App, HttpServer};
|
||||||
use futures::FutureExt as _;
|
use futures_util::FutureExt as _;
|
||||||
|
|
||||||
#[allow(dead_code)]
|
|
||||||
mod read_request_body;
|
mod read_request_body;
|
||||||
#[allow(dead_code)]
|
|
||||||
mod read_response_body;
|
mod read_response_body;
|
||||||
#[allow(dead_code)]
|
|
||||||
mod redirect;
|
mod redirect;
|
||||||
#[allow(dead_code)]
|
|
||||||
mod simple;
|
mod simple;
|
||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() -> std::io::Result<()> {
|
async fn main() -> std::io::Result<()> {
|
||||||
std::env::set_var("RUST_LOG", "actix_web=debug");
|
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
|
||||||
env_logger::init();
|
|
||||||
|
log::info!("starting HTTP server at http://localhost:8080");
|
||||||
|
|
||||||
HttpServer::new(|| {
|
HttpServer::new(|| {
|
||||||
App::new()
|
App::new()
|
||||||
|
@ -3,11 +3,12 @@ use std::{
|
|||||||
rc::Rc,
|
rc::Rc,
|
||||||
};
|
};
|
||||||
|
|
||||||
use actix_web::dev::{self, Service, Transform};
|
use actix_web::{
|
||||||
use actix_web::web::BytesMut;
|
dev::{self, Service, ServiceRequest, ServiceResponse, Transform},
|
||||||
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error, HttpMessage};
|
web::BytesMut,
|
||||||
use futures::future::LocalBoxFuture;
|
Error, HttpMessage,
|
||||||
use futures::stream::StreamExt;
|
};
|
||||||
|
use futures_util::{future::LocalBoxFuture, stream::StreamExt};
|
||||||
|
|
||||||
pub struct Logging;
|
pub struct Logging;
|
||||||
|
|
||||||
|
@ -1,13 +1,16 @@
|
|||||||
use std::future::Future;
|
use std::{
|
||||||
use std::future::{ready, Ready};
|
future::{ready, Future, Ready},
|
||||||
use std::marker::PhantomData;
|
marker::PhantomData,
|
||||||
use std::pin::Pin;
|
pin::Pin,
|
||||||
use std::task::{Context, Poll};
|
task::{Context, Poll},
|
||||||
|
};
|
||||||
|
|
||||||
use actix_web::body::{BodySize, MessageBody};
|
use actix_web::{
|
||||||
use actix_web::dev::{self, Service, Transform};
|
body::{BodySize, MessageBody},
|
||||||
use actix_web::web::{Bytes, BytesMut};
|
dev::{self, Service, ServiceRequest, ServiceResponse, Transform},
|
||||||
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error};
|
web::{Bytes, BytesMut},
|
||||||
|
Error,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct Logging;
|
pub struct Logging;
|
||||||
|
|
||||||
@ -69,7 +72,7 @@ where
|
|||||||
type Output = Result<ServiceResponse<BodyLogger<B>>, Error>;
|
type Output = Result<ServiceResponse<BodyLogger<B>>, Error>;
|
||||||
|
|
||||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||||
let res = futures::ready!(self.project().fut.poll(cx));
|
let res = futures_util::ready!(self.project().fut.poll(cx));
|
||||||
|
|
||||||
Poll::Ready(res.map(|res| {
|
Poll::Ready(res.map(|res| {
|
||||||
res.map_body(move |_, body| BodyLogger {
|
res.map_body(move |_, body| BodyLogger {
|
||||||
|
@ -4,7 +4,7 @@ use actix_web::body::EitherBody;
|
|||||||
use actix_web::dev::{self, ServiceRequest, ServiceResponse};
|
use actix_web::dev::{self, ServiceRequest, ServiceResponse};
|
||||||
use actix_web::dev::{Service, Transform};
|
use actix_web::dev::{Service, Transform};
|
||||||
use actix_web::{http, Error, HttpResponse};
|
use actix_web::{http, Error, HttpResponse};
|
||||||
use futures::future::LocalBoxFuture;
|
use futures_util::future::LocalBoxFuture;
|
||||||
|
|
||||||
pub struct CheckLogin;
|
pub struct CheckLogin;
|
||||||
|
|
||||||
@ -41,11 +41,12 @@ where
|
|||||||
dev::forward_ready!(service);
|
dev::forward_ready!(service);
|
||||||
|
|
||||||
fn call(&self, request: ServiceRequest) -> Self::Future {
|
fn call(&self, request: ServiceRequest) -> Self::Future {
|
||||||
// We only need to hook into the `start` for this middleware.
|
// Change this to see the change in outcome in the browser.
|
||||||
let is_logged_in = false; // Change this to see the change in outcome in the browser
|
// Usually this boolean would be acquired from a password check or other auth verification.
|
||||||
|
let is_logged_in = false;
|
||||||
|
|
||||||
// Don't forward to /login if we are already on /login
|
// Don't forward to `/login` if we are already on `/login`.
|
||||||
if is_logged_in || request.path() == "/login" {
|
if !is_logged_in && request.path() != "/login" {
|
||||||
let (request, _pl) = request.into_parts();
|
let (request, _pl) = request.into_parts();
|
||||||
|
|
||||||
let response = HttpResponse::Found()
|
let response = HttpResponse::Found()
|
||||||
|
@ -4,7 +4,7 @@ use actix_web::{
|
|||||||
dev::{self, Service, ServiceRequest, ServiceResponse, Transform},
|
dev::{self, Service, ServiceRequest, ServiceResponse, Transform},
|
||||||
Error,
|
Error,
|
||||||
};
|
};
|
||||||
use futures::future::LocalBoxFuture;
|
use futures_util::future::LocalBoxFuture;
|
||||||
|
|
||||||
// There are two steps in middleware processing.
|
// There are two steps in middleware processing.
|
||||||
// 1. Middleware initialization, middleware factory gets called with
|
// 1. Middleware initialization, middleware factory gets called with
|
||||||
|
Loading…
Reference in New Issue
Block a user