Merge pull request #76 from ayaankhan98/master

recieve response without request being another argument in handler
This commit is contained in:
Farook Al-Sammarraie 2020-12-03 15:53:11 +03:00 committed by GitHub
commit c81a2d84e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 37 additions and 12 deletions

View File

@ -30,8 +30,7 @@ You can also access the url parameters in the handler using `#!cpp req.url_param
For more information on `crow::request` go [here](../../reference/structcrow_1_1request.html).<br><br> For more information on `crow::request` go [here](../../reference/structcrow_1_1request.html).<br><br>
###Response ###Response
Crow also provides the ability to define a response in the parameters by using `#!cpp ([](const crow::request& req, crow::response& res){...})`.<br> Crow also provides the ability to define a response in the parameters by using `#!cpp ([](crow::response& res){...})`.<br><br>
If you don't want to use the request you can write `#!cpp ([](const crow::request& , crow::response& res){...})`.<br><br>
Please note that in order to return a response defined as a parameter you'll need to use `res.end();`.<br><br> Please note that in order to return a response defined as a parameter you'll need to use `res.end();`.<br><br>

View File

@ -13,12 +13,11 @@ struct ExampleMiddleware
{ {
std::string message; std::string message;
ExampleMiddleware() ExampleMiddleware() : message("foo")
{ {
message = "foo";
} }
void setMessage(std::string newMsg) void setMessage(const std::string &newMsg)
{ {
message = newMsg; message = newMsg;
} }
@ -84,7 +83,7 @@ int main()
// To see it in action submit {ip}:18080/add/1/2 and you should receive 3 (exciting, isn't it) // To see it in action submit {ip}:18080/add/1/2 and you should receive 3 (exciting, isn't it)
CROW_ROUTE(app,"/add/<int>/<int>") CROW_ROUTE(app,"/add/<int>/<int>")
([](const crow::request& /*req*/, crow::response& res, int a, int b){ ([](crow::response& res, int a, int b){
std::ostringstream os; std::ostringstream os;
os << a+b; os << a+b;
res.write(os.str()); res.write(os.str());

View File

@ -13,12 +13,11 @@ struct ExampleMiddleware
{ {
std::string message; std::string message;
ExampleMiddleware() ExampleMiddleware() : message("foo")
{ {
message = "foo";
} }
void setMessage(std::string newMsg) void setMessage(const std::string &newMsg)
{ {
message = newMsg; message = newMsg;
} }
@ -78,7 +77,7 @@ int main()
}); });
app.route_dynamic("/add/<int>/<int>") app.route_dynamic("/add/<int>/<int>")
([](const crow::request& req, crow::response& res, int a, int b){ ([](crow::response& res, int a, int b){
std::ostringstream os; std::ostringstream os;
os << a+b; os << a+b;
res.write(os.str()); res.write(os.str());

View File

@ -41,8 +41,10 @@ int main()
return crow::response(os.str()); return crow::response(os.str());
}); });
// example which uses only response as a paramter wihtout
// request being a parameter.
CROW_ROUTE(app,"/add/<int>/<int>") CROW_ROUTE(app,"/add/<int>/<int>")
([](const crow::request& /*req*/, crow::response& res, int a, int b){ ([](crow::response& res, int a, int b){
std::ostringstream os; std::ostringstream os;
os << a+b; os << a+b;
res.write(os.str()); res.write(os.str());

View File

@ -529,7 +529,33 @@ namespace crow
template <typename Func> template <typename Func>
typename std::enable_if< typename std::enable_if<
!black_magic::CallHelper<Func, black_magic::S<Args...>>::value && !black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
!black_magic::CallHelper<Func, black_magic::S<crow::request, Args...>>::value, !black_magic::CallHelper<Func, black_magic::S<crow::request, Args...>>::value &&
black_magic::CallHelper<Func, black_magic::S<crow::response&, Args...>>::value,
void>::type
operator()(Func&& f)
{
static_assert(black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
black_magic::CallHelper<Func, black_magic::S<crow::response&, Args...>>::value
,
"Handler type is mismatched with URL parameters");
static_assert(std::is_same<void, decltype(f(std::declval<crow::response&>(), std::declval<Args>()...))>::value,
"Handler function with response argument should have void return type");
handler_ = (
#ifdef CROW_CAN_USE_CPP14
[f = std::move(f)]
#else
[f]
#endif
(const crow::request&, crow::response& res, Args ... args){
f(res, args...);
});
}
template <typename Func>
typename std::enable_if<
!black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
!black_magic::CallHelper<Func, black_magic::S<crow::request, Args...>>::value &&
!black_magic::CallHelper<Func, black_magic::S<crow::response&, Args...>>::value,
void>::type void>::type
operator()(Func&& f) operator()(Func&& f)
{ {