Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use Middleware with CROW Blueprint #795

Open
manishjha5410 opened this issue Apr 12, 2024 · 13 comments
Open

How to use Middleware with CROW Blueprint #795

manishjha5410 opened this issue Apr 12, 2024 · 13 comments
Labels
question Issue can be closed by providing information

Comments

@manishjha5410
Copy link

manishjha5410 commented Apr 12, 2024

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
Copy link
Author

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

@witcherofthorns
Copy link
Contributor

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

@manishjha5410
Copy link
Author

manishjha5410 commented Apr 13, 2024

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

@witcherofthorns
Copy link
Contributor

witcherofthorns commented Apr 13, 2024

@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
Copy link
Contributor

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
Copy link
Contributor

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

@manishjha5410
Copy link
Author

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.

@witcherofthorns
Copy link
Contributor

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

@manishjha5410
Copy link
Author

manishjha5410 commented Apr 15, 2024

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
Copy link
Author

I will update my code accordingly

@witcherofthorns
Copy link
Contributor

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 🤷‍♂️

@gittiver gittiver added the question Issue can be closed by providing information label Apr 15, 2024
@manishjha5410
Copy link
Author

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.

@gittiver
Copy link
Member

Seems that not everything was clarified, so issue reopened.

@gittiver gittiver reopened this Apr 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issue can be closed by providing information
Projects
None yet
Development

No branches or pull requests

3 participants