当前位置: 首页 > news >正文

c++使用http请求-drogon框架

  1. 创建drogon框架
    drogon_ctl create project test_ctrl
  1. 添加一个控制器
  • 进入controllers目录下
    drogon_ctl create controller -h check_ctrl
  1. 编写主函数

#include <drogon/drogon.h>
int main() {//Set HTTP listener address and port//drogon::app().addListener("0.0.0.0",80);//Load config file#ifdef NDEBUGdrogon::app().loadConfigFile("./config.json");
#elsedrogon::app().loadConfigFile("../../../config.json");
#endifdrogon::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,  // 小时:文件名+~小时得到文件的生成时间}
  1. 控制器.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_BEGINMETHOD_ADD(check_ctrl::test, "/test?", Post, Get);METHOD_LIST_ENDvoid test(const HttpRequestPtr& req, std::function<void(const HttpResponsePtr&)>&& callback);
};
  1. 控制器.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();      // 文件时间需要小时 // ........
}
http://www.lryc.cn/news/163958.html

相关文章:

  • 幼儿棒球运动宣传介绍·野球6号位
  • grpc多语言通信之GO和DART
  • 基于FPGA的RGB图像转Ycbcr实现,包括tb测试文件以及MATLAB辅助验证
  • centos 编译安装的php多版本 切换
  • Unity 性能优化之Shader分析处理函数ShaderUtil.HasProceduralInstancing: 深入解析与实用案例
  • 2023数学建模国赛E题黄河水沙监测数据分析完整代码分析+处理结果+思路文档
  • 玩转Mysql系列 - 第19篇:游标详解
  • 【量化分析】Python 布林线( Bollinger)概念
  • MySQL的权限管理与远程访问
  • 在Qt创建的UI中放一个显示点云的窗口(PCL+QT5)
  • element ui el-table分页多选功能
  • 理解网络通信的基础:OSI七层模型与TCP/IP五层模型
  • Python爬虫-爬取文档内容,如何去掉文档中的表格,并保存正文内容
  • 【使用Cpolar和Qchan搭建自己的个人图床】
  • flutter解决多个类名重名问题
  • 微信小程序 按钮颜色
  • 【云原生】kubectl常用命令大全
  • git pull
  • C++学习之运算符与表达式
  • vue使用谷歌地图实现地点查询
  • 前端该了解的网络知识
  • python3在虚拟环境实用vscode调试错误输出ModuleNotFoundError: No module named ‘django‘解决方法
  • 如何获得一个Oracle 23c免费开发者版
  • 机器学习策略二——优化深度学习系统
  • Pytorch Advanced(三) Neural Style Transfer
  • 英飞凌TC3xx--深度手撕HSM安全启动(三)--TC3xx HSM系统架构
  • 黑马JVM总结(五)
  • C语言入门Day_18 判断和循坏的小结
  • mac 好用的工具推荐
  • 星际争霸之小霸王之小蜜蜂(十二)--猫有九条命