-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from tirr-c/subrouter
Subrouters and per-endpoint middleware
- Loading branch information
Showing
5 changed files
with
498 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.