Use 'any' to support two types of constraints
This commit is contained in:
parent
6745d703db
commit
eef0ee8729
|
@ -8,7 +8,7 @@ class Attachment : public drogon::HttpApiController<Attachment>
|
|||
public:
|
||||
METHOD_LIST_BEGIN
|
||||
//use METHOD_ADD to add your custom processing function here;
|
||||
registerMethod(&Attachment::get, "/", {HttpRequest::Post});
|
||||
METHOD_ADD(Attachment::get, "/", HttpRequest::Post);
|
||||
|
||||
METHOD_LIST_END
|
||||
//your declaration of processing function maybe like this:
|
||||
|
|
|
@ -7,19 +7,19 @@ namespace v1
|
|||
{
|
||||
class ApiTest : public drogon::HttpApiController<ApiTest>
|
||||
{
|
||||
public:
|
||||
METHOD_LIST_BEGIN
|
||||
//use METHOD_ADD to add your custom processing function here;
|
||||
registerMethod(&ApiTest::get, "/get/{2}/{1}", {},{"drogon::GetFilter"}); //path will be /api/v1/apitest/get/{arg2}/{arg1}
|
||||
registerMethod(&ApiTest::your_method_name, "/{1}/List?P2={2}", {HttpRequest::Get}); //path will be /api/v1/apitest/{arg1}/list
|
||||
registerMethod(&ApiTest::staticApi, "/static",{HttpRequest::Get,HttpRequest::Post});
|
||||
METHOD_ADD(ApiTest::get2, "/get/{1}", "drogon::GetFilter");
|
||||
METHOD_LIST_END
|
||||
//your declaration of processing function maybe like this:
|
||||
void get(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback, int p1, std::string &&p2);
|
||||
void your_method_name(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback, double p1, int p2) const;
|
||||
void staticApi(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback);
|
||||
void get2(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback, std::string &&p1);
|
||||
public:
|
||||
METHOD_LIST_BEGIN
|
||||
//use METHOD_ADD to add your custom processing function here;
|
||||
METHOD_ADD(ApiTest::get, "/get/{2}/{1}", "drogon::GetFilter"); //path will be /api/v1/apitest/get/{arg2}/{arg1}
|
||||
METHOD_ADD(ApiTest::your_method_name, "/{1}/List?P2={2}", HttpRequest::Get); //path will be /api/v1/apitest/{arg1}/list
|
||||
METHOD_ADD(ApiTest::staticApi, "/static", HttpRequest::Get, HttpRequest::Post);
|
||||
METHOD_ADD(ApiTest::get2, "/get/{1}", "drogon::GetFilter");
|
||||
METHOD_LIST_END
|
||||
//your declaration of processing function maybe like this:
|
||||
void get(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback, int p1, std::string &&p2);
|
||||
void your_method_name(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback, double p1, int p2) const;
|
||||
void staticApi(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback);
|
||||
void get2(const HttpRequestPtr &req, const std::function<void(const HttpResponsePtr &)> &callback, std::string &&p1);
|
||||
};
|
||||
} // namespace v1
|
||||
} // namespace api
|
||||
|
|
|
@ -25,9 +25,9 @@
|
|||
static void initMethods() \
|
||||
{
|
||||
|
||||
#define METHOD_ADD(method, pattern, filters...) \
|
||||
{ \
|
||||
registerMethod(&method, pattern, {}, {filters}); \
|
||||
#define METHOD_ADD(method, pattern, filters...) \
|
||||
{ \
|
||||
registerMethod(&method, pattern, {filters}); \
|
||||
}
|
||||
|
||||
#define METHOD_LIST_END \
|
||||
|
@ -45,14 +45,37 @@ class HttpApiController : public DrObject<T>
|
|||
template <typename FUNCTION>
|
||||
static void registerMethod(FUNCTION &&function,
|
||||
const std::string &pattern,
|
||||
const std::vector<HttpRequest::Method> &validMethods = std::vector<HttpRequest::Method>(),
|
||||
const std::vector<std::string> &filters = std::vector<std::string>())
|
||||
const std::vector<any> &filtersAndMethods = std::vector<any>())
|
||||
{
|
||||
std::string path = std::string("/") + HttpApiController<T>::classTypeName();
|
||||
LOG_TRACE << "classname:" << HttpApiController<T>::classTypeName();
|
||||
|
||||
//transform(path.begin(), path.end(), path.begin(), tolower);
|
||||
std::string::size_type pos;
|
||||
|
||||
std::vector<HttpRequest::Method> validMethods;
|
||||
std::vector<std::string> filters;
|
||||
for (auto &filterOrMethod : filtersAndMethods)
|
||||
{
|
||||
if (filterOrMethod.type() == typeid(std::string))
|
||||
{
|
||||
filters.push_back(*any_cast<std::string>(&filterOrMethod));
|
||||
}
|
||||
else if (filterOrMethod.type() == typeid(const char *))
|
||||
{
|
||||
filters.push_back(*any_cast<const char *>(&filterOrMethod));
|
||||
}
|
||||
else if (filterOrMethod.type() == typeid(HttpRequest::Method))
|
||||
{
|
||||
validMethods.push_back(*any_cast<HttpRequest::Method>(&filterOrMethod));
|
||||
}
|
||||
else
|
||||
{
|
||||
std::cerr << "Invalid controller constraint type:" << filterOrMethod.type().name() << std::endl;
|
||||
LOG_ERROR << "Invalid controller constraint type";
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
while ((pos = path.find("::")) != std::string::npos)
|
||||
{
|
||||
path.replace(pos, 2, "/");
|
||||
|
|
Loading…
Reference in New Issue