Crow icon indicating copy to clipboard operation
Crow copied to clipboard

How to use Middleware with CROW Blueprint

Open manishjha5410 opened this issue 10 months ago • 13 comments

I want to know how can i use middleware with crow_bp_route as i had seen the example of crow middleware there is a need to provide the middleware to crow:APP but is it even necessary ?

Suppose if i had 50 middleware which is separated across 10 blueprint, so mycrow app would be a lengthy very datatype so is there any way to provide middleware directly to blueprint instead of passing to crow::app.

Please answer @thedrow @gittiver

manishjha5410 avatar Apr 12 '24 17:04 manishjha5410

See my whole point if we are just copying and pasting entire parameter of crow blueprint to crow app then why to use crow blueprint use crow app instead

manishjha5410 avatar Apr 12 '24 18:04 manishjha5410

Hi, yep, i agree with you, there is this slightly inconvenient thing, you constantly have to forward instance crow::app. Personally, i dont use my custom middleware for this, instead, i use simple methods declared in my middlewares.hpp (for example jwt verify), it's much more convenient 🤷‍♂️ P.s. It seems to me that this is unlikely to be corrected in any way, most likely, everything will remain as it is

witcherofthorns avatar Apr 13 '24 09:04 witcherofthorns

Hi what are the custom middlewares that you used can you tell me more about it

manishjha5410 avatar Apr 13 '24 12:04 manishjha5410

@manishjha5410 All the middleware I used ended with CORS or Cookie, i can’t share much experience, because basically, everything was placed in main.cpp, but if you really care, you could try defining crow app in one place, and passing it as a variable by reference

// app.hpp
#include<crow/app.h>
#include<crow/middlewares/cors.h>
#define CROW_APP crow::App<crow::CORSHandler>

well, yes, this is just an option, but it will help you not to constantly change variables everywhere if you add new middleware

witcherofthorns avatar Apr 13 '24 18:04 witcherofthorns

in fact, in the working project I didn’t bother too much, everything is simple for me, I just have some kind of .hpp file that has a class/methods, and .cpp implementation files that I just call when I need it, here and that's all

// jwt.hpp
const std::string jwt_create_access(std::string uuid);
const std::string jwt_create_refresh(std::string uuid);

// jwt.cpp
const std::string jwt_create_access(std::string uuid){
    // your code
}
const std::string jwt_create_refresh(std::string uuid){
    // your code
}

// your auth route
CROW_BP_ROUTE(bp, "auth/update/access").methods(HTTPMethod::POST)([](const crow::request& req) {
    // get header (token) string from name "Refresh"
    // jwt verify your token
    // call jwt_create_access(uuid);
});

yes, I don’t use classes, because I don’t see much point in it, since everything is static anyway, and there’s actually not that much code

witcherofthorns avatar Apr 13 '24 19:04 witcherofthorns

but if you want to use the middleware scheme from Сrow, you can check out this page

witcherofthorns avatar Apr 13 '24 19:04 witcherofthorns

Thanks for your approach but is it possible to define all the stuff in single cpp file does isnt it too chaotic personally i think it would create a mess. Okay do you know someone who can give me a glance of the crow workflow just for the middlewares if there exist no possible solution I would try to create one.

manishjha5410 avatar Apr 14 '24 14:04 manishjha5410

I don't quite understand what you mean, well, you can look at the Crow source code or all the available information on the Crow website, maybe it will help, idnt know

witcherofthorns avatar Apr 14 '24 18:04 witcherofthorns

Actually i was saying can someone help me for understanding the functionality of middleware, how they are link how they are used,etc in crow source code

manishjha5410 avatar Apr 15 '24 02:04 manishjha5410

I will update my code accordingly

manishjha5410 avatar Apr 15 '24 02:04 manishjha5410

If middleware is used in your Route/Blueprint, then it occurs before Crow gives you the crow::req, the middleware is needed to do pre-checking, before passing along your custom route, this is just for convenience, that's all , there is no more magic here, I don’t know what else can be explained here 🤷‍♂️

witcherofthorns avatar Apr 15 '24 10:04 witcherofthorns

Okay so how is Crow Middleware works in crow:App how does the pre checking work didnt the middleware is called before the crow::App get the crow::req and why did @gittiver you closed this issue i had remaining some things to clear.

manishjha5410 avatar Apr 15 '24 14:04 manishjha5410

Seems that not everything was clarified, so issue reopened.

gittiver avatar Apr 15 '24 20:04 gittiver

@manishjha5410 please tell what to clear.

gittiver avatar Jul 29 '24 08:07 gittiver

Cleared

manishjha5410 avatar Jul 29 '24 08:07 manishjha5410