mirror of
https://gitee.com/an-tao/drogon.git
synced 2024-11-30 02:37:57 +08:00
Modify some comments in simple_example (#193)
This commit is contained in:
parent
d56b71c8a9
commit
6e8c718a52
@ -34,7 +34,7 @@ class ApiTest : public drogon::HttpController<ApiTest>
|
||||
METHOD_ADD(ApiTest::jsonTest, "/json", Post);
|
||||
METHOD_ADD(ApiTest::formTest, "/form", Post);
|
||||
METHOD_LIST_END
|
||||
// your declaration of processing function maybe like this:
|
||||
|
||||
void get(const HttpRequestPtr &req,
|
||||
std::function<void(const HttpResponsePtr &)> &&callback,
|
||||
int p1,
|
||||
|
@ -117,32 +117,30 @@ class Test : public HttpController<Test>
|
||||
|
||||
using namespace std::placeholders;
|
||||
using namespace drogon;
|
||||
|
||||
/// Some examples in the main function some common functions of drogon. In
|
||||
/// practice, we don't need such a lengthy main function.
|
||||
int main()
|
||||
{
|
||||
std::cout << banner << std::endl;
|
||||
// app().addListener("::1", 8848);
|
||||
// app().addListener("::1", 8848); //ipv6
|
||||
app().addListener("0.0.0.0", 8848);
|
||||
#ifdef USE_OPENSSL
|
||||
// https
|
||||
drogon::app().setSSLFiles("server.pem", "server.pem");
|
||||
// drogon::app().addListener("::1", 8849, true);
|
||||
drogon::app().addListener("0.0.0.0", 8849, true);
|
||||
#endif
|
||||
// app().setThreadNum(4);
|
||||
// trantor::Logger::setLogLevel(trantor::Logger::TRACE);
|
||||
// class function
|
||||
// Class function example
|
||||
app().registerHandler("/api/v1/handle1/{1}/{2}/?p3={3}&p4={4}", &A::handle);
|
||||
app().registerHandler("/api/v1/handle11/{1}/{2}/?p3={3}&p4={4}",
|
||||
&A::staticHandle);
|
||||
// lambda example
|
||||
// Lambda example
|
||||
app().registerHandler(
|
||||
"/api/v1/handle2/{1}/{2}",
|
||||
[](const HttpRequestPtr &req,
|
||||
std::function<void(const HttpResponsePtr &)> &&callback,
|
||||
int a,
|
||||
float b) {
|
||||
// LOG_DEBUG << "int a=" << a;
|
||||
// LOG_DEBUG << "float b=" << b;
|
||||
HttpViewData data;
|
||||
data.insert("title", std::string("ApiTest::get"));
|
||||
std::map<std::string, std::string> para;
|
||||
@ -153,10 +151,11 @@ int main()
|
||||
callback(res);
|
||||
});
|
||||
|
||||
// Functor example
|
||||
B b;
|
||||
// functor example
|
||||
app().registerHandler("/api/v1/handle3/{1}/{2}", b);
|
||||
|
||||
// API example for std::function
|
||||
A tmp;
|
||||
std::function<void(const HttpRequestPtr &,
|
||||
std::function<void(const HttpResponsePtr &)> &&,
|
||||
@ -165,28 +164,30 @@ int main()
|
||||
const std::string &,
|
||||
int)>
|
||||
func = std::bind(&A::handle, &tmp, _1, _2, _3, _4, _5, _6);
|
||||
// api example for std::function
|
||||
app().registerHandler("/api/v1/handle4/{4}/{3}/{1}", func);
|
||||
|
||||
app().setDocumentRoot("./");
|
||||
app().enableSession(60);
|
||||
// start app framework
|
||||
// drogon::app().enableDynamicViewsLoading({"/tmp/views"});
|
||||
|
||||
// Load configuration
|
||||
app().loadConfigFile("config.example.json");
|
||||
auto &json = app().getCustomConfig();
|
||||
if (json.empty())
|
||||
{
|
||||
std::cout << "empty custom config!" << std::endl;
|
||||
}
|
||||
|
||||
// Install custom controller
|
||||
auto ctrlPtr = std::make_shared<CustomCtrl>("Hi");
|
||||
app().registerController(ctrlPtr);
|
||||
|
||||
// Install custom filter
|
||||
auto filterPtr =
|
||||
std::make_shared<CustomHeaderFilter>("custom_header", "yes");
|
||||
app().registerFilter(filterPtr);
|
||||
app().setIdleConnectionTimeout(30s);
|
||||
// Test AOP
|
||||
|
||||
// AOP example
|
||||
app().registerBeginningAdvice(
|
||||
[]() { LOG_DEBUG << "Event loop is running!"; });
|
||||
app().registerNewConnectionAdvice([](const trantor::InetAddress &peer,
|
||||
@ -216,9 +217,6 @@ int main()
|
||||
drogon::AdviceCallback &&acb,
|
||||
drogon::AdviceChainCallback &&accb) {
|
||||
LOG_DEBUG << "preHandling1";
|
||||
// auto resp = HttpResponse::newNotFoundResponse();
|
||||
// acb(resp);
|
||||
// return;
|
||||
accb();
|
||||
});
|
||||
app().registerPostHandlingAdvice([](const drogon::HttpRequestPtr &,
|
||||
@ -235,6 +233,8 @@ int main()
|
||||
app().registerPreHandlingAdvice([](const drogon::HttpRequestPtr &req) {
|
||||
LOG_DEBUG << "preHanding observer";
|
||||
});
|
||||
|
||||
// Output information of all handlers
|
||||
auto handlerInfo = app().getHandlersInfo();
|
||||
for (auto &info : handlerInfo)
|
||||
{
|
||||
@ -264,5 +264,6 @@ int main()
|
||||
}
|
||||
std::cout << std::get<2>(info) << std::endl;
|
||||
}
|
||||
|
||||
app().run();
|
||||
}
|
||||
|
@ -212,7 +212,7 @@ static void loadApp(const Json::Value &app)
|
||||
{
|
||||
// set the number to the number of processors.
|
||||
threadsNum = std::thread::hardware_concurrency();
|
||||
LOG_DEBUG << "The number of processors is " << threadsNum;
|
||||
LOG_TRACE << "The number of processors is " << threadsNum;
|
||||
}
|
||||
if (threadsNum < 1)
|
||||
threadsNum = 1;
|
||||
|
Loading…
Reference in New Issue
Block a user