#[macro_use] extern crate error_chain; extern crate hyper; #[macro_use] extern crate serde_derive; extern crate serv; extern crate tokio; use hyper::rt::Future; mod error { use super::*; error_chain! { foreign_links { Serv(serv::Error); } errors { Overflow { display("overflow") } } } } use error::*; #[derive(Deserialize)] struct AddReq { a: i8, b: i8, } #[derive(Serialize)] struct AddResp { result: i8, } fn add(req: AddReq) -> Result { match req.a.checked_add(req.b) { Some(result) => Ok(AddResp { result }), None => bail!(ErrorKind::Overflow), } } fn main() { use serv::server::{Routes, Server}; let addr = ([127, 0, 0, 1], 3000).into(); let mut routes = Routes::new(); routes.push(hyper::Method::GET, "/", serv::sync::serv(add)); let server = Server::new(routes); let server = hyper::server::Server::bind(&addr) .serve(move || Ok::<_, hyper::Error>(server.clone())) .map_err(|e| eprintln!("failed to serve: {:?}", e)); let mut rt = tokio::runtime::current_thread::Runtime::new().expect("failed to create runtime"); rt.block_on(server).expect("error on runtime"); }