#![deny(warnings)]
use rweb::*;
#[get("/hi")]
fn hi() -> &'static str {
"Hello, World!"
}
#[get("/hello/from/warp")]
fn hello_from_warp() -> &'static str {
"Hello from warp!"
}
#[get("/sum/{a}/{b}")]
fn sum(a: u32, b: u32) -> String {
format!("{} + {} = {}", a, b, a + b)
}
#[get("/{a}/times/{b}")]
fn times(a: u16, b: u16) -> String {
format!("{} times {} = {}", a, b, a * b)
}
#[get("/byt/{name}")]
fn bye(name: String) -> String {
format!("Good bye, {}!", name)
}
#[tokio::main]
async fn main() {
pretty_env_logger::init();
let math = rweb::path("math");
let _sum = math.and(sum());
let _times = math.and(times());
let math = rweb::path("math").and(sum().or(times()));
let help = rweb::path("math")
.and(rweb::path::end())
.map(|| "This is the Math API. Try calling /math/sum/:u32/:u32 or /math/:u16/times/:u16");
let math = help.or(math);
let sum =
sum().map(|output| format!("(This route has moved to /math/sum/:u16/:u16) {}", output));
let times =
times().map(|output| format!("(This route has moved to /math/:u16/times/:u16) {}", output));
let routes = rweb::get().and(
hi().or(hello_from_warp())
.or(bye())
.or(math)
.or(sum)
.or(times),
);
rweb::serve(routes).run(([127, 0, 0, 1], 3030)).await;
}