Drogon: A Powerful C++ Library

Drogon is an HTTP application framework based on C++17/20 that helps developers easily build various types of web application server programs. The name Drogon is derived from a dragon in the popular TV series “Game of Thrones.” It is a cross-platform framework that supports Linux, macOS, FreeBSD, OpenBSD, HaikuOS, and Windows.

High-Performance Network I/O

Drogon uses a non-blocking I/O network library based on epoll (using kqueue on macOS/FreeBSD), providing high concurrency and high-performance network I/O. This design allows Drogon to maintain efficient performance while handling a large number of concurrent requests. On a single-core Ryzen 3700X, Drogon can handle over 150,000 HTTP requests per second.

Fully Asynchronous Programming Model

Drogon offers a fully asynchronous programming model, meaning applications can handle multiple requests without blocking. This model not only improves performance but also allows applications to utilize system resources more efficiently. Drogon also supports C++ coroutines, enabling developers to write asynchronous code without worrying about callback hell.

Powerful Feature Support

Drogon supports Http1.0/1.1 (both server-side and client-side) and provides a template-based simple reflection mechanism, completely decoupling the main program framework, controllers, and views. It also supports cookies and built-in session management, making it easy for developers to implement user authentication and session management.

Drogon supports backend rendering, allowing controllers to generate data and pass it to views to create HTML pages. Views are described by CSP template files, and C++ code can be embedded into HTML pages through CSP tags. Additionally, Drogon provides a lightweight command-line tool <span>drogon_ctl</span> that can automatically generate C++ code files.

Drogon also supports dynamic loading of view pages (dynamic compilation and loading), providing a convenient and flexible routing solution. It supports filter chains that can execute unified logic before processing HTTP requests, such as login validation and HTTP method constraint validation.

Security and Scalability

Drogon supports HTTPS (based on OpenSSL) and WebSocket (both server-side and client-side). It also supports JSON format for requests and responses, making it ideal for developing Restful API applications. Furthermore, Drogon supports file downloads and uploads, as well as gzip and brotli compression for transmission.

Drogon supports asynchronous read and write operations for databases based on non-blocking I/O (PostgreSQL and MySQL/MariaDB databases), as well as asynchronous read and write operations for sqlite3 databases based on thread pools. It also supports asynchronous read and write operations for Redis.

Development Efficiency

Drogon provides a lightweight command-line tool <span>drogon_ctl</span> that simplifies the creation of various classes in Drogon and the generation of view code. For example, developers can quickly create a controller using the command <span>drogon_ctl create controller TestCtrl</span>.

Example Code

Here is a simple example of a 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();
}

The main program of Drogon can remain concise, as the routing setup for controllers can be done through macros or configuration files. Developers can also directly add controller logic in the main function, such as registering a handler:

app().registerHandler("/test?username={name}",
                    [](const HttpRequestPtr& req,
                       std::function<void (const HttpResponsePtr &)> &&callback,
                       const std::string &name)
                    {
                        Json::Value json;
                        json["result"]="ok";
                        json["message"]=std::string("hello,")+name;
                        auto resp=HttpResponse::newHttpJsonResponse(json);
                        callback(resp);
                    },
                    {Get,"LoginFilter"});

Conclusion

Drogon is a powerful and efficient C++ web framework that offers rich features and flexible programming models, suitable for developing high-performance web applications. Whether you need to quickly develop simple web applications or build complex Restful APIs, Drogon is a choice worth trying.

If you find it useful, please follow, like, and share. You can star our public account to receive updates promptly.

Leave a Comment