Skip to content

Commit

Permalink
Add simple nested router example
Browse files Browse the repository at this point in the history
  • Loading branch information
tirr-c committed Nov 18, 2018
1 parent dfe72ab commit 4bb3d3f
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions examples/simple_nested_router.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// A `Router`-nesting version of the example `named_path`.

#![feature(async_await, futures_api)]

use tide::{
head::{Named, NamedComponent},
Router,
};

struct Number(i32);

impl NamedComponent for Number {
const NAME: &'static str = "num";
}

impl std::str::FromStr for Number {
type Err = std::num::ParseIntError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
s.parse().map(|num| Number(num))
}
}

async fn add_two(Named(number): Named<Number>) -> String {
let Number(num) = number;
format!("{} plus two is {}", num, num + 2)
}

fn build_add_two<Data: Clone + Send + Sync + 'static>(router: &mut Router<Data>) {
router.at("{num}").get(add_two);
}

fn main() {
let mut app = tide::App::new(());
app.at("add_two").nest(build_add_two);
app.serve("127.0.0.1:8000");
}

0 comments on commit 4bb3d3f

Please sign in to comment.