diff options
| author | Fuwn <[email protected]> | 2022-03-26 12:00:57 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2022-03-26 12:02:20 +0000 |
| commit | 124a583b60668b05d8e5c2ccd75b9636802da1bf (patch) | |
| tree | 92b3c69b34fe28b785b7c55ad8021120e595e4df /examples/windmark.rs | |
| parent | refactor(mount): dynamic format (diff) | |
| download | windmark-124a583b60668b05d8e5c2ccd75b9636802da1bf.tar.xz windmark-124a583b60668b05d8e5c2ccd75b9636802da1bf.zip | |
feat(mount): use a sophisticated route matcher
The [`matchit`](https://github.com/ibraheemdev/matchit) crate is a real
lifesaver...
Before this commit, I had manually implemented a dynamic pattern matcher
for routes (which only supported one pattern per route) that worked; just
not very well. While searching for a way to implement multiple patterns
per route, I landed upon two crates:
[`matchit`](https://github.com/ibraheemdev/matchit) and
[`urlmatch`](https://github.com/SergeiMinaev/urlmatch). Ultimately, I
settled with `matchit` because it seemed a lot more mature and supported
nearly everything which I was looked for!
Diffstat (limited to 'examples/windmark.rs')
| -rw-r--r-- | examples/windmark.rs | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/examples/windmark.rs b/examples/windmark.rs index 2a4bbf9..03385ae 100644 --- a/examples/windmark.rs +++ b/examples/windmark.rs @@ -31,14 +31,14 @@ fn main() -> std::io::Result<()> { .set_error_handler(|_, _, _| { Response::PermanentFailure("error...".to_string()) }) - .set_pre_route_callback(|stream, url| { + .set_pre_route_callback(|stream, url, _| { info!( "accepted connection from {} to {}", stream.peer_addr().unwrap().ip(), url.to_string() ) }) - .set_post_route_callback(|stream, _url| { + .set_post_route_callback(|stream, _url, _| { info!( "closed connection from {}", stream.peer_addr().unwrap().ip() @@ -78,8 +78,18 @@ fn main() -> std::io::Result<()> { windmark::utilities::queries_from_url(&url) )) }) - .mount("/param/<lang>", |_, _url, dynamic_parameter| { - Response::Success(format!("Parameter lang is {:?}", dynamic_parameter)) + .mount("/param/:lang", |_, _url, dynamic_parameter| { + Response::Success(format!( + "Parameter lang is {}", + dynamic_parameter.unwrap().get("lang").unwrap() + )) + }) + .mount("/names/:first/:last", |_, _url, Some(dynamic_parameter)| { + Response::Success(format!( + "{} {}", + dynamic_parameter.get("first").unwrap(), + dynamic_parameter.get("last").unwrap() + )) }) .mount("/input", |_, url, _| { if let Some(name) = url.query() { |