- 创建drogon框架
drogon_ctl create project test_ctrl
- 添加一个控制器
- 进入controllers目录下
drogon_ctl create controller -h check_ctrl
- 编写主函数
#include <drogon/drogon.h>
int main() {
//Set HTTP listener address and port
//drogon::app().addListener("0.0.0.0",80);
//Load config file
#ifdef NDEBUG
drogon::app().loadConfigFile("./config.json");
#else
drogon::app().loadConfigFile("../../../config.json");
#endif
drogon::app().run();
return 0;
}
配置文件初步介绍,进需要做如何配置
// 这一部分是http监听的端口和ip地址
"listeners": [
{
//address: Ip address,0.0.0.0 by default
"address": "0.0.0.0",
//port: Port number
"port": 8080,
//https: If true, use https for security,false by default
"https": false
}
// ,
// {
// "address": "0.0.0.0",
// "port": 443,
// "https": true,
// //cert,key: Cert file path and key file path, empty by default,0.0.1
// //if empty, use the global setting
// "cert": "",
// "key": "",
// //use_old_tls: enable the TLS1.0/1.1, false by default
// "use_old_tls": false,
// "ssl_conf": [
// //["MinProtocol", "TLSv1.3"]
// ]
// }
],
//------------------
// 这一部分是在程序中的配置文件部分需要使用app().getCustomConfig(),由getCustomConfig得到的变量直接["path"]就可以不需要["custom_config"]["path"]
//custom_config: custom configuration for users. This object can be get by the app().getCustomConfig() method.
"custom_config": {
"path": "D:/aaaa/bbb/",
"time": 8, // 小时:文件名+~小时得到文件的生成时间
}
- 控制器.h文件
#include <drogon/HttpController.h>
#include <drogon/drogon.h>
using namespace drogon;
using namespace std;
class check_ctrl : public drogon::HttpController<check_ctrl>
{
public:
METHOD_LIST_BEGIN
METHOD_ADD(check_ctrl::test, "/test?", Post, Get);
METHOD_LIST_END
void test(const HttpRequestPtr& req, std::function<void(const HttpResponsePtr&)>&& callback);
};
- 控制器.cpp文件
void check_ctrl::test(const HttpRequestPtr& req, std::function<void(const HttpResponsePtr&)>&& callback)
{
Json::Value ret;
// 获取配置文件参数
auto config = app().getCustomConfig();
if (config.empty())
{
ret["success"] = false;
ret["error_msg"] = "Empty custom config!";
callback(HttpResponse::newHttpJsonResponse(ret));
return;
}
// 读取配置文件参数
std::string bz2_file = config["path"].asString(); // 文件地址
int diff_time = config["time"].asInt(); // 文件时间需要小时
// ........
}