Working server
This commit is contained in:
commit
151aa09abe
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
/target
|
||||
Cargo.lock
|
21
Cargo.toml
Normal file
21
Cargo.toml
Normal file
|
@ -0,0 +1,21 @@
|
|||
[package]
|
||||
name = "hyperjive"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[[example]]
|
||||
name = "demo"
|
||||
required-features = ["server"]
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
[features]
|
||||
default = []
|
||||
server = ["hyper/server"]
|
||||
client = ["hyper/client"]
|
||||
|
||||
[dependencies]
|
||||
hyper = { version = "0.14", features = ["http1"] }
|
||||
jive = { git = "https://git.asonix.dog/safe-async/jive", features = [
|
||||
"tokio-io-compat",
|
||||
] }
|
||||
tokio = { version = "1", default-features = false }
|
24
examples/demo.rs
Normal file
24
examples/demo.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
use hyper::{
|
||||
service::{make_service_fn, service_fn},
|
||||
Body, Request, Response,
|
||||
};
|
||||
|
||||
async fn hello(_: Request<Body>) -> Result<Response<Body>, std::convert::Infallible> {
|
||||
Ok(Response::new(Body::from("Hello World!")))
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
jive::block_on(async move {
|
||||
let make_svc =
|
||||
make_service_fn(|_conn| async { Ok::<_, std::convert::Infallible>(service_fn(hello)) });
|
||||
|
||||
let server = hyperjive::server::builder(([0, 0, 0, 0], 8080))
|
||||
.await?
|
||||
.serve(make_svc);
|
||||
|
||||
println!("Listening on port 8080");
|
||||
server.await?;
|
||||
|
||||
Ok(())
|
||||
})?
|
||||
}
|
88
src/lib.rs
Normal file
88
src/lib.rs
Normal file
|
@ -0,0 +1,88 @@
|
|||
#[derive(Clone)]
|
||||
pub struct JiveExecutor;
|
||||
|
||||
impl<Fut> hyper::rt::Executor<Fut> for JiveExecutor
|
||||
where
|
||||
Fut: std::future::Future + Send + 'static,
|
||||
{
|
||||
fn execute(&self, fut: Fut) {
|
||||
jive::spawn(async move {
|
||||
fut.await;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "server")]
|
||||
pub mod server {
|
||||
use std::{
|
||||
pin::Pin,
|
||||
task::{Context, Poll},
|
||||
};
|
||||
|
||||
pub struct JiveIncoming {
|
||||
io: jive::io::Async<jive::net::TcpListener>,
|
||||
}
|
||||
|
||||
pub struct JiveConnection {
|
||||
io: jive::io::Async<jive::net::TcpStream>,
|
||||
}
|
||||
|
||||
impl hyper::server::accept::Accept for JiveIncoming {
|
||||
type Conn = JiveConnection;
|
||||
type Error = std::io::Error;
|
||||
|
||||
fn poll_accept(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
) -> Poll<Option<Result<Self::Conn, Self::Error>>> {
|
||||
self.get_mut()
|
||||
.io
|
||||
.poll_accept(cx)
|
||||
.map(|res| res.map(|(stream, _)| JiveConnection { io: stream }))
|
||||
.map(Some)
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn builder<A: Into<std::net::SocketAddr>>(
|
||||
socket_address: A,
|
||||
) -> std::io::Result<hyper::server::Builder<JiveIncoming, super::JiveExecutor>> {
|
||||
let listener = jive::io::Async::<jive::net::TcpListener>::bind(socket_address).await?;
|
||||
|
||||
let incoming = JiveIncoming { io: listener };
|
||||
|
||||
let http = hyper::server::conn::Http::new().with_executor(super::JiveExecutor);
|
||||
|
||||
Ok(hyper::server::Builder::new(incoming, http))
|
||||
}
|
||||
|
||||
impl tokio::io::AsyncRead for JiveConnection {
|
||||
fn poll_read(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &mut tokio::io::ReadBuf<'_>,
|
||||
) -> Poll<std::io::Result<()>> {
|
||||
tokio::io::AsyncRead::poll_read(Pin::new(&mut self.get_mut().io), cx, buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl tokio::io::AsyncWrite for JiveConnection {
|
||||
fn poll_write(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut Context<'_>,
|
||||
buf: &[u8],
|
||||
) -> Poll<std::io::Result<usize>> {
|
||||
tokio::io::AsyncWrite::poll_write(Pin::new(&mut self.get_mut().io), cx, buf)
|
||||
}
|
||||
|
||||
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
|
||||
tokio::io::AsyncWrite::poll_flush(Pin::new(&mut self.get_mut().io), cx)
|
||||
}
|
||||
|
||||
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<std::io::Result<()>> {
|
||||
tokio::io::AsyncWrite::poll_shutdown(Pin::new(&mut self.get_mut().io), cx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
mod client {}
|
Loading…
Reference in a new issue