summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorihc童鞋@提不起劲 <[email protected]>2021-12-06 20:44:15 +0800
committerGitHub <[email protected]>2021-12-06 20:44:15 +0800
commit78cd7b389386e1cd0f0745cfc8db311faa6c0504 (patch)
tree177868cc58c8a9b913f8bfa2ba367c94dda9c954 /examples
parent7223e0d1cf5ca6c4287be0334b393fbeca8311b5 (diff)
add hyper example and fix bug of monoio-compat (#5)
Diffstat (limited to 'examples')
-rw-r--r--examples/Cargo.toml13
-rw-r--r--examples/hyper.rs60
2 files changed, 71 insertions, 2 deletions
diff --git a/examples/Cargo.toml b/examples/Cargo.toml
index 2834170..f3479be 100644
--- a/examples/Cargo.toml
+++ b/examples/Cargo.toml
@@ -8,11 +8,16 @@ version = "0.0.0"
# [dependencies] instead. In additional, if you want to know how runtime
# works, you can enable "debug" feature.
[dev-dependencies]
-monoio = {path = "../monoio", features=["sync", "utils", "bytes", "macros"]}
+monoio = {path = "../monoio", features = ["sync", "utils", "bytes", "macros"]}
+# For hyper example
+hyper = {version = "0.14", features = ["full"]}
+monoio-compat = {path = "../monoio-compat"}
+tokio = {version = "1", default-features = false, features = ["io-util"]}
+
+futures = "0.3"
local-sync = "0.0.5"
pin-project-lite = "0.2"
-futures = "0.3"
[[example]]
name = "builder"
@@ -53,3 +58,7 @@ path = "channel.rs"
[[example]]
name = "proxy"
path = "proxy.rs"
+
+[[example]]
+name = "hyper"
+path = "hyper.rs"
diff --git a/examples/hyper.rs b/examples/hyper.rs
new file mode 100644
index 0000000..5d7b8d8
--- /dev/null
+++ b/examples/hyper.rs
@@ -0,0 +1,60 @@
+/// HTTP server example with hyper in compatible mode.
+///
+/// After running this example, you can open http://localhost:23300
+/// and http://localhost:23300/monoio in your browser or curl it.
+use futures::Future;
+use hyper::{server::conn::Http, service::service_fn};
+use monoio::net::TcpListener;
+use monoio_compat::TcpStreamCompat;
+use std::net::SocketAddr;
+
+#[derive(Clone)]
+struct HyperExecutor;
+
+impl<F> hyper::rt::Executor<F> for HyperExecutor
+where
+ F: Future + 'static,
+ F::Output: 'static,
+{
+ fn execute(&self, fut: F) {
+ monoio::spawn(fut);
+ }
+}
+
+pub(crate) async fn serve_http<S, F, R, A>(addr: A, service: S) -> std::io::Result<()>
+where
+ S: FnMut(Request<Body>) -> F + 'static + Copy,
+ F: Future<Output = Result<Response<Body>, R>> + 'static,
+ R: std::error::Error + 'static + Send + Sync,
+ A: Into<SocketAddr>,
+{
+ let listener = TcpListener::bind(addr.into())?;
+ loop {
+ let (stream, _) = listener.accept().await?;
+ monoio::spawn(
+ Http::new()
+ .with_executor(HyperExecutor)
+ .serve_connection(TcpStreamCompat::from(stream), service_fn(service)),
+ );
+ }
+}
+
+use hyper::{Body, Method, Request, Response, StatusCode};
+
+async fn hyper_handler(req: Request<Body>) -> Result<Response<Body>, std::convert::Infallible> {
+ match (req.method(), req.uri().path()) {
+ (&Method::GET, "/") => Ok(Response::new(Body::from("Hello World!"))),
+ (&Method::GET, "/monoio") => Ok(Response::new(Body::from("Hello Monoio!"))),
+ _ => Ok(Response::builder()
+ .status(StatusCode::NOT_FOUND)
+ .body(Body::from("404 not found"))
+ .unwrap()),
+ }
+}
+
+#[monoio::main]
+async fn main() {
+ println!("Running http server on 0.0.0.0:23300 with single thread");
+ let _ = serve_http(([0, 0, 0, 0], 23300), hyper_handler).await;
+ println!("Http server stopped");
+}