matt-42 / silicon

A high performance, middleware oriented C++14 http web framework please use matt-42/lithium instead
http://siliconframework.org
MIT License
1.72k stars 138 forks source link

Can global middlewares access request & response? #70

Closed lythesia closed 5 years ago

lythesia commented 5 years ago

For example, we can log time consume for each request with its req path and body included Or we can modify response after request completed

matt-42 commented 5 years ago

Yes, just add those as parameter of the middleware instantiate method:


#include <silicon/backends/mhd.hh>
#include <silicon/api.hh>
#include "symbols.hh"

using namespace s;

using namespace sl;

class my_midleware
{
public:

  my_midleware(mhd_request& request, mhd_response& response)
  {
    response.set_header("Server", "silicon_test_global");
  }

  static my_midleware instantiate(mhd_request* request, mhd_response* response)
  {
    return my_midleware(*request, *response);
  }

};

int main()
{
  auto hello_api = http_api(

    GET / _test = [] () {

      return "Hello world";
    }

    );

  auto hello_api_ga = add_global_middlewares<my_midleware>::to(hello_api);
  auto server = mhd_json_serve(hello_api_ga, 12345, _nthreads = 1, _blocking);

}```
lythesia commented 5 years ago

It works! Thank you. I'll take look at the instantiate method.

lythesia commented 5 years ago

One more question: can we access route info also (so we can log http verb then)? Maybe not possible for now (?) The route R hides in prefix_path_middleware<R>, but GA's instantiate method cannot be templated. (It seems that accessible args in taglist when instantiate GA must not be dependenty types?)

matt-42 commented 5 years ago

Not yet, I'll add it in the mhd_request object. Matthieu Garrigues

On Mon, Jun 17, 2019 at 11:53 AM Yi Lu notifications@github.com wrote:

One more question: can we access route info also (so we can log http verb then)? Maybe not possible for now (?) The route R hides in prefix_path_middleware, but GA's instantiate method cannot be templated. (It seems that accessible args in taglist when instantiate GA must not be dependenty types?)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/matt-42/silicon/issues/70?email_source=notifications&email_token=AAGOV6FCEYVBV2V7T25PXOTP25NLDA5CNFSM4HXXQRZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODX2VB3Y#issuecomment-502616303, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGOV6DJWLO7EMOQUJQNBKLP25NLDANCNFSM4HXXQRZA .

lythesia commented 5 years ago

Thanks!