diff --git a/Cargo.lock b/Cargo.lock index 555c8c73..11f5ae6d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", ] diff --git a/middleware/middleware-http-to-https/Cargo.toml b/middleware/middleware-http-to-https/Cargo.toml index 3bb7437c..eadfd068 100644 --- a/middleware/middleware-http-to-https/Cargo.toml +++ b/middleware/middleware-http-to-https/Cargo.toml @@ -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" diff --git a/middleware/middleware-http-to-https/src/main.rs b/middleware/middleware-http-to-https/src/main.rs index 1ea570e4..3f543741 100644 --- a/middleware/middleware-http-to-https/src/main.rs +++ b/middleware/middleware-http-to-https/src/main.rs @@ -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| { diff --git a/middleware/middleware/Cargo.toml b/middleware/middleware/Cargo.toml index 60c3d113..c3297adf 100644 --- a/middleware/middleware/Cargo.toml +++ b/middleware/middleware/Cargo.toml @@ -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" diff --git a/middleware/middleware/README.md b/middleware/middleware/README.md index 1cbf83ef..b32191ba 100644 --- a/middleware/middleware/README.md +++ b/middleware/middleware/README.md @@ -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). diff --git a/middleware/middleware/src/main.rs b/middleware/middleware/src/main.rs index 66ec56d5..38efb3c1 100644 --- a/middleware/middleware/src/main.rs +++ b/middleware/middleware/src/main.rs @@ -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() diff --git a/middleware/middleware/src/read_request_body.rs b/middleware/middleware/src/read_request_body.rs index a7a82bbc..40d38593 100644 --- a/middleware/middleware/src/read_request_body.rs +++ b/middleware/middleware/src/read_request_body.rs @@ -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; diff --git a/middleware/middleware/src/read_response_body.rs b/middleware/middleware/src/read_response_body.rs index a2450f49..5607dbe6 100644 --- a/middleware/middleware/src/read_response_body.rs +++ b/middleware/middleware/src/read_response_body.rs @@ -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>, Error>; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - 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 { diff --git a/middleware/middleware/src/redirect.rs b/middleware/middleware/src/redirect.rs index 5c25063d..dffd42d9 100644 --- a/middleware/middleware/src/redirect.rs +++ b/middleware/middleware/src/redirect.rs @@ -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() diff --git a/middleware/middleware/src/simple.rs b/middleware/middleware/src/simple.rs index e3afe14d..ad7070c7 100644 --- a/middleware/middleware/src/simple.rs +++ b/middleware/middleware/src/simple.rs @@ -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