From f8f642183e1943037389c4629c0ce4061140bbb9 Mon Sep 17 00:00:00 2001 From: Valentin Tolmer Date: Sat, 8 May 2021 13:27:48 +0200 Subject: [PATCH] Replace the echo server with an HTTP server --- Cargo.toml | 11 +++---- src/infra/ldap_server.rs | 9 +++--- src/infra/tcp_server.rs | 63 +++++----------------------------------- 3 files changed, 18 insertions(+), 65 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5f40cd5..1ec9b30 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,11 +5,12 @@ name = "lldap" version = "0.1.0" [dependencies] -actix = "0.11.0-beta.3" -actix-rt = "2.1.0" -actix-server = "2.0.0-beta.3" -actix-service = "2.0.0-beta.4" -actix-web = "4.0.0-beta.3" +actix = "0.11.1" +actix-http = "3.0.0-beta.6" +actix-rt = "2.2" +actix-server = "2.0.0-beta.5" +actix-service = "2.0.0" +actix-web = "4.0.0-beta.6" anyhow = "*" clap = "3.0.0-beta.2" chrono = "*" diff --git a/src/infra/ldap_server.rs b/src/infra/ldap_server.rs index c7f3678..ab943fa 100644 --- a/src/infra/ldap_server.rs +++ b/src/infra/ldap_server.rs @@ -3,9 +3,8 @@ use crate::infra::configuration::Configuration; use crate::infra::ldap_handler::LdapHandler; use actix_rt::net::TcpStream; use actix_server::ServerBuilder; -use actix_service::{fn_service, pipeline_factory}; -use anyhow::bail; -use anyhow::Result; +use actix_service::{fn_service, ServiceFactoryExt}; +use anyhow::{bail, Result}; use futures_util::future::ok; use ldap3_server::simple::*; use ldap3_server::LdapCodec; @@ -68,7 +67,7 @@ where let backend_handler = backend_handler.clone(); let ldap_base_dn = ldap_base_dn.clone(); let ldap_user_dn = ldap_user_dn.clone(); - pipeline_factory(fn_service(move |mut stream: TcpStream| { + fn_service(move |mut stream: TcpStream| { let backend_handler = backend_handler.clone(); let ldap_base_dn = ldap_base_dn.clone(); let ldap_user_dn = ldap_user_dn.clone(); @@ -88,7 +87,7 @@ where Ok(stream) } - })) + }) .map_err(|err: anyhow::Error| error!("Service Error: {:?}", err)) // catch .and_then(move |_| { diff --git a/src/infra/tcp_server.rs b/src/infra/tcp_server.rs index 8289e9e..ba124da 100644 --- a/src/infra/tcp_server.rs +++ b/src/infra/tcp_server.rs @@ -1,72 +1,25 @@ use crate::domain::handler::*; use crate::infra::configuration::Configuration; -use actix_rt::net::TcpStream; +use actix_http::HttpServiceBuilder; use actix_server::ServerBuilder; -use actix_service::pipeline_factory; +use actix_service::map_config; +use actix_web::dev::AppConfig; +use actix_web::App; use anyhow::{Context, Result}; -use futures_util::future::ok; -use log::*; -use std::sync::Arc; pub fn build_tcp_server( config: &Configuration, - backend_handler: Backend, + _backend_handler: Backend, server_builder: ServerBuilder, ) -> Result where Backend: BackendHandler + 'static, { - use std::sync::atomic::AtomicUsize; - use std::sync::atomic::Ordering; - use tokio::io::AsyncReadExt; - use tokio::io::AsyncWriteExt; - - let count = Arc::new(AtomicUsize::new(0)); - server_builder .bind("http", ("0.0.0.0", config.http_port), move || { - let count = Arc::clone(&count); - let num2 = Arc::clone(&count); - - pipeline_factory(move |mut stream: TcpStream| { - let count = Arc::clone(&count); - async move { - let num = count.fetch_add(1, Ordering::SeqCst); - let num = num + 1; - - let mut size: usize = 0; - let mut buf = Vec::with_capacity(4096); - - loop { - match stream.read_buf(&mut buf).await { - // end of stream; bail from loop - Ok(0) => break, - - // more bytes to process - Ok(bytes_read) => { - info!("[{}] read {} bytes", num, bytes_read); - stream.write_all(&buf[size..]).await.unwrap(); - size += bytes_read; - } - - // stream error; bail from loop with error - Err(err) => { - error!("Stream Error: {:?}", err); - return Err(()); - } - } - } - - // send data down service pipeline - Ok((buf, size)) - } - }) - .map_err(|err| error!("Service Error: {:?}", err)) - .and_then(move |(_, size)| { - let num = num2.load(Ordering::SeqCst); - info!("[{}] total bytes read: {}", num, size); - ok(size) - }) + HttpServiceBuilder::new() + .finish(map_config(App::new(), |_| AppConfig::default())) + .tcp() }) .with_context(|| { format!(