modify example of api controller

This commit is contained in:
an-tao 2018-06-07 09:41:15 +08:00
parent 6aceffd6ef
commit 43744f5df6
7 changed files with 102 additions and 56 deletions

View File

@ -11,12 +11,23 @@ class A
public:
void handle(const HttpRequest& req,
const std::function<void (HttpResponse &)>&callback,
int aa,const std::string &a,const std::string &b,int haha) const
int p1,const std::string &p2,const std::string &p3,int p4) const
{
LOG_DEBUG<<"int aa="<<aa;
LOG_DEBUG<<"string a="<<a;
LOG_DEBUG<<"string b="<<b;
LOG_DEBUG<<"int haha="<<haha;
LOG_DEBUG<<"int p1="<<p1;
LOG_DEBUG<<"string p2="<<p2;
LOG_DEBUG<<"string p3="<<p3;
LOG_DEBUG<<"int p4="<<p4;
HttpViewData data;
data.insert("title",std::string("ApiTest::get"));
std::map<std::string,std::string> para;
para["int p1"]=std::to_string(p1);
para["string p2"]=p2;
para["string p3"]=p3;
para["int p4"]=std::to_string(p4);
data.insert("parameters",para);
auto res=HttpResponse::newHttpViewResponse("ListParaView",data);
callback(*res);
}
};
class B
@ -26,6 +37,14 @@ public:
{
LOG_DEBUG<<"int p1="<<p1;
LOG_DEBUG<<"int p2="<<p2;
HttpViewData data;
data.insert("title",std::string("ApiTest::get"));
std::map<std::string,std::string> para;
para["p1"]=std::to_string(p1);
para["p2"]=std::to_string(p2);
data.insert("parameters",para);
auto res=HttpResponse::newHttpViewResponse("ListParaView",data);
callback(*res);
}
};
namespace api
@ -36,17 +55,35 @@ namespace api
{
METHOD_LIST_BEGIN
METHOD_ADD(Test::get,"/{2}/{1}",1,"drogon::GetFilter");//path will be /api/v1/test/get/{arg2}/{arg1}
METHOD_ADD(Test::list,"/{2}/list",0,"drogon::GetFilter");//path will be /api/v1/test/{arg2}/list
METHOD_ADD(Test::list,"/{2}/info",0,"drogon::GetFilter");//path will be /api/v1/test/{arg2}/info
METHOD_LIST_END
void get(const HttpRequest& req,const std::function<void (HttpResponse &)>&callback,int p1,int p2) const
{
LOG_DEBUG<<"int p1="<<p1;
LOG_DEBUG<<"int p2="<<p2;
HttpViewData data;
data.insert("title",std::string("ApiTest::get"));
std::map<std::string,std::string> para;
para["p1"]=std::to_string(p1);
para["p2"]=std::to_string(p2);
data.insert("parameters",para);
auto res=HttpResponse::newHttpViewResponse("ListParaView",data);
callback(*res);
}
void list(const HttpRequest& req,const std::function<void (HttpResponse &)>&callback,int p1,int p2) const
{
LOG_DEBUG<<"int p1="<<p1;
LOG_DEBUG<<"int p2="<<p2;
LOG_DEBUG<<"int p1="<<p1;
LOG_DEBUG<<"int p2="<<p2;
HttpViewData data;
data.insert("title",std::string("ApiTest::get"));
std::map<std::string,std::string> para;
para["p1"]=std::to_string(p1);
para["p2"]=std::to_string(p2);
data.insert("parameters",para);
auto res=HttpResponse::newHttpViewResponse("ListParaView",data);
callback(*res);
}
};
}
@ -62,21 +99,29 @@ int main()
trantor::Logger::setLogLevel(trantor::Logger::TRACE);
//class function
drogon::HttpAppFramework::registerHttpApiMethod("/api/v1/handle1","/{1}/{2}/{3}/{4}",&A::handle);
//lambda
drogon::HttpAppFramework::registerHttpApiMethod("/api/v1/handle2","",[](const HttpRequest&req,const std::function<void (HttpResponse &)>&callback,int a,float b){
//lambda example
drogon::HttpAppFramework::registerHttpApiMethod("/api/v1/handle2","/{1}/{2}",[](const HttpRequest&req,const std::function<void (HttpResponse &)>&callback,int a,float b){
LOG_DEBUG<<"int a="<<a;
LOG_DEBUG<<"flaot b="<<b;
HttpViewData data;
data.insert("title",std::string("ApiTest::get"));
std::map<std::string,std::string> para;
para["a"]=std::to_string(a);
para["b"]=std::to_string(b);
data.insert("parameters",para);
auto res=HttpResponse::newHttpViewResponse("ListParaView",data);
callback(*res);
});
B b;
//functor
//functor example
drogon::HttpAppFramework::registerHttpApiMethod("/api/v1/handle3","/{1}/{2}",b);
A tmp;
std::function<void(const HttpRequest&,const std::function<void (HttpResponse &)>&,int,const std::string &,const std::string &,int)>
func=std::bind(&A::handle,&tmp,_1,_2,_3,_4,_5,_6);
//api example for std::function
drogon::HttpAppFramework::registerHttpApiMethod("/api/v1/handle4","",func);
drogon::HttpAppFramework::registerHttpApiMethod("/api/v1/handle4","/{4}/{3}/{1}",func);
LOG_DEBUG<<drogon::DrObjectBase::demangle(typeid(func).name());
drogon::HttpAppFramework::instance().run();

View File

@ -71,6 +71,7 @@ namespace drogon
T::initMethods();
}
};
//use static value to register controller method in framework before main();
static methodRegister _register;
virtual void* touch()
{

View File

@ -21,12 +21,8 @@ std::shared_ptr<HttpResponse> DeleteFilter::doFilter(const HttpRequest& req)
{
return nullptr;
}
auto res=std::shared_ptr<HttpResponse>(HttpResponse::newHttpResponse());
if(res)
{
res->setStatusCode(HttpResponse::k404NotFound);
res->setCloseConnection(true);
}
auto res=drogon::HttpResponse::notFoundResponse();
return res;
}

