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]]
|
[[package]]
|
||||||
name = "ezhttp"
|
name = "ezhttp"
|
||||||
version = "0.1.5"
|
version = "0.1.6"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"rusty_pool",
|
"rusty_pool",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "ezhttp"
|
name = "ezhttp"
|
||||||
version = "0.1.5"
|
version = "0.1.6"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
repository = "https://github.com/MeexReay/ezhttp"
|
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)
|
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
|
```rust
|
||||||
use ezhttp::{Headers, HttpRequest, HttpResponse, HttpServer, HttpServerStarter};
|
use ezhttp::{Headers, HttpRequest, HttpResponse, HttpServer, HttpServerStarter};
|
||||||
use std::time::Duration;
|
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>;
|
pub type Handler<T> = Box<dyn Fn(Arc<Mutex<T>>, TimeoutStream<TcpStream>) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send + Sync>;
|
||||||
|
|
||||||
|
|
||||||
/// Default connection handler
|
/// Default connection handler
|
||||||
/// Turns input to request and response to output
|
/// Turns input to request and response to output
|
||||||
pub async fn handler_connection<S: HttpServer + Send + 'static>(
|
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")]
|
#[cfg(feature = "http_rrs")]
|
||||||
/// HTTP_RRS handler
|
/// HTTP_RRS handler
|
||||||
pub async fn handler_http_rrs<S: HttpServer + Send + 'static>(
|
pub async fn handler_http_rrs<S: HttpServer + Send + 'static>(
|
||||||
|
@ -1,20 +1,11 @@
|
|||||||
use tokio::task::JoinHandle;
|
use tokio::task::JoinHandle;
|
||||||
use tokio::sync::Mutex;
|
|
||||||
use tokio::net::TcpStream;
|
|
||||||
use tokio_io_timeout::TimeoutStream;
|
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
start_server_new_thread,
|
handler_connection, pin_handler, start_server_new_thread, start_server_sync, start_server_with_threadpool, Handler, HttpServer
|
||||||
start_server_sync,
|
|
||||||
start_server_with_threadpool,
|
|
||||||
handler_connection,
|
|
||||||
Handler,
|
|
||||||
HttpServer,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
use std::pin::Pin;
|
|
||||||
use std::{
|
use std::{
|
||||||
error::Error, future::Future, sync::{
|
error::Error, sync::{
|
||||||
atomic::{AtomicBool, Ordering},
|
atomic::{AtomicBool, Ordering},
|
||||||
Arc,
|
Arc,
|
||||||
}, time::Duration
|
}, time::Duration
|
||||||
@ -52,7 +43,7 @@ impl<T: HttpServer + Send + 'static> HttpServerStarter<T> {
|
|||||||
pub fn new(http_server: T, host: &str) -> Self {
|
pub fn new(http_server: T, host: &str) -> Self {
|
||||||
HttpServerStarter {
|
HttpServerStarter {
|
||||||
http_server,
|
http_server,
|
||||||
handler: Box::new(move |a, b| Box::pin(handler_connection(a, b))),
|
handler: pin_handler!(handler_connection),
|
||||||
timeout: None,
|
timeout: None,
|
||||||
host: host.to_string(),
|
host: host.to_string(),
|
||||||
threads: 0,
|
threads: 0,
|
||||||
@ -66,8 +57,8 @@ impl<T: HttpServer + Send + 'static> HttpServerStarter<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Set if http_rrs is supported
|
/// 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 {
|
pub fn handler(mut self, handler: Handler<T>) -> Self {
|
||||||
self.handler = Box::new(move |a, b| Box::pin(handler(a, b)));
|
self.handler = handler;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user