mirror of
https://github.com/actix/examples
synced 2024-11-23 14:31:07 +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 = [
|
||||
"actix-web",
|
||||
"env_logger",
|
||||
"futures",
|
||||
"futures-util",
|
||||
"log",
|
||||
"pin-project",
|
||||
]
|
||||
|
||||
@ -3136,7 +3137,9 @@ name = "middleware-http-to-https"
|
||||
version = "1.0.0"
|
||||
dependencies = [
|
||||
"actix-web",
|
||||
"futures",
|
||||
"env_logger",
|
||||
"futures-util",
|
||||
"log",
|
||||
"rustls 0.20.3",
|
||||
"rustls-pemfile",
|
||||
]
|
||||
|
@ -5,6 +5,8 @@ edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
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-pemfile = "0.2.1"
|
||||
futures = "0.3"
|
||||
|
@ -1,7 +1,7 @@
|
||||
use std::{fs::File, io::BufReader};
|
||||
|
||||
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_pemfile::{certs, pkcs8_private_keys};
|
||||
|
||||
@ -12,6 +12,8 @@ async fn index() -> String {
|
||||
|
||||
#[actix_web::main]
|
||||
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 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))
|
||||
.unwrap();
|
||||
|
||||
log::info!("starting HTTP server at http://localhost:8080");
|
||||
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.wrap_fn(|sreq, srv| {
|
||||
|
@ -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"
|
||||
|
@ -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).
|
||||
|
@ -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()
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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 {
|
||||
|
@ -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()
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user