refactor + more async
This commit is contained in:
parent
9cf1a41b52
commit
2c87b173e3
322
Cargo.lock
generated
322
Cargo.lock
generated
@ -2,19 +2,91 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "addr2line"
|
||||
version = "0.22.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678"
|
||||
dependencies = [
|
||||
"gimli",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "adler"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
|
||||
|
||||
[[package]]
|
||||
name = "backtrace"
|
||||
version = "0.3.73"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a"
|
||||
dependencies = [
|
||||
"addr2line",
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"miniz_oxide",
|
||||
"object",
|
||||
"rustc-demangle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.7.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8318a53db07bb3f8dca91a600466bdb3f2eaadeedfdbcf02e1accbad9271ba50"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.1.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "50d2eb3cd3d1bf4529e31c215ee6f93ec5a3d536d9f578f93d9d33ee19562932"
|
||||
dependencies = [
|
||||
"shlex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-channel"
|
||||
version = "0.5.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "33480d6946193aa8033910124896ca395333cae7e2d1113d1fef6c3272217df2"
|
||||
dependencies = [
|
||||
"crossbeam-utils",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossbeam-utils"
|
||||
version = "0.8.20"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80"
|
||||
|
||||
[[package]]
|
||||
name = "ezhttp"
|
||||
version = "0.1.3"
|
||||
dependencies = [
|
||||
"futures",
|
||||
"rusty_pool",
|
||||
"serde_json",
|
||||
"threadpool",
|
||||
"tokio",
|
||||
"urlencoding",
|
||||
]
|
||||
|
||||
@ -107,6 +179,12 @@ dependencies = [
|
||||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gimli"
|
||||
version = "0.29.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd"
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.3.9"
|
||||
@ -125,12 +203,43 @@ version = "0.2.155"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8a240ddb74feaf34a79a7add65a741f3167852fba007066dcac1ca548d89c08"
|
||||
dependencies = [
|
||||
"adler",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mio"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
"wasi",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num_cpus"
|
||||
version = "1.16.0"
|
||||
@ -141,6 +250,38 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "object"
|
||||
version = "0.36.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "27b64972346851a39438c60b341ebc01bba47464ae329e55cf343eb93964efd9"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27"
|
||||
dependencies = [
|
||||
"lock_api",
|
||||
"parking_lot_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot_core"
|
||||
version = "0.9.10"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
"smallvec",
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.14"
|
||||
@ -171,12 +312,46 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
version = "0.5.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2a908a6e00f1fdd0dfd9c0eb08ce85126f6d8bbda50017e74bc4a4b7d4a926a4"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
|
||||
|
||||
[[package]]
|
||||
name = "rusty_pool"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4ed36cdb20de66d89a17ea04b8883fc7a386f2cf877aaedca5005583ce4876ff"
|
||||
dependencies = [
|
||||
"crossbeam-channel",
|
||||
"futures",
|
||||
"futures-channel",
|
||||
"futures-executor",
|
||||
"num_cpus",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
|
||||
|
||||
[[package]]
|
||||
name = "scopeguard"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.203"
|
||||
@ -199,15 +374,31 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.117"
|
||||
version = "1.0.125"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "455182ea6142b14f93f4bc5320a2b31c1f266b66a4a5c858b013302a5d8cbfc3"
|
||||
checksum = "83c8e735a073ccf5be70aa8066aa984eaf2fa000db6c8d0100ae605b366d31ed"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shlex"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "slab"
|
||||
version = "0.4.9"
|
||||
@ -217,6 +408,22 @@ dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.13.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
|
||||
|
||||
[[package]]
|
||||
name = "socket2"
|
||||
version = "0.5.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.66"
|
||||
@ -229,12 +436,32 @@ dependencies = [
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "threadpool"
|
||||
version = "1.8.1"
|
||||
name = "tokio"
|
||||
version = "1.39.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d050e60b33d41c19108b32cea32164033a9013fe3b46cbd4457559bfbf77afaa"
|
||||
checksum = "9babc99b9923bfa4804bd74722ff02c0381021eafa4db9949217e3be8e84fff5"
|
||||
dependencies = [
|
||||
"num_cpus",
|
||||
"backtrace",
|
||||
"bytes",
|
||||
"libc",
|
||||
"mio",
|
||||
"parking_lot",
|
||||
"pin-project-lite",
|
||||
"signal-hook-registry",
|
||||
"socket2",
|
||||
"tokio-macros",
|
||||
"windows-sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-macros"
|
||||
version = "2.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -248,3 +475,82 @@ name = "urlencoding"
|
||||
version = "2.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.52.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
|
||||
dependencies = [
|
||||
"windows-targets",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm",
|
||||
"windows_aarch64_msvc",
|
||||
"windows_i686_gnu",
|
||||
"windows_i686_gnullvm",
|
||||
"windows_i686_msvc",
|
||||
"windows_x86_64_gnu",
|
||||
"windows_x86_64_gnullvm",
|
||||
"windows_x86_64_msvc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.52.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
|
||||
|
10
Cargo.toml
10
Cargo.toml
@ -4,13 +4,13 @@ version = "0.1.3"
|
||||
edition = "2021"
|
||||
|
||||
repository = "https://github.com/MeexReay/ezhttp"
|
||||
description = "lightweight http server"
|
||||
description = "easy http server for small sites"
|
||||
license-file = "LICENSE"
|
||||
readme = "README.md"
|
||||
keywords = ["http", "server", "site"]
|
||||
keywords = ["http", "server", "site", "async"]
|
||||
|
||||
[dependencies]
|
||||
urlencoding = "2.1.3"
|
||||
serde_json = "1.0"
|
||||
futures = "0.3.30"
|
||||
threadpool = "1.8.1"
|
||||
serde_json = "1.0.125"
|
||||
tokio = { version = "1.39.3", features = ["full"] }
|
||||
rusty_pool = "0.7.0"
|
@ -1,5 +1,5 @@
|
||||
# EzHttp
|
||||
Easy http server on rust
|
||||
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)
|
||||
|
||||
|
@ -63,7 +63,8 @@ impl HttpServer for EzSite {
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let site = EzSite::new("<h1>Hello World!</h1>");
|
||||
let host = "localhost:8080";
|
||||
|
||||
@ -71,5 +72,6 @@ fn main() {
|
||||
.timeout(Some(Duration::from_secs(5)))
|
||||
.threads(5)
|
||||
.start_forever()
|
||||
.await
|
||||
.expect("http server error");
|
||||
}
|
||||
|
@ -1,4 +1,3 @@
|
||||
use futures::executor::block_on;
|
||||
use std::{
|
||||
boxed::Box,
|
||||
error::Error,
|
||||
@ -7,12 +6,10 @@ use std::{
|
||||
net::{TcpListener, TcpStream},
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc, Mutex,
|
||||
Arc,
|
||||
},
|
||||
thread,
|
||||
time::Duration,
|
||||
};
|
||||
use threadpool::ThreadPool;
|
||||
|
||||
pub mod error;
|
||||
pub mod headers;
|
||||
@ -24,7 +21,9 @@ pub use error::*;
|
||||
pub use headers::*;
|
||||
pub use request::*;
|
||||
pub use response::*;
|
||||
use rusty_pool::ThreadPool;
|
||||
pub use starter::*;
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
fn read_line(data: &mut impl Read) -> Result<String, HttpError> {
|
||||
let mut bytes = Vec::new();
|
||||
@ -101,25 +100,24 @@ pub trait HttpServer {
|
||||
) -> impl Future<Output = Option<HttpResponse>> + Send;
|
||||
}
|
||||
|
||||
fn start_server_with_threadpool<F, S>(
|
||||
async fn start_server_with_threadpool<S>(
|
||||
server: S,
|
||||
host: &str,
|
||||
timeout: Option<Duration>,
|
||||
threads: usize,
|
||||
handler: F,
|
||||
rrs: bool,
|
||||
running: Arc<AtomicBool>,
|
||||
) -> Result<(), Box<dyn Error>>
|
||||
where
|
||||
F: (Fn(Arc<Mutex<S>>, TcpStream) -> ()) + Send + 'static + Copy,
|
||||
S: HttpServer + Send + 'static,
|
||||
{
|
||||
let threadpool = ThreadPool::new(threads);
|
||||
let threadpool = ThreadPool::new(threads, threads * 10, Duration::from_secs(60));
|
||||
let server = Arc::new(Mutex::new(server));
|
||||
let listener = TcpListener::bind(host)?;
|
||||
|
||||
let host_clone = String::from(host).clone();
|
||||
let server_clone = server.clone();
|
||||
block_on(server_clone.lock().unwrap().on_start(&host_clone));
|
||||
server_clone.lock().await.on_start(&host_clone).await;
|
||||
|
||||
while running.load(Ordering::Acquire) {
|
||||
let (sock, _) = match listener.accept() {
|
||||
@ -133,27 +131,29 @@ where
|
||||
sock.set_write_timeout(timeout).unwrap();
|
||||
|
||||
let now_server = Arc::clone(&server);
|
||||
threadpool.execute(move || {
|
||||
handler(now_server, sock);
|
||||
});
|
||||
|
||||
if !rrs {
|
||||
threadpool.spawn(handle_connection(now_server, sock));
|
||||
} else {
|
||||
threadpool.spawn(handle_connection_rrs(now_server, sock));
|
||||
}
|
||||
}
|
||||
|
||||
threadpool.join();
|
||||
|
||||
block_on(server.lock().unwrap().on_close());
|
||||
server.lock().await.on_close().await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn start_server_new_thread<F, S>(
|
||||
async fn start_server_new_thread<S>(
|
||||
server: S,
|
||||
host: &str,
|
||||
timeout: Option<Duration>,
|
||||
handler: F,
|
||||
rrs: bool,
|
||||
running: Arc<AtomicBool>,
|
||||
) -> Result<(), Box<dyn Error>>
|
||||
where
|
||||
F: (Fn(Arc<Mutex<S>>, TcpStream) -> ()) + Send + 'static + Copy,
|
||||
S: HttpServer + Send + 'static,
|
||||
{
|
||||
let server = Arc::new(Mutex::new(server));
|
||||
@ -161,7 +161,7 @@ where
|
||||
|
||||
let host_clone = String::from(host).clone();
|
||||
let server_clone = server.clone();
|
||||
block_on(server_clone.lock().unwrap().on_start(&host_clone));
|
||||
server_clone.lock().await.on_start(&host_clone).await;
|
||||
|
||||
while running.load(Ordering::Acquire) {
|
||||
let (sock, _) = match listener.accept() {
|
||||
@ -175,25 +175,27 @@ where
|
||||
sock.set_write_timeout(timeout).unwrap();
|
||||
|
||||
let now_server = Arc::clone(&server);
|
||||
thread::spawn(move || {
|
||||
handler(now_server, sock);
|
||||
});
|
||||
|
||||
if !rrs {
|
||||
tokio::spawn(handle_connection(now_server, sock));
|
||||
} else {
|
||||
tokio::spawn(handle_connection_rrs(now_server, sock));
|
||||
}
|
||||
}
|
||||
|
||||
block_on(server.lock().unwrap().on_close());
|
||||
server.lock().await.on_close().await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn start_server_sync<F, S>(
|
||||
async fn start_server_sync<S>(
|
||||
server: S,
|
||||
host: &str,
|
||||
timeout: Option<Duration>,
|
||||
handler: F,
|
||||
rrs: bool,
|
||||
running: Arc<AtomicBool>,
|
||||
) -> Result<(), Box<dyn Error>>
|
||||
where
|
||||
F: (Fn(Arc<Mutex<S>>, TcpStream) -> ()) + Send + 'static + Copy,
|
||||
S: HttpServer + Send + 'static,
|
||||
{
|
||||
let server = Arc::new(Mutex::new(server));
|
||||
@ -201,7 +203,7 @@ where
|
||||
|
||||
let host_clone = String::from(host).clone();
|
||||
let server_clone = server.clone();
|
||||
block_on(server_clone.lock().unwrap().on_start(&host_clone));
|
||||
server_clone.lock().await.on_start(&host_clone).await;
|
||||
|
||||
while running.load(Ordering::Acquire) {
|
||||
let (sock, _) = match listener.accept() {
|
||||
@ -215,16 +217,24 @@ where
|
||||
sock.set_write_timeout(timeout).unwrap();
|
||||
|
||||
let now_server = Arc::clone(&server);
|
||||
handler(now_server, sock);
|
||||
|
||||
if !rrs {
|
||||
handle_connection(now_server, sock).await;
|
||||
} else {
|
||||
handle_connection_rrs(now_server, sock).await;
|
||||
}
|
||||
}
|
||||
|
||||
block_on(server.lock().unwrap().on_close());
|
||||
server.lock().await.on_close().await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_connection<S: HttpServer + Send + 'static>(server: Arc<Mutex<S>>, mut sock: TcpStream) {
|
||||
let addr = sock.peer_addr().unwrap();
|
||||
async fn handle_connection<S: HttpServer + Send + 'static>(
|
||||
server: Arc<Mutex<S>>,
|
||||
mut sock: TcpStream
|
||||
) {
|
||||
let Ok(addr) = sock.peer_addr() else { return; };
|
||||
|
||||
let req = match HttpRequest::read(&mut sock, &addr) {
|
||||
Ok(i) => i,
|
||||
@ -232,16 +242,18 @@ fn handle_connection<S: HttpServer + Send + 'static>(server: Arc<Mutex<S>>, mut
|
||||
return;
|
||||
}
|
||||
};
|
||||
let resp = match block_on(server.lock().unwrap().on_request(&req)) {
|
||||
|
||||
let resp = match server.lock().await.on_request(&req).await {
|
||||
Some(i) => i,
|
||||
None => {
|
||||
return;
|
||||
}
|
||||
};
|
||||
resp.write(&mut sock).unwrap();
|
||||
|
||||
let _ = resp.write(&mut sock);
|
||||
}
|
||||
|
||||
fn handle_connection_rrs<S: HttpServer + Send + 'static>(
|
||||
async fn handle_connection_rrs<S: HttpServer + Send + 'static>(
|
||||
server: Arc<Mutex<S>>,
|
||||
mut sock: TcpStream,
|
||||
) {
|
||||
@ -251,25 +263,27 @@ fn handle_connection_rrs<S: HttpServer + Send + 'static>(
|
||||
return;
|
||||
}
|
||||
};
|
||||
let resp = match block_on(server.lock().unwrap().on_request(&req)) {
|
||||
let resp = match server.lock().await.on_request(&req).await {
|
||||
Some(i) => i,
|
||||
None => {
|
||||
return;
|
||||
}
|
||||
};
|
||||
resp.write(&mut sock).unwrap();
|
||||
let _ = resp.write(&mut sock);
|
||||
}
|
||||
|
||||
/// Start [`HttpServer`](HttpServer) on some host
|
||||
///
|
||||
/// Use [`HttpServerStarter`](HttpServerStarter) to set more options
|
||||
pub fn start_server<S: HttpServer + Send + 'static>(server: S, host: &str) {
|
||||
pub async fn start_server<S: HttpServer + Send + 'static>(
|
||||
server: S,
|
||||
host: &str
|
||||
) -> Result<(), Box<dyn Error>> {
|
||||
start_server_new_thread(
|
||||
server,
|
||||
host,
|
||||
None,
|
||||
handle_connection,
|
||||
false,
|
||||
Arc::new(AtomicBool::new(true)),
|
||||
)
|
||||
.unwrap();
|
||||
).await
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
use tokio::task::JoinHandle;
|
||||
|
||||
use super::{
|
||||
handle_connection, handle_connection_rrs, start_server_new_thread, start_server_sync,
|
||||
start_server_new_thread, start_server_sync,
|
||||
start_server_with_threadpool, HttpServer,
|
||||
};
|
||||
|
||||
@ -9,7 +11,6 @@ use std::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
},
|
||||
thread,
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
@ -24,19 +25,19 @@ pub struct HttpServerStarter<T: HttpServer + Send + 'static> {
|
||||
|
||||
/// Running http server
|
||||
pub struct RunningHttpServer {
|
||||
thread: thread::JoinHandle<()>,
|
||||
thread: JoinHandle<()>,
|
||||
running: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
impl RunningHttpServer {
|
||||
fn new(thread: thread::JoinHandle<()>, running: Arc<AtomicBool>) -> Self {
|
||||
fn new(thread: JoinHandle<()>, running: Arc<AtomicBool>) -> Self {
|
||||
RunningHttpServer { thread, running }
|
||||
}
|
||||
|
||||
/// Stop http server
|
||||
pub fn close(self) {
|
||||
pub fn close(&self) {
|
||||
self.running.store(false, Ordering::Release);
|
||||
self.thread.join().unwrap();
|
||||
self.thread.abort();
|
||||
}
|
||||
}
|
||||
|
||||
@ -114,82 +115,62 @@ impl<T: HttpServer + Send + 'static> HttpServerStarter<T> {
|
||||
}
|
||||
|
||||
/// Start http server forever with options
|
||||
pub fn start_forever(self) -> Result<(), Box<dyn Error>> {
|
||||
let handler = if self.support_http_rrs {
|
||||
move |server, sock| {
|
||||
handle_connection_rrs(server, sock);
|
||||
}
|
||||
} else {
|
||||
move |server, sock| {
|
||||
handle_connection(server, sock);
|
||||
}
|
||||
};
|
||||
|
||||
pub async fn start_forever(self) -> Result<(), Box<dyn Error>> {
|
||||
let running = Arc::new(AtomicBool::new(true));
|
||||
|
||||
if self.threads == 0 {
|
||||
start_server_new_thread(self.http_server, &self.host, self.timeout, handler, running)
|
||||
start_server_new_thread(self.http_server, &self.host, self.timeout, self.support_http_rrs, running).await
|
||||
} else if self.threads == 1 {
|
||||
start_server_sync(self.http_server, &self.host, self.timeout, handler, running)
|
||||
start_server_sync(self.http_server, &self.host, self.timeout, self.support_http_rrs, running).await
|
||||
} else {
|
||||
start_server_with_threadpool(
|
||||
self.http_server,
|
||||
&self.host,
|
||||
self.timeout,
|
||||
self.threads,
|
||||
handler,
|
||||
self.support_http_rrs,
|
||||
running,
|
||||
)
|
||||
).await
|
||||
}
|
||||
}
|
||||
|
||||
/// Start http server with options in new thread
|
||||
pub fn start(self) -> RunningHttpServer {
|
||||
let handler = if self.support_http_rrs {
|
||||
move |server, sock| {
|
||||
handle_connection_rrs(server, sock);
|
||||
}
|
||||
} else {
|
||||
move |server, sock| {
|
||||
handle_connection(server, sock);
|
||||
}
|
||||
};
|
||||
|
||||
let running = Arc::new(AtomicBool::new(true));
|
||||
let running_clone = running.clone();
|
||||
|
||||
let thread = if self.threads == 0 {
|
||||
thread::spawn(move || {
|
||||
tokio::spawn(async move {
|
||||
start_server_new_thread(
|
||||
self.http_server,
|
||||
&self.host,
|
||||
self.timeout,
|
||||
handler,
|
||||
self.support_http_rrs,
|
||||
running_clone,
|
||||
)
|
||||
).await
|
||||
.expect("http server error");
|
||||
})
|
||||
} else if self.threads == 1 {
|
||||
thread::spawn(move || {
|
||||
tokio::spawn(async move {
|
||||
start_server_sync(
|
||||
self.http_server,
|
||||
&self.host,
|
||||
self.timeout,
|
||||
handler,
|
||||
self.support_http_rrs,
|
||||
running_clone,
|
||||
)
|
||||
).await
|
||||
.expect("http server error");
|
||||
})
|
||||
} else {
|
||||
thread::spawn(move || {
|
||||
tokio::spawn(async move {
|
||||
start_server_with_threadpool(
|
||||
self.http_server,
|
||||
&self.host,
|
||||
self.timeout,
|
||||
self.threads,
|
||||
handler,
|
||||
self.support_http_rrs,
|
||||
running_clone,
|
||||
)
|
||||
).await
|
||||
.expect("http server error")
|
||||
})
|
||||
};
|
||||
|
@ -37,7 +37,8 @@ impl HttpServer for EzSite {
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let site = EzSite::new("Hello World!");
|
||||
let host = "localhost:8080";
|
||||
|
||||
@ -45,6 +46,7 @@ fn main() {
|
||||
.timeout(Some(Duration::from_secs(5))) // read & write timeout
|
||||
.threads(5) // threadpool size
|
||||
.start_forever()
|
||||
.await
|
||||
.expect("http server error");
|
||||
|
||||
// ezhttp::start_server(site, host);
|
||||
|
Loading…
x
Reference in New Issue
Block a user