Drogon is a Web Framework written with C++ that works on both C++14 and C++17 versions.
The TechEmpower website (ranking link) has some benchmark services that test the performance of some technologies and among these tests, it analyzed the performance of Web Frameworks for Back-end and Drogon was the first placed in tests with 105 frameworks, among them: Rails, Django, NestJS, Laravel and others.
To install, you will first need some dependencies at compile time and at run time. They are:
For systems that use APT: Debian, Ubuntu, Mint and derivatives, just run this command below:
sudo apt install git gcc g++ cmake libjsoncpp-dev uuid-dev openssl libssl-dev zlib1g-dev
For systems using Portage: Gentoo, Funtoo and derivatives:
sudo emerge dev-vcs/git jsoncpp ossp-uuid openssl
For systems that use RPM: Fedora, Red Hat and derivatives
sudo dnf install git gcc gcc-c++ cmake libuuid-devel openssl-devel zlib-devel
If in the future you will develop applications that use databases (which I believe so), it is extremely important that you install them before installing Drogon. It can be one of those listed below:
git clone https://github.com/an-tao/drogon
cd drogon
git submodule update --init
mkdir build
cd build
cmake ..
make && sudo make install
For this basic example we are going to create a static page without querying the database
cd $HOME
drogon_ctl create project my-project
Now my my-project has the following files:
my-project/
├── build
├── CMakeLists.txt
├── config.json
├── controllers
├── filters
├── main.cc
├── models
│ └── model.json
├── plugins
└── views
6 directories, 4 files
The original main.cc
file is on port 80
, but this could be a conflict on your system. Then change, for example, to port 8080
.
If you also have a problem with port
8080
, use port1024
or try a port bigger than it that is not being used, to find out if it is being used use nmap, for example, to find out. Do not use ports less than 1024 as they should only be used for privileged users (root
, for example).
cd my-project
vim main.cc
With the following content:
#include <drogon/HttpAppFramework.h>
int main() {
drogon::app().addListener("0.0.0.0",8080);
drogon::app().run();
return 0;
}
And compile:
cd build
cmake ..
make
Now run the binary file that was created inside the directory you are in (build
). In my case, as I called the project my-project
, there is a binary with that name, so just run it:
./my-project
Access the address: http://0.0.0.0:8080/ . And accessing it you will already find a beautiful 404
:
To “take down” the server’s execution, press: Ctrl + C
. But that means it’s already working. Then just add an HTML page (still inside the build
directory) and access it again:
echo '<h1>Server with Drogon C++, running cool!</h1>' > index.html
./my-project
And go to: http://0.0.0.0:8080/
We are going to create a test controller named ListController
just to display the phrase: “My controller list”. At the root of your project, enter the directory: controller
and run the following command to create the controller.
cd controller/
drogon_ctl create controller ListController
The output should be:
create a http simple controller: ListController.
Two files were created: ListController.cc
and ListController.h
First, let’s edit the file: ListController.h
and add these two lines:
PATH_ADD("/",Get,Post);
PATH_ADD("/list",Get);
The final file looks like this:
#pragma once
#include <drogon/HttpSimpleController.h>
using namespace drogon;
class ListController:public drogon::HttpSimpleController<ListController>{
public:
virtual void asyncHandleHttpRequest(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&callback) override;
PATH_LIST_BEGIN
PATH_ADD("/",Get,Post);
PATH_ADD("/list",Get);
PATH_LIST_END
};
And the ListController.cc
file should look like this:
#include "ListController.h"
void ListController::asyncHandleHttpRequest(const HttpRequestPtr& req,
std::function<void (const HttpResponsePtr &)> &&callback){
auto resp=HttpResponse::newHttpResponse();
resp->setStatusCode(k200OK);
resp->setContentTypeCode(CT_TEXT_HTML);
resp->setBody("My controller list");
callback(resp);
}
Recompile the project and access:
cd ../build
cmake ..
make
./my-project
Now both at the root (http://localhost:8080/ via get or post) and at http://localhost:8080/list we will get content.
If you are using Postman you will notice that the root will return the information for both GET and POST, but the /list
will only return for GET.
The address of the official Drogon on GitHub is: https://github.com/an-tao/drogon, there you can get information of all documentation to install and develop your web applications.
The video is in Brazilian Portuguese, but you can use subtitles and translate into your language.