#[macro_use]
extern crate tower_web;
extern crate tokio;
use tower_web::ServiceBuilder;
#[derive(Clone, Debug)]
pub struct ArgResource;
impl_web! {
impl ArgResource {
#[get("/one/:param")]
fn path_str(&self, param: String) -> Result<String, ()> {
Ok(format!("We received: {} in the path", param))
}
#[get("/two/:foo/:bar")]
fn path_multi_str(&self, foo: String, bar: String) -> Result<String, ()> {
Ok(format!("We received: {} and {} in the path", foo, bar))
}
#[get("/num/:num")]
fn path_num(&self, num: u32) -> Result<String, ()> {
Ok(format!("We received: {} in the path", num))
}
#[get("/query-string")]
fn hello_query_string(&self, query_string: String) -> Result<String, ()> {
Ok(format!("We received the query {:?}", query_string))
}
#[post("/request-body")]
fn request_body(&self, body: Vec<u8>) -> Result<String, ()> {
Ok(format!("We received {} bytes", body.len()))
}
#[get("/headers")]
fn headers(&self, x_required: String, x_optional: Option<String>) -> Result<String, ()> {
Ok(format!("We received: x-required = {}; x-optional = {:?}", x_required, x_optional))
}
}
}
pub fn main() {
let addr = "127.0.0.1:8080".parse().expect("Invalid address");
println!("Listening on http://{}", addr);
ServiceBuilder::new()
.resource(ArgResource)
.run(&addr)
.unwrap();
}