Coder Social home page Coder Social logo

Request Interceptor about restinio HOT 2 CLOSED

stiffstream avatar stiffstream commented on May 22, 2024
Request Interceptor

from restinio.

Comments (2)

eao197 avatar eao197 commented on May 22, 2024

Hi!

Sorry for the delay with the response, I needed some time to think.

I suppose you want to implement some handler that will accept all incoming requests, do something and then delegate the processing of a request to ordinary RESTinio mechanisms (like express-router).

If so then I'm afraid the only way to do that is to write your own router object. This object will accept all requests, do what you need and then will call some of RESTinio's standard router. For example, it could be something like that:

class interceptor_router {
    // This will be the actual router.
   restinio::router::express_router_t actual_router_;
   ...
public:
   // Add a request handler to actual_router.
   void http_get(restinio::string_view_t path, restinio::router::express_request_handler_t handler) {
      actual_router_.http_get(path, std::move(handler));
   }
   void http_post(restinio::string_view_t path, restinio::router::express_request_handler_t handler) {
      actual_router_.http_post(path, std::move(handler));
   }
   ...
   // This operator will be called by RESTinio on arrival of a new request.
   restinio::request_handling_status_t operator()(restinio::request_handle_t req) const
   {
      ... // Do what you want.
      // Now the actual router can be called.
      actual_handler_(std::move(req));
   }
   ...
};

Then you can parametrize RESTinio's server traits by this type:

struct my_traits_t : public restinio::default_traits_t {
   using request_handler_t = interceptor_router;
};
...
run( restinio::on_thread_pool<my_traits_t>(4)... );

PS. It seems that you can derive interceptor_router from restinio::router::express_router_t:

class interceptor_router : public restinio::router::express_router_t {
   using base_type = restinio::router::express_router_t;
   ...
public:
   // This operator will be called by RESTinio on arrival of a new request.
   restinio::request_handling_status_t operator()(restinio::request_handle_t req) const
   {
      ... // Do what you want.
      // Now the actual router can be called.
      base_type::operator()(std::move(req));
   }
   ...
};

In that case you don't need to redefine methods like http_get and http_put in your router type.

from restinio.

webfolderio avatar webfolderio commented on May 22, 2024

Thanks so much, your code snippet it works flawlessly.

from restinio.

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.