Skip to content

Commit

Permalink
Fix routing
Browse files Browse the repository at this point in the history
  • Loading branch information
TheNeikos committed Nov 24, 2018
1 parent 44eb399 commit 6a5d61b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 11 deletions.
3 changes: 3 additions & 0 deletions webframework-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ pub fn controller(args: TokenStream, input: TokenStream) -> TokenStream {
fn handle(&self, mut req: ::webframework_core::request::Request,
path: Option<String>, params: ::std::collections::HashMap<String, String>)
-> ::webframework_core::router::RouterResult {
if path.is_some() && path != Some("/".into()) {
return ::webframework_core::router::RouterResult::Unhandled(req, params);
}
let result = ||{
#(#input_tokens)*;
#(#param_tokens)*;
Expand Down
9 changes: 1 addition & 8 deletions webframework/src/request_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ impl PathFilter for PathRegex {

if new_path.is_empty() || new_path == "/" {
new_path = String::from("/");
} else {
return PathFilterResult::NotMatched;
}

PathFilterResult::Matched(new_path, params)
Expand All @@ -62,11 +60,6 @@ macro_rules! request_filter {
fn handles(&self, $req: &$crate::request_filter::Request) -> bool {
let val: bool = $impl;

if val {
let log = $req.log();
slog::debug!(log, "filtered by {}", stringify!($name);"request_filter" => stringify!($name));
}

return val;
}

Expand All @@ -90,7 +83,7 @@ request_filter! {
}

request_filter! {
delegate, "Delegates to the next filter or router" => req {
delegate, "Delegates to the next filter or router" => _req {
true
}
}
Expand Down
12 changes: 11 additions & 1 deletion webframework/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,17 @@ impl<S: Router + 'static + Send> hyper::service::Service for Service<S> {
Box::new(ret.then(move |resp| {
let elapsed = now.elapsed();
let time: f64 = elapsed.as_secs() as f64 * 1000.0 + elapsed.subsec_nanos() as f64 / 1_000_000.0;
slog::info!(time_logger, "Handled in {}ms", time; "elapsed_time" => time);

let mut status = None;

match &resp {
Ok(resp) => {
status = Some(resp.status().as_u16());
}
Err(_) => (),
}

slog::debug!(time_logger, "Handled in {}ms", time; "elapsed_time" => time, "status" => status);
resp
}))
}
Expand Down
3 changes: 1 addition & 2 deletions webframework/tests/macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ fn check_routing() {
assert!(router.handle(req, None, HashMap::new()).is_unhandled());
}

#[controller]
#[params="test"]
#[controller(params = "test")]
fn dynamic(test: String)-> WebResult<Response> {
assert_eq!(test, "foo");

Expand Down
47 changes: 47 additions & 0 deletions webframework/tests/nested_router.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
extern crate webframework as wfw;

use crate::wfw::prelude::*;

use std::collections::HashMap;

use hyper::{Request as HyperRequest, Body};
use slog;
use uuid;

#[controller]
fn handle_it(_req: &Request) -> WebResult<Response> {
Ok(Response::from_string("Response it"))
}

routing! {
SimpleRouter => {
GET "/bar/:test" => handle_it;
GET "/" => handle_it;
}
}

routing! {
NestedRouter => {
GET "/foo" => SimpleRouter;
}
}

fn new_request(path: &str) -> Request {
let req = HyperRequest::get(path).body(Body::empty()).unwrap();
let logger = slog::Logger::root(slog::Discard, slog::o!());
let id = uuid::Uuid::new_v4();

Request::from_req(id, logger, req)
}

#[test]
fn nested_dynamic() {
let router = NestedRouter;

for path in &["/foo", "/foo/", "/foo/bar/blah"] {
println!("Testing {}", path);
let req = new_request(&path);
assert!(router.handle(req, None, HashMap::new()).is_handled());
}
}

0 comments on commit 6a5d61b

Please sign in to comment.