Example Usage 3: IHttp Dynamic Routing¶
This document describes how to use the dynamic routing feature of IHttp.
In the previous two documents, we integrated the nlohmann/json library and implemented static file services. This document will demonstrate how to use the dynamic routing feature of IHttp, capturing and validating path parameters to achieve flexible URL routing.
Configuring Dependency Libraries¶
This time, we are creating a CMake project, not a qmake project.
First, we create an IMakeCore-supported project named dynamicMapping_demo. Add IMakeCore support by running ipc init.
CMakeLists.txt¶
The content of this project's CMakeLists.txt file is as follows:
Compared to the original file, the following changes were made:
- Added support for the
_WIN32_WINNTmacro. Without this macro, the asio library will throw a warning when compiled with mingw. - Added support for the
ws2_32andmswsocklibraries. - These libraries are not required when using the MSVC compiler, but they must be included when using the mingw or llvm compilers.
- Added IMakeCore support, which is the last two lines of code.
Additionally, we added the main.cpp file and the IndexController class. The IndexController class is the class where we will handle the main logic today.
packages.json Configuration¶
After executing cmake on the project, a packages.json file is created. We now need to add some libraries to this file.
Modify the packages.json file as follows:
In this packages.json file, we added five libraries, which are:
- ICore
- This library is the foundation of the IWebCore framework. It supports reflection, configuration, runtime functionality, and more.
- nlohmann.json
- nlohmann.json is the JSON library chosen for the IWebCore project. It balances performance and usability compared to other libraries.
- asio
- A fundamental network library.
- ITcp
- A layer built on top of asio for managing TCP content.
- IHttp
- The HTTP service package.
We did not add the IHttp.assets library because this project no longer uses static file services.
Writing the main.cpp File¶
Create the main.cpp file:
In this file, we included the IApplication.h and IHttpServer.h header files and created the corresponding instances. The $EnableHttpPrintTrace(true) macro annotation is used to enable HTTP request printing, which facilitates debugging.
The $EnableHttpPrintTrace(true) macro annotation allows the program to print our routes. Without this macro annotation, the program will not print HTTP routes. In the following content, there will be server log output, which includes route-related information.
Writing the Dynamic Routing Controller¶
Creating IndexController¶
Now, let's create a controller that supports dynamic routing. First, create the IndexController.h file:
On line 5, we inherited from the IHttpControllerInterface class using CRTP.
On line 7, the Q_GADGET macro is introduced, which is the foundation for implementing reflection.
On line 8, $AsController(/) must be defined. This indicates that the prefix path for the routes defined in this class is /.
Lines 13-14 define an index function, mapping it to the root route / using the GET method.
Lines 16-17 define a hello function, mapping it to the route hello. If the route is omitted, the function name becomes the route. Here, we also define a name parameter, which the HTTP request will look for in the parameters, headers, cookies, or session. If the parameter is not found, the program returns a 404 status.
Lines 19-20 define a POST request to the /welcome path. The parameter name must be queried via a path parameter.
Implementing IndexController¶
Next, create the IndexController.cpp file:
Running the Project¶
Running Output¶
Now, we can compile and run the project. The output is as follows:
In this log output, there is information about the routes:
We can refer to this content along with the code above to see the defined request methods, routes, corresponding class member functions, and mapped function content.
Request Testing¶
Next, we use curl to write a series of requests.
- Normal request to
/
- A POST request to
/returns a 404 status.
- Requesting the
hellocontent and adding a path parameter.
- Requesting the
hellocontent via header.
- Requesting
/welcome.
- Invalid request.
Download¶
Users can also directly download the project code and run it:
Note: The project includes the complete dynamic routing example code and can be compiled and run directly.
The example has now been concluded. If users wish to learn more in depth, please refer to the IHttp-related documentation.