Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the C++ web service framework

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use the C++ network service framework". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought. Let's study and learn how to use the C++ network service framework.

C++ is one of the most important programming languages. It can not only carry out procedural programming, but also carry out object-based programming using abstract data types, and can also carry out object-oriented programming characterized by inheritance and polymorphism.

However, due to various problems in the standard library and community ecology, the development of C++ in the Web server is not satisfactory. At the same time, compared with other languages, the server framework of C++ is often more primitive and more tedious to support a variety of advanced functions. C++ 14x17 provides more excellent features for the language, making it possible to implement a more modern Web framework.

Brief introduction

Drogon is an an-tao open source Http application framework based on C++ 14amp 17 on Github. The current version is v1.4.1.

Drogon is a cross-platform framework that makes it easy to use C++ to build various types of Web application servers. Its network layer is based on epoll, uses full asynchronous programming mode, implements a simple reflection mechanism, supports back-end rendering, supports dynamic loading of runtime view pages, supports filter chains, supports websocket, and supports pipelining

Use

Drogon relies on trantor, a non-blocking IO library, as well as jsoncpp, libuuid, and zlib. Drogon is compiled and installed using CMake + Make:

Cd $WORK_PATH git clone https://github.com/an-tao/drogon cd drogon git submodule update-init mkdir build cd build cmake.. Make & & sudo make install

You can also use the Drogon source code directly in the project and add it to the project's cmake file:

Add_subdirectory (third_party/drogon) arget_link_libraries (${PROJECT_NAME} PRIVATE drogon)

After the installation is complete, you can start writing Web service applications in Drogon. Let's look at a simple main program:

# include using namespace drogon; int main () {app () .setLogPath (". /") .setLogLevel (trantor::Logger::kWarn) .addListener ("0.0.0.0", 80) .setThreadNum (16) .enableRunAsDaemon () .run ();}

Generate an application instance through a chained configuration, set the log path and level, listen for 0.0.0.0V80, use 16 threads, and run in the background. Compile and run, we can get a HTTP service.

Using the idea of configuration, Drogon can load the configuration file directly to complete the configuration of the server:

App (). LoadConfigFile (". / config.json"). Run ()

With the HTTP service, we have to write handlers for logical processing. Let's write a simple controller:

# include using namespace drogon; class TestCtrl: public drogon::HttpSimpleController {public: virtual void asyncHandleHttpRequest (const HttpRequestPtr& req, std::function & & callback) override; PATH_LIST_BEGIN PATH_ADD ("/ test", Get); PATH_LIST_END}

This controller inherits HttpSimpleController, provides asyncHandleHttpRequest to implement the processing logic, and registers on the path / test. Then, we implement this handler:

Void TestCtrl::asyncHandleHttpRequest (const HttpRequestPtr& req, std::function & & callback) {/ / write your application logic here auto resp = HttpResponse::newHttpResponse (); resp- > setBody ("

Hello, world!

"); resp- > setExpiredTime (0); callback (resp);}

This handler creates a new response, sets the HTML of the content to Hello World, and sets the expiration time. Finally, a callback is used to return the HTTP response. In this way, we get a simple Hello World service.

We can also simply implement an example of RESTful API:

Class User: public drogon::HttpController {public: METHOD_LIST_BEGIN / / use METHOD_ADD to add your custom processing function here; METHOD_ADD (User::getInfo, "/ {id}", Get); / / path is / api/v1/User/ {arg1} METHOD_ADD (User::getDetailInfo, "/ {id} / detailinfo", Get) / / path is / api/v1/User/ {arg1} / detailinfo METHOD_ADD (User::newUser, "/ {name}", Post); / / path is / api/v1/User/ {arg1} METHOD_LIST_END.} Summary

Drogon is powerful, encapsulating and abstracting a large number of underlying implementations, so that developers can easily call various functions and focus on the implementation of business logic.

The design of Drogon aligns the modern Web framework of other high-level languages, fully applies the ideas of configuration, dynamic, automation and decoupling, and provides lightweight ORM, template engine and command line tools, which can be said to be a very comprehensive Web framework.

Thank you for your reading. the above is the content of "how to use the C++ Network Service Framework". After the study of this article, I believe you have a deeper understanding of how to use the C++ network service framework. The specific use also needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report