linux c 高性能架构,Drogon:基于C++11的Linux高性能HTTP web应用框架

457af01faf948dc38fe150374de54cdb.png

68747470733a2f2f7472617669732d63692e636f6d2f616e2d74616f2f64726f676f6e2e7376673f6272616e63683d6d617374657268747470733a2f2f63692e6170707665796f722e636f6d2f6170692f70726f6a656374732f7374617475732f313266667566366a3576616e6b6779622f6272616e63682f6d61737465723f7376673d7472756568747470733a2f2f696d672e736869656c64732e696f2f6c67746d2f616c657274732f672f616e2d74616f2f64726f676f6e2e7376673f6c6f676f3d6c67746d266c6f676f57696474683d313868747470733a2f2f696d672e736869656c64732e696f2f6c67746d2f67726164652f6370702f672f616e2d74616f2f64726f676f6e2e7376673f6c6f676f3d6c67746d266c6f676f57696474683d313868747470733a2f2f6261646765732e6769747465722e696d2f64726f676f6e2d7765622f636f6d6d756e6974792e73766768747470733a2f2f696d672e736869656c64732e696f2f62616467652f446f636b65722d696d6167652d626c75652e737667

Overview

Drogon is a C++14/17-based HTTP application framework. Drogon can be used to easily build various types of web application server programs using C++. Drogon is the name of a dragon in the American TV series "Game of Thrones" that I really like.

Drogon is a cross-platform framework, It supports Linux, macOS, FreeBSD, and Windows. Its main features are as follows:

Use a non-blocking I/O network lib based on epoll (kqueue under macOS/FreeBSD) to provide high-concurrency, high-performance network IO, please visit the benchmarks page and TFB Live Results for more details;

Provide a completely asynchronous programming mode;

Support Http1.0/1.1 (server side and client side);

Based on template, a simple reflection mechanism is implemented to completely decouple the main program framework, controllers and views.

Support cookies and built-in sessions;

Support back-end rendering, the controller generates the data to the view to generate the Html page. Views are described by CSP template files, C++ codes are embedded into Html pages through CSP tags. And the drogon command-line tool automatically generates the C++ code files for compilation;

Support view page dynamic loading (dynamic compilation and loading at runtime);

Provide a convenient and flexible routing solution from the path to the controller handler;

Support filter chains to facilitate the execution of unified logic (such as login verification, Http Method constraint verification, etc.) before handling HTTP requests;

Support https (based on OpenSSL);

Support WebSocket (server side and client side);

Support JSON format request and response, very friendly to the Restful API application development;

Support file download and upload;

Support gzip, brotli compression transmission;

Support pipelining;

Provide a lightweight command line tool, drogon_ctl, to simplify the creation of various classes in Drogon and the generation of view code;

Support non-blocking I/O based asynchronously reading and writing database (PostgreSQL and MySQL(MariaDB) database);

Support asynchronously reading and writing sqlite3 database based on thread pool;

Support ARM Architecture;

Provide a convenient lightweight ORM implementation that supports for regular object-to-database bidirectional mapping;

Support plugins which can be installed by the configuration file at load time;

Support AOP with build-in joinpoints.

A very simple example

Unlike most C++ frameworks, the main program of the drogon application can be kept clean and simple. Drogon uses a few tricks to decouple controllers from the main program. The routing settings of controllers can be done through macros or configuration file.

Below is the main program of a typical drogon application:

#include

using namespace drogon;

int main()

{

app().setLogPath("./")

.setLogLevel(trantor::Logger::kWarn)

.addListener("0.0.0.0", 80)

.setThreadNum(16)

.enableRunAsDaemon()

.run();

}

It can be further simplified by using configuration file as follows:

#include

using namespace drogon;

int main()

{

app().loadConfigFile("./config.json").run();

}

Drogon provides some interfaces for adding controller logic directly in the main() function, for example, user can register a handler like this in Drogon:

app.registerHandler("/test?username={name}",

[](const HttpRequestPtr& req,

std::function &&callback,

const std::string &name)

{

Json::Value json;

json["result"]="ok";

json["message"]=std::string("hello,")+name;

auto resp=HttpResponse::newHttpJsonResponse(json);

callback(resp);

},

{Get,"LoginFilter"});

While such interfaces look intuitive, they are not suitable for complex business logic scenarios. Assuming there are tens or even hundreds of handlers that need to be registered in the framework, isn't it a better practice to implement them separately in their respective classes? So unless your logic is very simple, we don't recommend using above interfaces. Instead, we can create an HttpSimpleController as follows:

/// The TestCtrl.h file

#pragma once

#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

};

/// The TestCtrl.cc file

#include "TestCtrl.h"

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);

}

Most of the above programs can be automatically generated by the command line tool drogon_ctl provided by drogon (The cammand is drogon_ctl create controller TestCtrl). All the user needs to do is add their own business logic. In the example, the controller returns a Hello, world! string when the client accesses the http://ip/test URL.

