Coder Social home page Coder Social logo

Comments (4)

gocarlos avatar gocarlos commented on May 16, 2024

my first guess was to add the missing template to basic_router.cpp though no luck -> does not work either (compiling though now functioning):

    template <class... OnRequest>
    void use(resource_regex_type const& path_to_resource, const base_type& other, OnRequest&&... on_request) {
      add_resource_cb_without_method(path_to_resource, storage_type{std::forward<OnRequest>(on_request)...});

      base_type::use(path_to_resource, other);
    }
  main_router.use(route_api_str, api_router, [](auto request, auto context) {
    if (request[boost::beast::http::field::authorization] != "my_token") {
      context.send(make_401<boost::beast::http::string_body>(request));
      std::cout << "asdf" << std::endl;
      return;
    }
  });

from beasthttp.

0xdead4ead avatar 0xdead4ead commented on May 16, 2024

If you still need to, but why not do this:

  api_router.get("^/1$",
                 std::bind(authMiddleware, std::_1, std::_2, std::_3),
                 [](auto request, auto context) {
                   /// ...
                 });

  api_router.get("^/2$",
                 std::bind(authMiddleware, std::_1, std::_2, std::_3),
                 [](auto request, auto context) {
                   /// ...
                 });
// ...

main_router.use("/numbers", api_router);

?

from beasthttp.

gocarlos avatar gocarlos commented on May 16, 2024

The point is that it does not seem to work to use chainable middlewares for e.g. logging, authentication etc for router.use one can use the middlewares only wity router.get|post|put etc

Using an auth middleware in all get methods is pretty verbose when you can just add one middleware to the router...
E.g. all routes starting with /api/ shall be authenticated with e.g. a token
And all other routes are serving static content which is public
Furthermore all routes with /api/admin/ shall be send through an authorization middleware after the authentication middleware

from beasthttp.

gocarlos avatar gocarlos commented on May 16, 2024

did it myself as I also needed other features...
Example of what I wished in this issue: https://github.com/expresscpp/expresscpp/blob/master/example/router.cpp

from beasthttp.

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.