Replace the echo server with an HTTP server

This commit is contained in:
Valentin Tolmer 2021-05-08 13:27:48 +02:00
parent d771293a43
commit f8f642183e
3 changed files with 18 additions and 65 deletions

View File

@ -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 = "*"

View File

@ -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 |_| {

View File

@ -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<Backend>(
config: &Configuration,
backend_handler: Backend,
_backend_handler: Backend,
server_builder: ServerBuilder,
) -> Result<ServerBuilder>
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!(