HTTP(S)服务:Header Only的轻量级C++ Http(S)服务

简介

在Github上找到一个比较好用的c++轻量级的http(s)服务,只需要包含一个头文件,使用非常方便。项目的Github地址如下:

https://github.com/yhirose/cpp-httplib

服务

项目的使用非常方便,声明Server对象,开启监听端口即可:

Server svr;
svr.listen("0.0.0.0", 1234);

API

使用如下代码声明一个Post服务,api返回值为“Hello"。

svr.Post("/api/hello", [&](const Request& req, Response& res) {
    std::string response = "Hello";
    res.set_content(response, "text/plain");
});

文件

使用如下代码配置一个Web 文件服务器:

svr.set_mount_point("/", "./webclient");

Https配置

开启Https服务需要集成OpenSSL1.1.0,使用httplib的commit为6b22409217e58b81271c1d50f88265481d042fa8.
使用如下代码开启https服务:

SSLServer sslServer("./cert.pem", "./key.pem");

生成自签名的cert.pem与key.pem使用如下命令:

openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 365
openssl req  -nodes -new -x509  -keyout key.pem -out cert.pem

猜你喜欢

转载自blog.csdn.net/chenxiemin/article/details/106893220