Skip to content

Commit 902a1ca

Browse files
committed
Add an example and more documentation
1 parent 350e3fe commit 902a1ca

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

examples/server.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#![deny(warnings, clippy::all)]
2+
3+
use conduit::{Handler, Request, Response};
4+
use conduit_hyper::Server;
5+
use conduit_router::RouteBuilder;
6+
use futures::Future;
7+
use tokio::runtime;
8+
9+
use std::collections::HashMap;
10+
use std::io::{Cursor, Error};
11+
use std::thread::sleep;
12+
13+
fn main() {
14+
let app = build_conduit_handler();
15+
let addr = ([127, 0, 0, 1], 12345).into();
16+
let server = Server::bind(&addr, app).map_err(|e| {
17+
eprintln!("server error: {}", e);
18+
});
19+
20+
let mut rt = runtime::Builder::new()
21+
// Set the max number of concurrent requests (tokio defaults to 100)
22+
.blocking_threads(2)
23+
.build()
24+
.unwrap();
25+
rt.spawn(server);
26+
rt.shutdown_on_idle().wait().unwrap();
27+
}
28+
29+
fn build_conduit_handler() -> impl Handler {
30+
let mut router = RouteBuilder::new();
31+
router.get("/", endpoint);
32+
router.get("/panic", panic);
33+
router
34+
}
35+
36+
fn endpoint(_: &mut dyn Request) -> Result<Response, Error> {
37+
let body = "Hello world!";
38+
39+
sleep(std::time::Duration::from_secs(2));
40+
41+
let mut headers = HashMap::new();
42+
headers.insert(
43+
"Content-Type".to_string(),
44+
vec!["text/plain; charset=utf-8".to_string()],
45+
);
46+
headers.insert("Content-Length".to_string(), vec![body.len().to_string()]);
47+
Ok(Response {
48+
status: (200, "OK"),
49+
headers,
50+
body: Box::new(Cursor::new(body)),
51+
})
52+
}
53+
54+
fn panic(_: &mut dyn Request) -> Result<Response, Error> {
55+
// For now, connection is immediately closed
56+
panic!("message");
57+
}

src/lib.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,61 @@
11
#![deny(warnings, clippy::all, missing_debug_implementations)]
22

3+
//! A wrapper for integrating `hyper 0.12` with a `conduit 0.8` blocking application stack.
4+
//!
5+
//! A `conduit::Handler` is allowed to block so the `Server` must be spawned on the (default)
6+
//! multi-threaded `Runtime` which allows (by default) 100 concurrent blocking threads. Any excess
7+
//! requests will asynchronously wait for an available blocking thread.
8+
//!
9+
//! # Examples
10+
//!
11+
//! Try out the example with `cargo run --example server`.
12+
//!
13+
//! Typical usage:
14+
//!
15+
//! ```no_run
16+
//! use conduit::Handler;
17+
//! use conduit_hyper::Server;
18+
//! use futures::Future;
19+
//! use tokio::runtime::Runtime;
20+
//!
21+
//! fn main() {
22+
//! let a = ();
23+
//! let app = build_conduit_handler();
24+
//! let addr = ([127, 0, 0, 1], 12345).into();
25+
//! let server = Server::bind(&addr, app).map_err(|e| {
26+
//! eprintln!("server error: {}", e);
27+
//! });
28+
//!
29+
//! let mut rt = Runtime::new().unwrap();
30+
//! rt.spawn(server);
31+
//! rt.shutdown_on_idle().wait().unwrap();
32+
//! }
33+
//!
34+
//! fn build_conduit_handler() -> impl Handler {
35+
//! // ...
36+
//! # Endpoint()
37+
//! }
38+
//! #
39+
//! # use std::collections::HashMap;
40+
//! # use std::error::Error;
41+
//! # use std::io::Cursor;
42+
//! #
43+
//! # use conduit::{Request, Response};
44+
//! #
45+
//! # struct Endpoint();
46+
//! #
47+
//! # impl Handler for Endpoint {
48+
//! # fn call(&self, _: &mut dyn Request) -> Result<Response, Box<dyn Error + Send>> {
49+
//! # let body = "";
50+
//! # Ok(Response {
51+
//! # status: (200, "OK"),
52+
//! # headers: HashMap::new(),
53+
//! # body: Box::new(Cursor::new(body)),
54+
//! # })
55+
//! # }
56+
//! # }
57+
//! ```
58+
359
#[cfg(test)]
460
mod tests;
561

0 commit comments

Comments
 (0)