View File

@ -21,12 +21,8 @@ std::shared_ptr<HttpResponse> GetFilter::doFilter(const HttpRequest& req)
{
return nullptr;
}
auto res=std::shared_ptr<HttpResponse>(HttpResponse::newHttpResponse());
if(res)
{
res->setStatusCode(HttpResponse::k404NotFound);
res->setCloseConnection(true);
}
auto res=drogon::HttpResponse::notFoundResponse();
return res;
}

View File

@ -64,6 +64,11 @@ namespace drogon
typedef std::shared_ptr<Session> SessionPtr;
std::unique_ptr<CacheMap<std::string,SessionPtr>> _sessionMapPtr;
bool doFilters(const std::vector<std::string> &filters,
const HttpRequest& req,
const std::function<void (HttpResponse &)> & callback,
bool needSetJsessionid,
const std::string &session_id);
//
struct ControllerAndFiltersName
{
@ -221,6 +226,34 @@ void HttpAppFrameworkImpl::run()
loop.loop();
}
bool HttpAppFrameworkImpl::doFilters(const std::vector<std::string> &filters,
const HttpRequest& req,
const std::function<void (HttpResponse &)> & callback,
bool needSetJsessionid,
const std::string &session_id)
{
LOG_TRACE<<"filters count:"<<filters.size();
for(auto filter:filters)
{
auto _object=std::shared_ptr<DrObjectBase>(DrClassMap::newObject(filter));
auto _filter = std::dynamic_pointer_cast<HttpFilterBase>(_object);
if(_filter)
{
auto resPtr=_filter->doFilter(req);
if(resPtr)
{
if(needSetJsessionid)
resPtr->addCookie("JSESSIONID",session_id);
callback(*resPtr);
return true;
}
} else
{
LOG_ERROR<<"filter "<<filter<<" not found";
}
}
return false;
}
void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequest& req,const std::function<void (HttpResponse &)> & callback)
{
@ -315,28 +348,9 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequest& req,const std::func
//fix me!need mutex;
if(_simpCtrlMap.find(pathLower)!=_simpCtrlMap.end())
{
auto filters=_simpCtrlMap[pathLower].filtersName;
LOG_TRACE<<"filters count:"<<filters.size();
for(auto filter:filters)
{
LOG_TRACE<<"filters in path("<<pathLower<<"):"<<filter;
auto _object=std::shared_ptr<DrObjectBase>(DrClassMap::newObject(filter));
auto _filter = std::dynamic_pointer_cast<HttpFilterBase>(_object);
if(_filter)
{
auto resPtr=_filter->doFilter(req);
if(resPtr)
{
if(needSetJsessionid)
resPtr->addCookie("JSESSIONID",session_id);
callback(*resPtr);
return;
}
} else
{
LOG_ERROR<<"filter "<<filter<<" not found";
}
}
auto &filters=_simpCtrlMap[pathLower].filtersName;
if(doFilters(filters,req,callback,needSetJsessionid,session_id))
return;
auto controller=_simpCtrlMap[pathLower].controller;
std::string ctrlName = _simpCtrlMap[pathLower].controllerName;
if(!controller)
@ -378,6 +392,9 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequest& req,const std::func
size_t ctlIndex=i-1;
auto &binder=_apiCtrlVector[ctlIndex];
LOG_DEBUG<<"got api access,regex="<<binder.pathParameterPattern;
auto &filters=binder.filtersName;
if(doFilters(filters,req,callback,needSetJsessionid,session_id))
return;
std::vector<std::string> params(binder.parameterPlaces.size());
std::smatch r;
if(std::regex_match(req.path(),r,std::regex(binder.pathParameterPattern)))

View File

@ -21,12 +21,7 @@ std::shared_ptr<HttpResponse> PostFilter::doFilter(const HttpRequest& req)
{
return nullptr;
}
auto res=std::shared_ptr<HttpResponse>(HttpResponse::newHttpResponse());
if(res)
{
res->setStatusCode(HttpResponse::k404NotFound);
res->setCloseConnection(true);
}
auto res=drogon::HttpResponse::notFoundResponse();
return res;
}

View File

@ -21,12 +21,8 @@ std::shared_ptr<HttpResponse> PutFilter::doFilter(const HttpRequest& req)
{
return nullptr;
}
auto res=std::shared_ptr<HttpResponse>(HttpResponse::newHttpResponse());
if(res)
{
res->setStatusCode(HttpResponse::k404NotFound);
res->setCloseConnection(true);
}
auto res=drogon::HttpResponse::notFoundResponse();
return res;
}