Code Monkey home page Code Monkey logo

Comments (3)

tqwewe avatar tqwewe commented on June 14, 2024

Should the _ catch all route inherit the middleware just like other routes?

For example:

router! {
    use LoggingMiddleware;

    GET "/foo" => foo_handler
    GET "/bar" => bar_handler
    _ => handle_404
}

Should routes handled by handle_404 call the logging middleware like a regular request?

from submillisecond.

bkolobara avatar bkolobara commented on June 14, 2024

Yes, I think so. To stay consistent.

from submillisecond.

tqwewe avatar tqwewe commented on June 14, 2024

I would expect we treat the value after _ => to be a handler rather than a subrouter... but what if we want to support a subrouter being there instead?

Sadly, our currently implementation treats handler functions different to subrouters. Subrouters are called directly with subrouter(__req, __params, __reader), and handlers with Handler::handle(handler, __req).

An example of using a router with the catch all system would be with static_dir! macro:

router! {
    "/static" => {
        GET "/foo.txt" => serve_foo
        _ => static_dir!("./static")
    }
}

The most simple way to treat the value diffrently is to check if it's prefixed with a HTTP method, and if so, treat it as a handler, otherwise subrouter. But that means there wouldn't be an easy way to serve a custom 404 handler if the catchall could be prefixed with handlers. They'd have to do:

router! {
    GET "/foo" => foo_handler
    GET _ => handle_404
    POST _ => handle_404
    PATCH _ => handle_404
    // ...and so on for each method
}

Perhaps we need to introduce a http keyword for any http method, but to indicate that we treat the value as a handler.
For example:

router! {
    GET "/foo" => foo_handler // handler
    ANY _ => handle_404 // handler
    _ => SUBROUTER_404 // subrouter
}

from submillisecond.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.