the-moisrex / webpp

C++ web framework | web development can be done with C++ as well.
https://t.me/webpp
MIT License
128 stars 9 forks source link

Transformer Middlewares #172

Open the-moisrex opened 1 year ago

the-moisrex commented 1 year ago

Imagine this scenario:

You have an app, that has API and a view.

The normal html website will require the same json values that are provided in the API.

struct app {

  auto users() {
    json::document doc;
    // ...
    return doc;
  }

  void register_routes(auto& router) {
    router[rel / "users"] = &app::users;
  }

  auto transform_to_web(Context auto& context, HTTPResponse auto&& res) {
     return res.view("web/template.mustache", res.body.as_json());
  }

  void setup_router(auto& router) {
     router["api"] = &app::register_routes;
     router["web"] | &app::transform_to_web = &app::register_routes;
  }
};

This way, we're transforming the api call's response, and passing it to a mustache view to be used there, and then returning the result if the user calls for the "web". But if the user requests for "api", the middleware won't happen.

This is an excellent idea that will help the users to provide an API and at the same time enable Server Side Rendering.

Of course, the syntax for registering the middlewares/transformers are decided yet.

I would like the Transformer idea to be the same as Middleware idea.

the-moisrex commented 1 year ago

idea:

  void setup_router(auto& router) {
     router["api"] = &app::register_routes;
     auto web_router = router | &app::transform_to_web;
     web_router["web"] = &app::register_routes;
  }