Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I make run middleware if there is no matches in router? #375

Closed
AngelicosPhosphoros opened this issue Nov 19, 2019 · 3 comments
Closed

Comments

@AngelicosPhosphoros
Copy link
Contributor

I recently started a project and I added special middleware to render special pages for 404 and 500 results.
But the issue is that it not run sometimes.

How can I make my middleware trigger if no matching routes in router?

I created very small example program to show the issue:

extern crate gotham;
#[macro_use]
extern crate gotham_derive;
extern crate hyper;
extern crate mime;

use gotham::router::builder::DefineSingleRoute;
use gotham::router::builder::DrawRoutes;

use gotham::middleware::Middleware;
use gotham::router::Router;
use gotham::pipeline::set::new_pipeline_set;
use gotham::pipeline::new_pipeline;
use gotham::pipeline::set::finalize_pipeline_set;
use gotham::router::builder::build_router;
use gotham::state::State;
use gotham::handler::HandlerFuture;


const HELLO_WORLD: &str = "Hello World!";
const HELLO_MIDDLE: &str = "Hello World From Middle!";

pub fn say_hello(state: State) -> (State, &'static str) {
    (state, HELLO_WORLD)
}

#[derive(Clone, NewMiddleware)]
pub struct PrintMiddleWare;

impl Middleware for PrintMiddleWare {
    fn call<Chain>(self, state: State, chain: Chain) -> Box<HandlerFuture>
    where
        Chain: FnOnce(State) -> Box<HandlerFuture>,
    {
    	println!("{:?}", HELLO_MIDDLE);
    	chain(state)
    }
}

fn router()->Router{
	let pipelines = new_pipeline_set();
    let (pipelines, default) = pipelines.add(
        new_pipeline()
            .add(PrintMiddleWare{})
            .build(),
    );
    let pipeline_set = finalize_pipeline_set(pipelines);
    let default_chain = (default, ());
    build_router(default_chain, pipeline_set, |route| {
    	// Here will print
    	route.get_or_head("/").to(say_hello);
    	// Here will print
    	route.get_or_head("/will-print").to(say_hello);
    	// Will NOT print on /test because no matches
    	// But I want to print
    })
}

pub fn main() {
    let addr = "127.0.0.1:7878";
    println!("Listening for requests at http://{}", addr);
    gotham::start(addr, router())
}
@AngelicosPhosphoros
Copy link
Contributor Author

AngelicosPhosphoros commented Nov 20, 2019

I handled it by adding this at the end of routes:

route.get_or_head("/*").to(|state|{
    let response = create_empty_response(&state, StatusCode::NOT_FOUND);
    (state, response)
});

@colinbankier
Copy link
Collaborator

Thanks for the report @AngelicosPhosphoros . There have been a few similar issues such as #256 or #292. These are definitely things we'd like to address as we improve the router.

@msrd0
Copy link
Member

msrd0 commented Aug 26, 2020

Closing as duplicate of #256

@msrd0 msrd0 closed this as completed Aug 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants