Drogon:基于 Linux/macOS/Unix/Windows 运行的 HTTP 网络应用框架
Drogon is a C++17/20 based HTTP application framework. Drogon can be used to easily build various types of web application server programs using C++. Drogon is the name of a dragon from the American TV series Game of Thrones, which I really enjoy.
Drogon is a cross-platform framework, It supports Linux, macOS, FreeBSD, OpenBSD, HaikuOS, and Windows. Its main features are as follows:
Unlike most C++ frameworks, the main program of the drogon application can be kept clean and simple. Drogon uses a few tricks to decouple controllers from the main program. The routing settings of controllers can be done through macros or configuration file.
Below is the main program of a typical drogon application:
#include <drogon/drogon.h>
using namespace drogon;
int main()
{
app().setLogPath("./")
.setLogLevel(trantor::Logger::kWarn)
.addListener("0.0.0.0", 80)
.setThreadNum(16)
.enableRunAsDaemon()
.run();
}It can be further simplified by using configuration file as follows:
#include <drogon/drogon.h>
using namespace drogon;
int main()
{
app().loadConfigFile("./config.json").run();
}Drogon provides some interfaces for adding controller logic directly in the main() function, for example, user can register a handler like this in Drogon:
…While such interfaces look intuitive, they are not suitable for complex business logic scenarios. Assuming there are tens or even hundreds of handlers that need to be registered in the framework, isn't it a better practice to implement them separately in their respective classes? So unless your logic is very simple, we don't recommend using above interfaces. Instead, we can create an HttpSimpleController as follows:
…Most of the above programs can be automatically generated by the command line tool drogon_ctl provided by drogon (The command is drogon_ctl create controller TestCtrl). All the user needs to do is add their own business logic. In the example, the controller returns a Hello, world! string when the client accesses the http://ip/test URL.
For JSON format response, we create the controller as follows:
…Let's go a step further and create a demo RESTful API with the HttpController class, as shown below (Omit the source file):
…As you can see, users can use the HttpController to map paths and parameters at the same time. This is a very convenient way to create a RESTful API application.
In addition, you can also find that all handler interfaces are in asynchronous mode, where the response is returned by a callback object. This design is for performance reasons because in asynchronous mode the drogon application can handle a large number of concurrent requests with a small number of threads.
After compiling all of the above source files, we get a very simple web application. This is a good start. For more information, please visit the documentation on GitHub.
Drogon supports cross-compilation, you should define the CMAKE_SYSTEM_NAME in toolchain file, for example:
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)You can disable building options for examples and drogon_ctl by settings BUILD_EXAMPLES and BUILD_CTL to OFF in the toolchain file.
Drogon provides some building options, you can enable or disable them by setting the corresponding variables to ON or OFF in the cmake command line, cmake file etc...
| Option name | Description | Default value |
|---|---|---|
| BUILD_CTL | Build drogon_ctl | ON |
| BUILD_EXAMPLES | Build examples | ON |
| BUILD_ORM | Build orm | ON |
| COZ_PROFILING | Use coz for profiling | OFF |
| BUILD_SHARED_LIBS | Build drogon as a shared lib | OFF |
| BUILD_DOC | Build Doxygen documentation | OFF |
| BUILD_BROTLI | Build Brotli | ON |
| BUILD_YAML_CONFIG | Build yaml config | ON |
| USE_SUBMODULE | Use trantor as a submodule | ON |
This project exists thanks to all the people who contribute code.
Every contribution is welcome. Please refer to the contribution guidelines for more information.
添加在连接池中所有连接上执行 SQL 查询的功能
不带返回值的代码路径
在创建数据库连接时可能出现异常
MQTT
RealIpResolver: IPv6 不受支持 — v6 trust_ips 失败,出现误导性错误, v6 X-Forwarded-For 将被默认丢弃
服务器侧会话图中来自客户端提供会话ID的潜在 HashDoS
[安全性] StaticFileRouter 中的空字节 (%00) 截断破坏了文件类型允许列 (文件泄露)
RedisClient::execCommand* 无法正确分割 EVAL 参数
PostgreSQL重接重试间隔应可配置
在协程 promise 聚合初始化期间抛出异常时出现 SIGSEGV (Clang 21 / nlohmann::json)