drogon/examples/simple_example/main.cc

128 lines
4.9 KiB
C++
Raw Normal View History

2018-05-04 09:51:22 +00:00
#include <iostream>
#include <drogon/HttpAppFramework.h>
2018-06-05 06:40:03 +00:00
#include <drogon/HttpApiController.h>
#include <trantor/utils/Logger.h>
#include <vector>
#include <string>
2018-06-05 06:40:03 +00:00
2018-05-11 07:33:11 +00:00
using namespace drogon;
2018-08-10 14:53:09 +00:00
class A:public DrObjectBase
{
public:
2018-08-02 07:38:06 +00:00
void handle(const HttpRequestPtr& req,
2018-08-26 08:25:35 +00:00
const std::function<void (const HttpResponsePtr &)>&callback,
2018-06-07 01:41:15 +00:00
int p1,const std::string &p2,const std::string &p3,int p4) 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["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);
2018-08-26 08:25:35 +00:00
callback(res);
}
};
2018-08-10 14:53:09 +00:00
class B:public DrObjectBase
2018-06-05 02:43:53 +00:00
{
public:
2018-08-26 08:25:35 +00:00
void operator ()(const HttpRequestPtr& req,const std::function<void (const HttpResponsePtr &)>&callback,int p1,int p2)
2018-06-05 02:43:53 +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["p1"]=std::to_string(p1);
para["p2"]=std::to_string(p2);
data.insert("parameters",para);
auto res=HttpResponse::newHttpViewResponse("ListParaView",data);
2018-08-26 08:25:35 +00:00
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>
{
2018-08-14 08:35:05 +00:00
public:
2018-06-05 06:40:03 +00:00
METHOD_LIST_BEGIN
2018-08-30 02:18:04 +00:00
METHOD_ADD(Test::get,"get/{2}/{1}","drogon::GetFilter");//path will be /api/v1/test/get/{arg2}/{arg1}
METHOD_ADD(Test::list,"/{2}/info","drogon::GetFilter");//path will be /api/v1/test/{arg2}/info
2018-06-05 06:40:03 +00:00
METHOD_LIST_END
2018-08-26 08:25:35 +00:00
void get(const HttpRequestPtr& req,const std::function<void (const HttpResponsePtr &)>&callback,int p1,int p2) const
2018-06-05 06:40:03 +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["p1"]=std::to_string(p1);
para["p2"]=std::to_string(p2);
data.insert("parameters",para);
auto res=HttpResponse::newHttpViewResponse("ListParaView",data);
2018-08-26 08:25:35 +00:00
callback(res);
2018-06-05 06:40:03 +00:00
}
2018-08-26 08:25:35 +00:00
void list(const HttpRequestPtr& req,const std::function<void (const HttpResponsePtr &)>&callback,int p1,int p2) const
2018-06-05 06:40:03 +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["p1"]=std::to_string(p1);
para["p2"]=std::to_string(p2);
data.insert("parameters",para);
auto res=HttpResponse::newHttpViewResponse("ListParaView",data);
2018-08-26 08:25:35 +00:00
callback(res);
2018-06-05 06:40:03 +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-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-08-31 07:17:09 +00:00
//#ifdef USE_OPENSSL
// //https
// drogon::HttpAppFramework::instance().setSSLFiles("server.pem","server.pem");
// drogon::HttpAppFramework::instance().addListener("0.0.0.0",4430,true);
// drogon::HttpAppFramework::instance().addListener("0.0.0.0",4431,true);
//#endif
2018-06-08 09:21:36 +00:00
drogon::HttpAppFramework::instance().setThreadNum(4);
trantor::Logger::setLogLevel(trantor::Logger::TRACE);
2018-06-05 02:43:53 +00:00
//class function
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-08-26 08:25:35 +00:00
drogon::HttpAppFramework::registerHttpApiMethod("/api/v1/handle2/{1}/{2}",[](const HttpRequestPtr&req,const std::function<void (const HttpResponsePtr &)>&callback,int a,float b){
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);
2018-08-26 08:25:35 +00:00
callback(res);
});
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-05 02:43:53 +00:00
A tmp;
2018-08-26 08:25:35 +00:00
std::function<void(const HttpRequestPtr&,const std::function<void (const HttpResponsePtr &)>&,int,const std::string &,const std::string &,int)>
2018-06-05 02:43:53 +00:00
func=std::bind(&A::handle,&tmp,_1,_2,_3,_4,_5,_6);
//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
2018-08-24 03:21:02 +00:00
drogon::HttpAppFramework::instance().setDocumentRoot("./");
2018-06-11 09:11:06 +00:00
//start app framework
2018-09-02 08:21:12 +00:00
drogon::HttpAppFramework::instance().enableDynamicViewsLoading({"/tmp/views"});
2018-05-11 07:33:11 +00:00
drogon::HttpAppFramework::instance().run();
2018-05-04 09:51:22 +00:00
}