[4] Nginx Server Setup - Writing a Drogon Test API
- 공유 링크 만들기
- X
- 이메일
- 기타 앱
Let's create an API using Drogon to return specific values as a test.
1. Setting up the Drogon Test Project
- Run the following command in the directory where `drogon_ctl` is installed: ``` drogon_ctl create project testAPI cd testAPI ```
2. Build
- The full path will be /root/drogon2/drogon/build/drogon_ctl/testAPI/build
``` mkdir build cd build cmake .. make ```
If the following error occurs, it’s best to set the environment variable and reinstall Drogon:
``` CMake Error at /usr/share/cmake/Modules/CMakeFindDependencyMacro.cmake:76 (find_package): By not providing "FindJsoncpp.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "Jsoncpp", but CMake did not find one.
Could not find a package configuration file provided by "Jsoncpp" with any of the following names:
JsoncppConfig.cmake jsoncpp-config.cmake
Add the installation prefix of "Jsoncpp" to CMAKE_PREFIX_PATH or set "Jsoncpp_DIR" to a directory containing one of the above files. If "Jsoncpp" provides a separate development package or SDK, be sure it has been installed. Call Stack (most recent call first): /root/drogon/build/CMakeFiles/DrogonConfig.cmake:39 (find_dependency) CMakeLists.txt:31 (find_package) ```
This issue arises from a mismatch between the paths where Drogon and Jsoncpp are installed. To resolve this:
``` Make sure to specify the path where `jsoncpp` is located
export Jsoncpp_DIR=/usr/local/lib/cmake/jsoncpp Check the correct path using the following commands:
find / -name "jsoncpp-config.cmake" 2>/dev/null find / -name "JsoncppConfig.cmake" 2>/dev/null
Once identified, set the path and reinstall following the instructions at Framework Build Reference under Section 2. ```
3. Creating the Controller
``` drogon_ctl create controller testAPIController
A controller will be created in the following path: [root@vbox controllers] (master) $ pwd /root/drogon2/drogon/build/drogon_ctl/testAPI/controllers ```
3-1. Source Code
``` testAPIController.cc
#include "testAPIController.h"
void testAPIController::handleRequest(const HttpRequestPtr& req, std::function
testAPIController.h
#pragma once #include
using namespace drogon;
class testAPIController : public HttpController
void handleRequest(const HttpRequestPtr& req, std::function
3-2. Configuration Changes
Edit `/root/drogon2/drogon/build/drogon_ctl/testAPI/config.json`. Use the following structure, modifying only the port to match the NGINX proxy port.
``` "listeners": [ { // address: Ip address, 0.0.0.0 by default "address": "0.0.0.0", // port: Port number "port": 8848, // https: If true, use https for security, false by default "https": false } ], ```
3-3. Modify main.cc
Modify to load the configuration file by editing /root/drogon2/drogon/build/drogon_ctl/testAPI/main.cc
``` #include
// drogon::app().loadConfigFile("../config.yaml"); // Run HTTP framework, the method will block in the internal event loop drogon::app().run(); return 0; } ```
3-4. Rebuild
``` cd /root/drogon2/drogon/build/drogon_ctl/testAPI/build make
./testAPI # Run the application ```
4. Testing
Verify access via the web.
Related Links
Recommended Link
- 공유 링크 만들기
- X
- 이메일
- 기타 앱
댓글
댓글 쓰기