1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
//! Breadboard is a simple JSON API request router for [hyper].
//!
//! Like [electronics breadboards], Breadboard is well-suited to prototyping your API. Also like
//! electronics breadboards, its functionality is limited and if you bend it too hard it will snap.
//!
//! To create a hyper [`Service`], create a `Breadboard` with [`Breadboard::new`], use methods like
//! [`Breadboard::get`] to add handlers, then use it in [`hyper::server::Builder::serve`].
//!
//! [hyper]: ../hyper/index.html
//! [electronics breadboards]: https://en.wikipedia.org/wiki/Breadboard
//! [`Service`]: ../hyper/service/trait.Service.html
//! [`Breadboard::new`]: struct.Breadboard.html#method.new
//! [`Breadboard::get`]: struct.Breadboard.html#method.get
//! [`hyper::server::Builder::serve`]: ../hyper/server/struct.Builder.html#method.serve
//!
//! ## Quick start
//!
//! ```
//! extern crate breadboard;
//! extern crate hyper;
//!
//! use breadboard::Breadboard;
//! use hyper::server::Server;
//! use hyper::{Request, Response};
//! use std::string::ParseError; // waiting for `!`
//!
//! fn hello(_: Request<()>) -> Result<Response<&'static str>, ParseError> {
//! Ok(Response::new("Hello, world!"))
//! }
//!
//! let board = Breadboard::new().get("/", hello);
//! let server = Server::bind(&"127.0.0.1:3000".parse().unwrap())
//! .serve(board);
//! // hyper::rt::run(server.map_err(|e| eprintln!("server error: {}", e)));
//! ```
//!
//! You can also use closures as handlers:
//!
//! ```
//! # extern crate breadboard;
//! # extern crate hyper;
//! # use breadboard::Breadboard;
//! # use hyper::server::Server;
//! # use hyper::{Request, Response};
//! # use std::string::ParseError; // waiting for `!`
//! let board = Breadboard::new().get(
//! "/",
//! |_: Request<()>| -> Result<Response<&'static str>, ParseError> {
//! Ok(Response::new("Hello, world!"))
//! },
//! );
//! let server = Server::bind(&"127.0.0.1:3000".parse().unwrap())
//! .serve(board);
//! ```
//!
//! ## Handlers that return Futures
//!
//! Handlers return a type that implement [`IntoFuture`]. This includes `Result`, as well as all
//! types that implement [`Future`].
//!
//! This example handler makes an HTTP request and responds with the response it receives. It
//! returns a `Future` that resolves after the request completes.
//!
//! ```
//! extern crate breadboard;
//! extern crate hyper;
//!
//! use breadboard::Breadboard;
//! use hyper::client::Client;
//! use hyper::rt::{Future, Stream};
//! use hyper::server::Server;
//! use hyper::{Body, Request, Response};
//!
//! fn proxy(_: Request<()>) -> impl Future<Item = Response<Vec<u8>>, Error = hyper::Error> {
//! let client: Client<_, Body> = Client::builder().build_http();
//! client
//! .get("http://icanhazip.com/".parse().unwrap())
//! .and_then(|response| {
//! let (parts, body) = response.into_parts();
//! body.concat2()
//! .map(|chunk| Response::from_parts(parts, chunk.into_bytes().to_vec()))
//! })
//! }
//!
//! let board = Breadboard::new().get("/", proxy);
//! let server = Server::bind(&"127.0.0.1:3000".parse().unwrap())
//! .serve(board);
//! ```
//!
//! [`IntoFuture`]: ../futures/future/trait.IntoFuture.html
//! [`Future`]: ../futures/future/trait.Future.html
//!
//! ## Deserialize and Serialize
//!
//! The real power of Breadboard is that request and response bodies can be anything as long as
//! they implement Deserialize and Serialize, respectively.
//!
//! ```
//! extern crate breadboard;
//! extern crate hyper;
//! extern crate serde;
//! #[macro_use]
//! extern crate serde_derive;
//!
//! use breadboard::Breadboard;
//! use hyper::client::Client;
//! use hyper::rt::{Future, Stream};
//! use hyper::server::Server;
//! use hyper::{Body, Request, Response};
//! use std::string::ParseError; // waiting for `!`
//!
//! #[derive(Debug, Deserialize)]
//! struct Message {
//! message: String,
//! }
//!
//! fn message(request: Request<Message>) -> Result<Response<String>, ParseError> {
//! Ok(Response::new(request.into_body().message))
//! }
//!
//! # fn main() {
//! let board = Breadboard::new().post("/", message);
//! let server = Server::bind(&"127.0.0.1:3000".parse().unwrap())
//! .serve(board);
//! # }
//! ```
#![warn(
future_incompatible,
rust_2018_compatibility,
rust_2018_idioms,
)]
#![deny(
missing_docs,
unused,
unused_extern_crates,
missing_copy_implementations,
missing_debug_implementations
)]
#![cfg_attr(feature = "cargo-clippy", warn(clippy_pedantic))]
extern crate futures;
extern crate hyper;
extern crate regex;
extern crate serde;
extern crate serde_json;
use futures::future::{Future, FutureResult, IntoFuture};
use futures::Stream;
use hyper::header::{HeaderValue, CONTENT_TYPE};
use hyper::{Body, Method, Request, Response};
use regex::RegexSet;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt::{self, Debug};
use std::sync::Arc;
macro_rules! status {
($code:expr) => {
Response::builder()
.status($code)
.body(Body::empty())
.expect("cannot fail")
};
}
mod error;
pub use crate::error::{BuildError, RequestError};
type HandlerFuture = Box<dyn Future<Item = Response<Vec<u8>>, Error = RequestError> + Send>;
type Handler = Arc<dyn Fn(Request<&[u8]>) -> HandlerFuture + Send + Sync>;
fn convert_route(route: &str) -> Result<String, BuildError> {
Ok(if route.starts_with('/') {
format!("^{}$", regex::escape(route))
} else {
format!("^/{}$", regex::escape(route))
})
}
// If the body is empty, serde_json will throw an EOF. Appease it by trying to deserialize `null`.
fn from_slice<T>(buf: &[u8]) -> Result<T, RequestError>
where
for<'de> T: Deserialize<'de>,
{
serde_json::from_slice(if buf.is_empty() { b"null" } else { buf }).map_err(RequestError::De)
}
struct RouteList<'a>(&'a Vec<(String, Handler)>);
impl<'a> Debug for RouteList<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list()
.entries(self.0.iter().map(|(s, _)| s))
.finish()
}
}
/// A router builder.
///
/// Register your routes, then use hyper's [`NewService`] trait to create the service.
///
/// See the crate-level documentation for examples.
///
/// ## Anatomy of a handler
///
/// A handler is a function that takes a single argument, [`Request<T>`]. `T` must implement
/// [`serde::Deserialize`].
///
/// The handler returns a type that implements [`IntoFuture`]. The future's [`Item`] is a
/// [`Response<T>`], where `T` must implement [`serde::Serialize`]. The future's
/// [`Error`][future-error] can be anything that implements [`std::error::Error`], [`Send`], and
/// [`Sync`].
///
/// If you aren't doing any work in your handler that requires use of futures, you can simply
/// return a [`Result`], as `Result` implements `IntoFuture`.
///
/// [`NewService`]: ../hyper/service/trait.NewService.html
/// [`Request<T>`]: ../hyper/struct.Request.html
/// [`serde::Deserialize`]: ../serde/trait.Deserialize.html
/// [`IntoFuture`]: ../futures/future/trait.IntoFuture.html
/// [`Item`]: ../futures/future/trait.Future.html#associatedtype.Item
/// [`Response<T>`]: ../hyper/struct.Response.html
/// [`serde::Serialize`]: ../serde/trait.Serialize.html
/// [future-error]: ../futures/future/trait.Future.html#associatedtype.Error
/// [`std::error::Error`]: https://doc.rust-lang.org/nightly/std/error/trait.Error.html
/// [`Send`]: https://doc.rust-lang.org/nightly/std/marker/trait.Send.html
/// [`Sync`]: https://doc.rust-lang.org/nightly/std/marker/trait.Sync.html
/// [`Result`]: https://doc.rust-lang.org/nightly/std/result/enum.Result.html
/// [`String`]: https://doc.rust-lang.org/nightly/std/string/struct.String.html
/// [`!`]: https://doc.rust-lang.org/nightly/std/primitive.never.html
#[derive(Default)]
pub struct Breadboard {
routes: HashMap<Method, Vec<(String, Handler)>>,
}
impl Debug for Breadboard {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map()
.entries(self.routes.iter().map(|(method, v)| (method, RouteList(v))))
.finish()
}
}
macro_rules! method_shortcut {
($lower:ident, $upper:ident, $upper_str:expr) => (
#[doc = "Add a handler for "]
#[doc = $upper_str]
#[doc = " requests for a route."]
pub fn $lower<A, F, R, D, S, E>(self, route: A, handler: F) -> Self
where
A: AsRef<str>,
F: Fn(Request<D>) -> R + Send + Sync + 'static,
R: IntoFuture<Item = Response<S>, Error = E> + 'static,
R::Future: Send,
for<'de> D: Deserialize<'de> + Send + 'static,
S: Serialize + 'static,
E: Into<Box<dyn std::error::Error + Send + Sync>>,
{
self.route(Method::$upper, route, handler)
}
);
($lower:ident, $upper:tt) => (
method_shortcut!($lower, $upper, stringify!($upper));
);
}
impl Breadboard {
/// Start a new `Breadboard`.
pub fn new() -> Self {
Self::default()
}
/// Add a handler for a route with the given method.
pub fn route<A, F, R, D, S, E>(self, method: Method, route: A, handler: F) -> Self
where
A: AsRef<str>,
F: Fn(Request<D>) -> R + Send + Sync + 'static,
R: IntoFuture<Item = Response<S>, Error = E> + 'static,
R::Future: Send,
for<'de> D: Deserialize<'de> + Send + 'static,
S: Serialize + 'static,
E: Into<Box<dyn std::error::Error + Send + Sync>>,
{
let mut s = self;
s.routes.entry(method).or_insert_with(Vec::new).push((
route.as_ref().to_owned(),
Arc::new(move |request| {
let (parts, body) = request.into_parts();
let request = match from_slice(body) {
Ok(buf) => Request::from_parts(parts, buf),
Err(err) => return Box::new(FutureResult::from(Err(err))),
};
Box::new(
handler(request)
.into_future()
.map_err(|err| RequestError::Handler(err.into()))
.and_then(|response| {
let (res_parts, res_body) = response.into_parts();
let mut response = Response::from_parts(
res_parts,
serde_json::to_vec(&res_body).map_err(RequestError::Ser)?,
);
response
.headers_mut()
.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
Ok(response)
}),
)
}),
));
s
}
method_shortcut!(get, GET);
method_shortcut!(post, POST);
method_shortcut!(put, PUT);
method_shortcut!(delete, DELETE);
method_shortcut!(head, HEAD);
method_shortcut!(options, OPTIONS);
method_shortcut!(connect, CONNECT);
method_shortcut!(patch, PATCH);
method_shortcut!(trace, TRACE);
/// fixme
pub fn build(&self) -> Result<Service, BuildError> {
Ok(Service {
routes: self
.routes
.iter()
.map(|(method, v)| {
let set = RegexSet::new(
v.iter()
.map(|x| convert_route(&x.0))
.collect::<Result<Vec<_>, _>>()?,
).expect("breadboard should not generate invalid regexes");
Ok((
method.clone(),
(set, v.iter().map(|x| x.1.clone()).collect()),
))
}).collect::<Result<HashMap<_, _>, _>>()?,
})
}
}
impl hyper::service::NewService for Breadboard {
type ReqBody = Body;
type ResBody = Body;
type Error = RequestError;
type Service = Service;
type Future = FutureResult<Service, BuildError>;
type InitError = BuildError;
fn new_service(&self) -> Self::Future {
self.build().into()
}
}
struct ServiceRoutes<'a>(&'a HashMap<Method, (RegexSet, Vec<Handler>)>);
impl<'a> Debug for ServiceRoutes<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_map()
.entries(self.0.iter().map(|(method, v)| (method, &v.0)))
.finish()
}
}
/// The hyper [`Service`].
///
/// [`Service`]: ../hyper/service/trait.Service.html
pub struct Service {
routes: HashMap<Method, (RegexSet, Vec<Handler>)>,
}
impl Debug for Service {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Service")
.field("routes", &ServiceRoutes(&self.routes))
.finish()
}
}
impl hyper::service::Service for Service {
type ReqBody = Body;
type ResBody = Body;
type Error = RequestError;
type Future = Box<dyn Future<Item = Response<Body>, Error = RequestError> + Send>;
fn call(&mut self, request: Request<Self::ReqBody>) -> Self::Future {
let handler = match self.routes.get(request.method()).and_then(|(set, v)| {
set.matches(request.uri().path())
.into_iter()
.next()
.and_then(|i| v.get(i))
}) {
Some(h) => h.clone(),
None => return Box::new(FutureResult::from(Ok(status!(404)))),
};
let (parts, body) = request.into_parts();
Box::new(
body.concat2()
.map_err(RequestError::Hyper)
.and_then(move |chunk| handler(Request::from_parts(parts, &chunk)))
.map(|response| response.map(Body::from)),
)
}
}