fix handler
This commit is contained in:
parent
b8f9f9b3ca
commit
febaf3073b
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -82,7 +82,7 @@ checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
|
||||
|
||||
[[package]]
|
||||
name = "ezhttp"
|
||||
version = "0.1.5"
|
||||
version = "0.1.6"
|
||||
dependencies = [
|
||||
"rusty_pool",
|
||||
"serde_json",
|
||||
|
@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "ezhttp"
|
||||
version = "0.1.5"
|
||||
version = "0.1.6"
|
||||
edition = "2021"
|
||||
|
||||
repository = "https://github.com/MeexReay/ezhttp"
|
||||
|
14
README.md
14
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;
|
||||
|
@ -9,7 +9,6 @@ use {super::read_line_lf, std::net::{ToSocketAddrs, SocketAddr}};
|
||||
|
||||
pub type Handler<T> = Box<dyn Fn(Arc<Mutex<T>>, TimeoutStream<TcpStream>) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>;
|
||||
|
||||
|
||||
/// Default connection handler
|
||||
/// Turns input to request and response to output
|
||||
pub async fn handler_connection<S: HttpServer + Send + 'static>(
|
||||
@ -43,6 +42,14 @@ pub async fn handler_connection<S: HttpServer + Send + 'static>(
|
||||
}
|
||||
}
|
||||
|
||||
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<S: HttpServer + Send + 'static>(
|
||||
|
@ -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<T: HttpServer + Send + 'static> HttpServerStarter<T> {
|
||||
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<T: HttpServer + Send + 'static> HttpServerStarter<T> {
|
||||
}
|
||||
|
||||
/// Set if http_rrs is supported
|
||||
pub fn handler(mut self, handler: impl Fn(Arc<Mutex<T>>, TimeoutStream<TcpStream>) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync + 'static) -> Self {
|
||||
self.handler = Box::new(move |a, b| Box::pin(handler(a, b)));
|
||||
pub fn handler(mut self, handler: Handler<T>) -> Self {
|
||||
self.handler = handler;
|
||||
self
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user