Guide · Open source
How to use Drogon: install, build and write a C++ HTTP server
Updated · written with AI from the project's own documentation and checked against our catalogue data
Drogon is an open-source HTTP web application framework for writing web server programs in C++. It is licensed under MIT and runs on Linux, macOS, Unix-like systems and Windows. This guide walks through getting the source, the basic shape of a Drogon program, controllers, JSON responses and the main features you'll use, so you can build a small C++ backend or REST API.
Quick start
- 1Clone the repository. Run git clone https://github.com/drogonframework/drogon to get the source code.
- 2Read the build instructions. Check the repository files for how to build Drogon, since the latest release provides no binaries.
- 3Write your main program. Create a main() that calls app() and either sets options directly or loads a config file with loadConfigFile.
- 4Generate a controller. Use drogon_ctl create controller with a class name to scaffold a controller file.
- 5Add your handler logic. Implement asyncHandleHttpRequest and return a response through the callback, using newHttpJsonResponse for JSON.
- 6Test the endpoint. Run the program and request the path you registered, such as /test or /json, to confirm the response.
What you need before you start
Drogon is a C++ only framework. The code and the build process are C++, so you need to be comfortable writing and compiling C++ code before you start. The project's README does not list any prerequisites, so check the repository's own build documentation for the tools you need.
Drogon is cross-platform and the README names Linux, macOS, FreeBSD, OpenBSD, HaikuOS and Windows as supported platforms.
- Working knowledge of C++ and building C++ projects
- A supported operating system such as Linux, macOS or Windows
- Optional: PostgreSQL, MySQL/MariaDB, SQLite3 or Redis if you want built-in database or cache access
Install Drogon by cloning and building from source
The README's install method is clone-and-build. Drogon does not ship a ready-made binary or installer in the latest release, so you get the source code from the repository and compile it yourself.
Clone the repository with the command below, then follow the build instructions in the project files.
- The latest release lists no binaries, so plan to build from source
- The README mentions CMake and Conan Center packaging badges, so these are the build routes the project points to
git clone https://github.com/drogonframework/drogonWrite a minimal Drogon application
A typical Drogon main program is short. The app() object lets you set the log path, log level, listening address and port, thread count, and whether the program runs as a daemon, then call run().
You can also move these settings into a configuration file and load it with loadConfigFile, which keeps the main program clean.
#include <drogon/drogon.h>
using namespace drogon;
int main()
{
app().loadConfigFile("./config.json").run();
}Create a controller that returns a response
Drogon keeps controller logic in separate classes instead of the main program. A controller that inherits from HttpSimpleController declares its path with PATH_ADD and implements asyncHandleHttpRequest. The README's example returns an HTML body for the /test path.
The README notes that the drogon_ctl command-line tool can generate these files for you. The command it gives is drogon_ctl create controller TestCtrl, and you then add your own business logic.
- Declare the class in a header file with PATH_LIST_BEGIN and PATH_LIST_END around PATH_ADD entries
- Implement asyncHandleHttpRequest in the source file and send the result through the callback
void TestCtrl::asyncHandleHttpRequest(const HttpRequestPtr& req,
std::function<void (const HttpResponsePtr &)> &&callback)
{
// write your application logic here
HttpResponsePtr resp = HttpResponse::newHttpResponse();
resp->setBody("<p>Hello, world!</p>");
resp->setExpiredTime(0);
callback(resp);
}Return JSON from an API endpoint
For JSON, the README shows a controller that builds a Json::Value and sends it with HttpResponse::newHttpJsonResponse. Drogon lists JSON request and response support as a feature aimed at RESTful API development.
Json::Value ret;
ret["message"] = "Hello, World!";
HttpResponsePtr resp = HttpResponse::newHttpJsonResponse(ret);
callback(resp);Build a RESTful API with HttpController
For several related endpoints, the README recommends HttpController. You map methods and paths with METHOD_ADD, and parameters in braces such as {id} are passed to your handler. The README's example places the controller in the api::v1 namespace, so its path becomes /api/v1/User/{arg1}.
All handler interfaces are asynchronous: you return the response through a callback rather than returning it directly.
- Use METHOD_LIST_BEGIN and METHOD_LIST_END around METHOD_ADD entries
- Add HTTP method names such as Get or Post after the path
METHOD_ADD(User::getInfo, "/{id}", Get);
METHOD_ADD(User::newUser, "/{name}", Post);What people use it for
- A C++ REST API backend that serves JSON to a web or mobile front end
- A WebSocket server that needs both server-side and client-side support
- A server-rendered web app that fills CSP template views with data from controllers
- A service that reads and writes PostgreSQL, MySQL, SQLite or Redis without blocking on I/O
Frequently asked questions
Is Drogon free to use?
Drogon is released under the MIT licence, as listed on its project page. The MIT licence generally permits use, modification and distribution, but read the LICENSE file in the repository for the exact terms.
Does Drogon require C++ experience?
Yes. Drogon's code and build process are C++ only, so developers without C++ experience will find it hard to use. Simpler scripts or static sites are better served by a higher-level language framework.
Is there a Drogon installer or binary download?
The latest release lists no binaries. The project's install method is to clone the repository with git clone https://github.com/drogonframework/drogon and build it from source.
Which databases does Drogon support?
The README lists asynchronous support for PostgreSQL and MySQL (MariaDB), asynchronous support for SQLite3 through a thread pool, and asynchronous support for Redis. It also provides a lightweight ORM for mapping objects to database tables.
What are the alternatives to Drogon?
Projects often compared with Drogon include Crow, Pistache, cpp-httplib and Boost.Beast. Choose based on which features you need, such as built-in database support, WebSockets or a minimal header-only approach.
Which operating systems does Drogon run on?
The README says Drogon is cross-platform and supports Linux, macOS, FreeBSD, OpenBSD, HaikuOS and Windows. It uses epoll on Linux and kqueue under macOS and FreeBSD for its non-blocking network I/O.
New agent skills, repos and AI tools — every week
The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.
Our verdict
Drogon suits C++ developers who need a fast, asynchronous HTTP server, REST API or WebSocket backend with built-in database support. Teams without C++ experience, or anyone who needs a ready-made binary or a quick script or static site, should pick a higher-level framework instead.
Install options & downloads