1
0
mirror of https://github.com/actix/examples synced 2025-06-28 18:00:37 +02:00

use futures-util in more cases

This commit is contained in:
Rob Ede
2022-02-22 12:12:17 +00:00
parent f229251f0d
commit bad25d643e
10 changed files with 49 additions and 33 deletions

View File

@ -6,5 +6,6 @@ edition = "2021"
[dependencies]
actix-web = "4.0.0-rc.3"
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"

View File

@ -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.
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).

View File

@ -1,19 +1,16 @@
use actix_web::{dev::Service, web, App, HttpServer};
use futures::FutureExt as _;
use futures_util::FutureExt as _;
#[allow(dead_code)]
mod read_request_body;
#[allow(dead_code)]
mod read_response_body;
#[allow(dead_code)]
mod redirect;
#[allow(dead_code)]
mod simple;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=debug");
env_logger::init();
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
log::info!("starting HTTP server at http://localhost:8080");
HttpServer::new(|| {
App::new()

View File

@ -3,11 +3,12 @@ use std::{
rc::Rc,
};
use actix_web::dev::{self, Service, Transform};
use actix_web::web::BytesMut;
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error, HttpMessage};
use futures::future::LocalBoxFuture;
use futures::stream::StreamExt;
use actix_web::{
dev::{self, Service, ServiceRequest, ServiceResponse, Transform},
web::BytesMut,
Error, HttpMessage,
};
use futures_util::{future::LocalBoxFuture, stream::StreamExt};
pub struct Logging;

View File

@ -1,13 +1,16 @@
use std::future::Future;
use std::future::{ready, Ready};
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::{
future::{ready, Future, Ready},
marker::PhantomData,
pin::Pin,
task::{Context, Poll},
};
use actix_web::body::{BodySize, MessageBody};
use actix_web::dev::{self, Service, Transform};
use actix_web::web::{Bytes, BytesMut};
use actix_web::{dev::ServiceRequest, dev::ServiceResponse, Error};
use actix_web::{
body::{BodySize, MessageBody},
dev::{self, Service, ServiceRequest, ServiceResponse, Transform},
web::{Bytes, BytesMut},
Error,
};
pub struct Logging;
@ -69,7 +72,7 @@ where
type Output = Result<ServiceResponse<BodyLogger<B>>, Error>;
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| {
res.map_body(move |_, body| BodyLogger {

View File

@ -4,7 +4,7 @@ use actix_web::body::EitherBody;
use actix_web::dev::{self, ServiceRequest, ServiceResponse};
use actix_web::dev::{Service, Transform};
use actix_web::{http, Error, HttpResponse};
use futures::future::LocalBoxFuture;
use futures_util::future::LocalBoxFuture;
pub struct CheckLogin;
@ -41,11 +41,12 @@ where
dev::forward_ready!(service);
fn call(&self, request: ServiceRequest) -> Self::Future {
// We only need to hook into the `start` for this middleware.
let is_logged_in = false; // Change this to see the change in outcome in the browser
// 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
if is_logged_in || request.path() == "/login" {
// Don't forward to `/login` if we are already on `/login`.
if !is_logged_in && request.path() != "/login" {
let (request, _pl) = request.into_parts();
let response = HttpResponse::Found()

View File

@ -4,7 +4,7 @@ use actix_web::{
dev::{self, Service, ServiceRequest, ServiceResponse, Transform},
Error,
};
use futures::future::LocalBoxFuture;
use futures_util::future::LocalBoxFuture;
// There are two steps in middleware processing.
// 1. Middleware initialization, middleware factory gets called with