Middleware Examples? #78
-
I'm having fun trying out Grapevine, but unfortunately for now it lacks documentation and examples. Perhaps it is why some is sticking with Grapevine 4, but I'm interested with the middleware. I couldn't find anything about that, but what I have in mind is that it is something like Express JS' middleware. I'm trying to validate headers on each request except for some routes. Is there any examples for that, or something similar, where I can decide to continue the routing or not? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Yes. The easiest way to do this it to hook into the In your startup class: public void ConfigureServer(IRestServer server)
{
// These handlers are executed in the order that they are registered
server.Router.BeforeRoutingAsync += MyRoutingEventHandler;
} And then somewhere else in your code: public async Task MyRoutingEventHandler(IHttpContext context)
{
// do whatever you want to do here
// if you don't want to continue to the routing, then you need to send an response here
} The other places you can hook into the request/response lifecycle are:
You can also hook into the server start/stop using
|
Beta Was this translation helpful? Give feedback.
-
You are correct, you can only send the response once. That is why no more routes are invoked once a response has been sent. |
Beta Was this translation helpful? Give feedback.
Yes. The easiest way to do this it to hook into the
Router.BeforeRoutingAsync
event handler.In your startup class:
And then somewhere else in your code:
The other places you can hook into the request/response lifecycle are:
RestServer.OnRequestAsync
will run when the request is received, useful if you want to…