From febaf3073b3134ff6f8cd937fb917f8f9f780677 Mon Sep 17 00:00:00 2001 From: MeexReay Date: Thu, 29 Aug 2024 02:43:49 +0300 Subject: [PATCH] fix handler --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 14 +++++++++++++- src/ezhttp/handler.rs | 9 ++++++++- src/ezhttp/starter.rs | 19 +++++-------------- 5 files changed, 28 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index eb4fa98..d18a14f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -82,7 +82,7 @@ checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" [[package]] name = "ezhttp" -version = "0.1.5" +version = "0.1.6" dependencies = [ "rusty_pool", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index ad43130..6e592ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ezhttp" -version = "0.1.5" +version = "0.1.6" edition = "2021" repository = "https://github.com/MeexReay/ezhttp" diff --git a/README.md b/README.md index e160277..75e24b3 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,19 @@ Easy http server for small sites This library is under developement, so if you found any bugs, please write them to [Issues](https://github.com/MeexReay/ezhttp/issues) -Example: +## Setup + +```toml +ezhttp = "0.1.6" # stable +ezhttp = { git = "https://github.com/MeexReay/ezhttp" } # unstable +``` + +Features: +- http_rrs (adds handler_http_rrs) + +## Examples + +Hello world example: ```rust use ezhttp::{Headers, HttpRequest, HttpResponse, HttpServer, HttpServerStarter}; use std::time::Duration; diff --git a/src/ezhttp/handler.rs b/src/ezhttp/handler.rs index f8dd978..de70a42 100644 --- a/src/ezhttp/handler.rs +++ b/src/ezhttp/handler.rs @@ -9,7 +9,6 @@ use {super::read_line_lf, std::net::{ToSocketAddrs, SocketAddr}}; pub type Handler = Box>, TimeoutStream) -> Pin + Send>> + Send + Sync>; - /// Default connection handler /// Turns input to request and response to output pub async fn handler_connection( @@ -43,6 +42,14 @@ pub async fn handler_connection( } } +macro_rules! pin_handler { + ($handler: expr) => { + Box::new(move |a, b| Box::pin($handler(a, b))) + }; +} + +pub(crate) use pin_handler; + #[cfg(feature = "http_rrs")] /// HTTP_RRS handler pub async fn handler_http_rrs( diff --git a/src/ezhttp/starter.rs b/src/ezhttp/starter.rs index 30c1f4e..ddaf260 100644 --- a/src/ezhttp/starter.rs +++ b/src/ezhttp/starter.rs @@ -1,20 +1,11 @@ use tokio::task::JoinHandle; -use tokio::sync::Mutex; -use tokio::net::TcpStream; -use tokio_io_timeout::TimeoutStream; use super::{ - start_server_new_thread, - start_server_sync, - start_server_with_threadpool, - handler_connection, - Handler, - HttpServer, + handler_connection, pin_handler, start_server_new_thread, start_server_sync, start_server_with_threadpool, Handler, HttpServer }; -use std::pin::Pin; use std::{ - error::Error, future::Future, sync::{ + error::Error, sync::{ atomic::{AtomicBool, Ordering}, Arc, }, time::Duration @@ -52,7 +43,7 @@ impl HttpServerStarter { pub fn new(http_server: T, host: &str) -> Self { HttpServerStarter { http_server, - handler: Box::new(move |a, b| Box::pin(handler_connection(a, b))), + handler: pin_handler!(handler_connection), timeout: None, host: host.to_string(), threads: 0, @@ -66,8 +57,8 @@ impl HttpServerStarter { } /// Set if http_rrs is supported - pub fn handler(mut self, handler: impl Fn(Arc>, TimeoutStream) -> Pin + Send>> + Send + Sync + 'static) -> Self { - self.handler = Box::new(move |a, b| Box::pin(handler(a, b))); + pub fn handler(mut self, handler: Handler) -> Self { + self.handler = handler; self }