The main function of the program is to make it easy for users to create various drogon project files. Use the dg_ctl help command to see the functions it supports, as follows:

The version subcommand is used to print the drogon version currently installed on the system, as follows:

  1. $ dg_ctl version
  2. _
  3. __| |_ __ ___ __ _ ___ _ __
  4. / _` | '__/ _ \ / _` |/ _ \| '_ \
  5. | (_| | | | (_) | (_| | (_) | | | |
  6. \__,_|_| \___/ \__, |\___/|_| |_|
  7. |___/
  8. drogon ctl tools
  9. version:0.9.30.771
  10. git commit:d4710d3da7ca9e73b881cbae3149c3a570da8de4
  11. compile config:-O3 -DNDEBUG -Wall -std=c++17 -I/root/drogon/trantor -I/root/drogon/lib/inc -I/root/drogon/orm_lib/inc -I/usr/local/include -I/usr/include/uuid -I/usr/include -I/usr/include/mysql

The create subcommand is used to create various objects. It is currently the main function of drogon_ctl. Use the dg_ctl help create command to print detailed help for this command, as follows:

  1. $ dg_ctl help create
  2. Use create command to create some source files of drogon webapp
  3. Usage:drogon_ctl create <view|controller|filter|project|model> [-options] <object name>
  4. drogon_ctl create view <csp file name> [-o <output path>] [-n <namespace>]|[--path-to-namespace] //create HttpView source files from csp file
  5. drogon_ctl create controller -h <[namespace::]class_name> //create HttpController source files
  6. drogon_ctl create controller -w <[namespace::]class_name> //create WebSocketController source files
  7. drogon_ctl create filter <[namespace::]class_name> //create a filter named class_name
  8. drogon_ctl create project <project_name> //create a project named project_name
  9. drogon_ctl create model <model_path> //create model classes in model_path

View creation

The dg_ctl create view command is used to generate source files from csp files, see the View section. In general, this command does not need to be used directly. It is better practice to configure the cmake file to executed this command automatically. The command example is as follows, assuming the csp file is UsersList.csp.

  1. dg_ctl create view UsersList.csp

Controller creation

The dg_ctl create controller command is used to help the user create the controller’s source files. The three controllers currently supported by drogon can be created by this command.

  • The command to create an HttpSimpleController is as follows:
  • The command to create an HttpController is as follows:
  1. dg_ctl create controller -h ControllerTest
  2. dg_ctl create controller -h api::v1::ControllerTest
  • The command to create a WebSocketController is as follows:
  1. dg_ctl create controller -w WsControllerTest

Filter creation

The dg_ctl create filter command is used to help the user create the source files for filters, see the section.

  1. dg_ctl create filter LoginFilter
  2. dg_ctl create filter webapp::v1::LoginFilter

Create project

The best way the user creates a new Drogon application project is via the drogon_ctl command, as follows:

After the command is executed, a complete project directory will be created in the current directory. The directory name is ProjectName, and the user can directly compile the project in the build directory (cmake .. && make). Of course, it does not have any business logic.

The directory structure of the project is as follows:

  1. ├── build Build folder
  2. ├── cmake_modules Cmake scripts for third-party libraries lookup
  3. ├── FindJsoncpp.cmake
  4. ├── FindMySQL.cmake
  5. ├── FindSQLite3.cmake
  6. └── FindUUID.cmake
  7. ├── config.json The configuration file of the drogon application, please refer to the introduction section of the configuration file.
  8. ├── controllers The directory where the controller source files are stored
  9. ├── filters The directory where the filter files are stored
  10. ├── main.cc Main program
  11. ├── models The directory of the database model file, model source file creation see 11.2.5
  12. └── model.json
  13. ├── tests The direftory for unit/integration tests
  14. └── test_main.cc Entry point for tests
  15. └── views The directory where view csp files are stored, the source file does not need to be manually created by the user, and csp files are automatically preprocessed to obtain view source files when the project is compiled.

Create models

Use the dg_ctl create model command to create database model source files. The last parameter is the directory where models is stored. This directory must contain a model configuration file named model.json to tell dg_ctl how to connect to the database and which tables to be mapped.

  1. dg_ctl create model models

This command will prompt the user that the file will be overwritten directly. After the user enters y, it will generate all the model files.

Other source files need to reference model classes should include model header files, such as:

  1. #include "models/User.h"

Note that the models directory name is included to distinguish between multiple data sources in the same project. See ORM.

One can use the dg_ctl press command to do a stress testing, there are several options for this command.

  • -n num Set the number of requests(default : 1)
  • -t num Set the number of threads(default : 1), Set the number to the number of CPUs to achieve maximum performance
  • No progress indication(default: no)

For example, users can test an HTTP server as follows:

12