2018-05-04 09:51:22 +00:00
|
|
|
#include <iostream>
|
2018-05-09 08:14:21 +00:00
|
|
|
#include <drogon/HttpAppFramework.h>
|
2018-06-05 06:40:03 +00:00
|
|
|
#include <drogon/HttpApiController.h>
|
2018-05-09 08:14:21 +00:00
|
|
|
#include <trantor/utils/Logger.h>
|
2018-06-04 11:00:35 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
2018-06-05 06:40:03 +00:00
|
|
|
|
2018-05-11 07:33:11 +00:00
|
|
|
using namespace drogon;
|
2018-06-04 11:00:35 +00:00
|
|
|
class A
|
|
|
|
{
|
|
|
|
public:
|
2018-06-05 02:43:53 +00:00
|
|
|
void handle(const HttpRequest& req,
|
|
|
|
const std::function<void (HttpResponse &)>&callback,
|
2018-06-07 01:41:15 +00:00
|
|
|
int p1,const std::string &p2,const std::string &p3,int p4) const
|
2018-06-04 11:00:35 +00:00
|
|
|
{
|
2018-06-07 01:41:15 +00:00
|
|
|
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);
|
2018-06-04 11:00:35 +00:00
|
|
|
}
|
|
|
|
};
|
2018-06-05 02:43:53 +00:00
|
|
|
class B
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void operator ()(const HttpRequest& req,const std::function<void (HttpResponse &)>&callback,int p1,int p2)
|
|
|
|
{
|
2018-06-07 01:41:15 +00:00
|
|
|
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);
|
2018-06-05 02:43:53 +00:00
|
|
|
}
|
|
|
|
};
|
2018-06-05 06:40:03 +00:00
|
|
|
namespace api
|
|
|
|
{
|
|
|
|
namespace v1
|
|
|
|
{
|
|
|
|
class Test:public HttpApiController<Test>
|
|
|
|
{
|
|
|
|
METHOD_LIST_BEGIN
|
2018-06-06 08:34:02 +00:00
|
|
|
METHOD_ADD(Test::get,"/{2}/{1}",1,"drogon::GetFilter");//path will be /api/v1/test/get/{arg2}/{arg1}
|
2018-06-07 01:41:15 +00:00
|
|
|
METHOD_ADD(Test::list,"/{2}/info",0,"drogon::GetFilter");//path will be /api/v1/test/{arg2}/info
|
2018-06-05 06:40:03 +00:00
|
|
|
METHOD_LIST_END
|
|
|
|
void get(const HttpRequest& req,const std::function<void (HttpResponse &)>&callback,int p1,int p2) const
|
|
|
|
{
|
2018-06-07 01:41:15 +00:00
|
|
|
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);
|
2018-06-05 06:40:03 +00:00
|
|
|
}
|
|
|
|
void list(const HttpRequest& req,const std::function<void (HttpResponse &)>&callback,int p1,int p2) const
|
|
|
|
{
|
2018-06-07 01:41:15 +00:00
|
|
|
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);
|
2018-06-05 06:40:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2018-06-04 15:45:21 +00:00
|
|
|
using namespace std::placeholders;
|
2018-05-11 01:25:17 +00:00
|
|
|
int main()
|
|
|
|
{
|
2018-05-11 09:39:52 +00:00
|
|
|
|
2018-05-04 09:51:22 +00:00
|
|
|
std::cout<<banner<<std::endl;
|
2018-06-04 15:45:21 +00:00
|
|
|
|
2018-05-31 05:55:33 +00:00
|
|
|
drogon::HttpAppFramework::instance().addListener("0.0.0.0",12345);
|
|
|
|
drogon::HttpAppFramework::instance().addListener("0.0.0.0",8080);
|
2018-06-08 09:21:36 +00:00
|
|
|
drogon::HttpAppFramework::instance().setThreadNum(4);
|
2018-06-04 11:00:35 +00:00
|
|
|
trantor::Logger::setLogLevel(trantor::Logger::TRACE);
|
2018-06-05 02:43:53 +00:00
|
|
|
//class function
|
2018-06-08 03:15:29 +00:00
|
|
|
drogon::HttpAppFramework::registerHttpApiMethod("/api/v1/handle1/{1}/{2}/?p3={3}&p4={4}",&A::handle);
|
2018-06-07 01:41:15 +00:00
|
|
|
//lambda example
|
2018-06-07 02:29:41 +00:00
|
|
|
drogon::HttpAppFramework::registerHttpApiMethod("/api/v1/handle2/{1}/{2}",[](const HttpRequest&req,const std::function<void (HttpResponse &)>&callback,int a,float b){
|
2018-06-04 15:45:21 +00:00
|
|
|
LOG_DEBUG<<"int a="<<a;
|
2018-06-08 04:22:11 +00:00
|
|
|
LOG_DEBUG<<"float b="<<b;
|
2018-06-07 01:41:15 +00:00
|
|
|
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);
|
2018-06-04 15:45:21 +00:00
|
|
|
});
|
2018-06-05 02:43:53 +00:00
|
|
|
|
|
|
|
B b;
|
2018-06-07 01:41:15 +00:00
|
|
|
//functor example
|
2018-06-07 02:29:41 +00:00
|
|
|
drogon::HttpAppFramework::registerHttpApiMethod("/api/v1/handle3/{1}/{2}",b);
|
2018-06-06 08:34:02 +00:00
|
|
|
|
2018-06-05 02:43:53 +00:00
|
|
|
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);
|
2018-06-06 10:29:05 +00:00
|
|
|
//api example for std::function
|
2018-06-07 02:29:41 +00:00
|
|
|
drogon::HttpAppFramework::registerHttpApiMethod("/api/v1/handle4/{4}/{3}/{1}",func);
|
2018-06-11 09:11:06 +00:00
|
|
|
|
|
|
|
//start app framework
|
2018-06-13 09:30:40 +00:00
|
|
|
//drogon::HttpAppFramework::instance().enableDynamicSharedLibLoading();
|
2018-05-11 07:33:11 +00:00
|
|
|
drogon::HttpAppFramework::instance().run();
|
2018-06-04 11:00:35 +00:00
|
|
|
|
2018-05-04 09:51:22 +00:00
|
|
|
}
|