For JSON format response, we create the controller as follows:

/// The header file

#pragma once

#include

using namespace drogon;

class JsonCtrl : public drogon::HttpSimpleController

{

public:

virtual void asyncHandleHttpRequest(const HttpRequestPtr &req, std::function &&callback) override;

PATH_LIST_BEGIN

//list path definitions here;

PATH_ADD("/json", Get);

PATH_LIST_END

};

/// The source file

#include "JsonCtrl.h"

void JsonCtrl::asyncHandleHttpRequest(const HttpRequestPtr &req,

std::function &&callback)

{

Json::Value ret;

ret["message"] = "Hello, World!";

auto resp = HttpResponse::newHttpJsonResponse(ret);

callback(resp);

}

Let's go a step further and create a demo RESTful API with the HttpController class, as shown below (Omit the source file):

/// The header file

#pragma once

#include

using namespace drogon;

namespace api

{

namespace v1

{

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

//your declaration of processing function maybe like this:

void getInfo(const HttpRequestPtr &req, std::function &&callback, int userId) const;

void getDetailInfo(const HttpRequestPtr &req, std::function &&callback, int userId) const;

void newUser(const HttpRequestPtr &req, std::function &&callback, std::string &&userName);

public:

User()

{

LOG_DEBUG << "User constructor!";

}

};

} // namespace v1

} // namespace api

As you can see, users can use the HttpController to map paths and parameters at the same time. This is a very convenient way to create a RESTful API application.

In addition, you can also find that all handler interfaces are in asynchronous mode, where the response is returned by a callback object. This design is for performance reasons because in asynchronous mode the drogon application can handle a large number of concurrent requests with a small number of threads.

After compiling all of the above source files, we get a very simple web application. This is a good start. For more information, please visit the wiki or DocsForge

Contributions

Every contribution is welcome. Please refer to the contribution guidelines for more information.

Webadmin!是一个免费的开源框架,用于为Linux系统的快速搭建统一、稳定、易用的Web管理系统。 WebAdmin系统由三部分组成:WEB图形用户接口、WebAdmin守护进程和进程监视程序。Web图形用户接口(WebGUI)是WebAdmin系统的前端部分,为用户提供一个统一、易操作的图形界面。WebAdmin守护进程 (WebAdmind)是WebAdmin系统的后台部分,实时监视WebGUI生成的配置文件,并根据配置文件的变化情况,启动或停止相应的服务进程,WebAdmin进程监视程序(DaemonWatcher)用于实时监视WebAdmind启动的服务进程的运行状况,一旦发现启动的服务进程异常中止,立即重启中止的服务进程,从而确保系统可靠稳定运行。 WebAdmin!提供了一个结构化的WebAdmin开发框架,它的前后台部分均采用插件式的程序开发方法,借助提供的插件开发模板,WebAdmin系统开发者不必关WebAdmin开发框架的具体实现,就可设计出界面统一、操作简单、安全稳定的WebGUI界面。与WebGUI相对应,Webadmind也是采用插件式的程序开发方法。WebAdmind插件与WebGUI插件一一对应完成对界面操作的响应。DaemonWatcher是一个独立的进程监视程序,是为确保WebAdmind启动的进程能够不可间断地提供服务,一旦发现被监视程序发生异常中止,DaemonWatcher将根据进程的启动脚本立即启动被中止进程。 WebAdmin是一个用C语言设计的易用的图形用户接口开发框架,C语言的高可移植性使得WebAdmin可以广泛应用于包括Linux、Unix、Windows及各种嵌入式操作系统中,编译WebAdmin系统除Libxml2库处不需要额外的C函数库支持。WebAdmin提供了丰富的API函数,开发者可以根据自己的需要定制个性化的WebAdmin系统。 WebAdmin系统的界面风格也可以自己定制,对于OEM厂商可以根据需要修改界面风格,满足定制要求。 WebAdmin的开放设计思想,为WebAdmin系统的不断发展普奠定了基础,无数开发者提供了开源插件模块,用户甚至不用写一行代码就可根据自己的需要设计WebAdmin系统。 【简单使用方法】:下载后将压缩文件上传到Linux系统中,用tar xvfz webadmin-devel-1.3.tar.gz解压,解压后进入webadmin-devel目录,执行./configure,make命令后将会在test/webui目录下生成一个webadmin.cgi文件,将此文件拷贝到apache下的WEB根目录下cgi-bin目录下即可,为测试webadmin.cgi,还需将htdocs目录下的文件拷贝到apache的WEB根目录下,将etc目录中的所有文件拷贝到根目录下的etc中,最后用浏览器访问你的apache Web服务器即可看到Linux系统的WEB管理界面。 【说明】:编译此源码需要libxml2库的支持 有技术问题可以访问官方网站:http://www.webadminc.com,联系电话:13311223928
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值