![](https://github.com/an-tao/drogon/wiki/images/drogon-white.jpg)
[![Build Status](https://travis-ci.com/an-tao/drogon.svg?branch=master)](https://travis-ci.com/an-tao/drogon)
[![Build status](https://ci.appveyor.com/api/projects/status/12ffuf6j5vankgyb/branch/master?svg=true)](https://ci.appveyor.com/project/an-tao/drogon/branch/master)
[![Total alerts](https://img.shields.io/lgtm/alerts/g/an-tao/drogon.svg?logo=lgtm&logoWidth=18)](https://lgtm.com/projects/g/an-tao/drogon/alerts/)
[![Join the chat at https://gitter.im/drogon-web/community](https://badges.gitter.im/drogon-web/community.svg)](https://gitter.im/drogon-web/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Docker image](https://img.shields.io/badge/Docker-image-blue.svg)](https://cloud.docker.com/u/drogonframework/repository/docker/drogonframework/drogon)
English | [简体中文](./README.zh-CN.md) | [繁體中文](./README.zh-TW.md)
### 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, OpenBSD, 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 [TFB Tests Results](https://www.techempower.com/benchmarks/#section=data-r19&hw=ph&test=composite) 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:
```c++
#include <drogon/drogon.h>
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:
```c++
#include <drogon/drogon.h>
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:
```c++
app().registerHandler("/test?username={name}",
[](const HttpRequestPtr& req,
std::function<void (const HttpResponsePtr &)> &&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:
```c++
/// The TestCtrl.h file
#pragma once
#include <drogon/HttpSimpleController.h>
using namespace drogon;
class TestCtrl:public drogon::HttpSimpleController<TestCtrl>
{
public:
virtual void asyncHandleHttpRequest(const HttpRequestPtr& req, std::function<void (const HttpResponsePtr &)> &&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<void (const HttpResponsePtr &)> &&callback)
{
//write your application logic here
auto resp = HttpResponse::newHttpResponse();
resp->setBody("<p>Hello, world!</p>");
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 command 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:
```c++
/// The header file
#pragma once
#include <drogon/HttpSimpleController.h>
using namespace drogon;
class JsonCtrl : public drogon::HttpSimpleController<JsonCtrl>
{
public:
virtual void asyncHandleHttpRequest(const HttpRequestPtr &req, std::function<void(const HttpResponsePtr &)> &&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<void(const HttpResponsePtr &)> &&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):
```c++
/// The header file
#pragma once
#include <drogon/HttpController.h>
using namespace drogon;
namespace api
{
namespace v1
{
class User : public drogon::HttpController<User>
{
public:
METHOD_LIST_BEGIN
//use METHOD_ADD to add your custom processing function here;
METHOD_ADD(User::getInfo, "
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
软件开发设计:应用软件开发、系统软件开发、移动应用开发、网站开发C++、Java、python、web、C#等语言的项目开发与学习资料 硬件与设备:单片机、EDA、proteus、RTOS、包括计算机硬件、服务器、网络设备、存储设备、移动设备等 操作系统:LInux、树莓派、安卓开发、微机操作系统、网络操作系统、分布式操作系统等。此外,还有嵌入式操作系统、智能操作系统等。 网络与通信:数据传输、信号处理、网络协议、网络与通信硬件、网络安全网络与通信是一个非常广泛的领域,它涉及到计算机科学、电子工程、数学等多个学科的知识。 云计算与大数据:包括云计算平台、大数据分析、人工智能、机器学习等,云计算是一种基于互联网的计算方式,通过这种方式,共享的软硬件资源和信息可以按需提供给计算机和其他设备
资源推荐
资源详情
资源评论
收起资源包目录
一个基于高性能web c++框架drogon的web,网页,api实例demo的测试项目.zip (485个子文件)
Wepoll.c 76KB
test.c 6KB
mman.c 4KB
News.cc 122KB
db_test.cc 71KB
Groups.cc 55KB
Users.cc 48KB
Users.cc 48KB
main.cc 48KB
Users.cc 45KB
create_model.cc 44KB
TcpConnectionImpl.cc 44KB
LoggerTest.cc 40KB
HttpAppFrameworkImpl.cc 31KB
Utilities.cc 30KB
HttpControllersRouter.cc 27KB
HttpRequestImpl.cc 21KB
HttpClientImpl.cc 20KB
HttpResponseImpl.cc 20KB
HttpServer.cc 20KB
create_view.cc 19KB
ConfigLoader.cc 18KB
MysqlConnection.cc 17KB
HttpRequestParser.cc 17KB
HttpUtils.cc 16KB
create_controller.cc 15KB
PgBatchConnection.cc 15KB
WebSocketClientImpl.cc 14KB
StaticFileRouter.cc 13KB
DbClientLockFree.cc 13KB
DbClientImpl.cc 13KB
api_v1_ApiTest.cc 13KB
HttpSimpleControllersRouter.cc 13KB
main.cc 12KB
WebsocketControllersRouter.cc 12KB
WebSocketConnectionImpl.cc 11KB
PgConnection.cc 11KB
HttpResponseParser.cc 10KB
Date.cc 10KB
Sqlite3Connection.cc 10KB
press.cc 10KB
GzipTest.cc 9KB
Md5.cc 9KB
TransactionImpl.cc 9KB
SharedLibManager.cc 9KB
EventLoop.cc 9KB
ListenerManager.cc 8KB
TimerQueue.cc 8KB
SqlBinder.cc 8KB
ArrayParser.cc 7KB
AresResolver.cc 7KB
DigestAuthFilter.cc 7KB
DbClientManager.cc 7KB
InetAddress.cc 6KB
AsyncFileLogger.cc 6KB
LogStream.cc 6KB
TcpServer.cc 6KB
EpollPoller.cc 6KB
Connector.cc 6KB
test1.cc 6KB
KQueue.cc 6KB
Socket.cc 6KB
TcpClient.cc 5KB
MsgBuffer.cc 5KB
splitStringUnittest.cc 5KB
test1.cc 5KB
test1.cc 5KB
Logger.cc 4KB
MultiPart.cc 4KB
Criteria.cc 4KB
HttpFileImpl.cc 4KB
Result.cc 4KB
Sha1.cc 4KB
create_filter.cc 4KB
create_plugin.cc 4KB
TimingWheel.cc 4KB
test2.cc 3KB
Field.cc 3KB
NormalResolver.cc 3KB
SimpleReverseProxy.cc 3KB
HttpPipeliningTest.cc 3KB
create_project.cc 3KB
PluginsManager.cc 3KB
Api_IndexController.cc 3KB
Row.cc 3KB
CacheMapTest.cc 3KB
WebSocketTest.cc 3KB
FiltersFunction.cc 3KB
AOPAdvice.cc 3KB
DrClassMap.cc 3KB
SessionManager.cc 3KB
SendfileTest.cc 2KB
SecureSSLRedirector.cc 2KB
ConcurrentTaskQueue.cc 2KB
Exception.cc 2KB
PostgreSQLResultImpl.cc 2KB
DnsTest.cc 2KB
Channel.cc 2KB
TestCtrl.cc 2KB
RestfulController.cc 2KB
共 485 条
- 1
- 2
- 3
- 4
- 5
资源评论
妄北y
- 粉丝: 2w+
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Git操作图解(带VS2022集成Git工具)
- Android Studio Ladybug(android-studio-2024.2.1.12-mac-arm.zip.001)
- IKBC机械键盘固件.tar
- python源码教程,超级详细,附开发教程手册,python前端开发,入门级教程,第二章
- TCR+FC型svc无功补偿simulink仿真模型,一共两个仿真,如下图所示,两个其实大致内容差不多,只是封装不同,有详细资料,资料中有相关lunwen,有背景原理和分析,有使用说明,有建模仿真总结
- 蜘蛛分拣机器人工作站工程图机械结构设计图纸和其它技术资料和技术方案非常好100%好用.zip
- 字符串批处理工具(源程序+代码)
- PSAT( Power System Analysis Toolbox)最新说明书.zip
- HTML+JS获取地理位置(经纬度)
- 基于simulink的车辆坡度与质量识别模型,扩展卡尔曼滤波,估计曲线与实际误差合理
- HTML+JS教程-实现图片页面内拖拽、拖放
- python源码教程,超级详细,附开发教程手册,python前端开发,入门学习第三章
- 神经网络基本概念及其在人工智能领域的应用概述
- EEMD算法应用于信号去噪.zip
- 使用comsol仿真软件 利用双温方程模拟飞秒激光二维移动烧蚀材料 可看观察温度与应力分布 周期为10us,变形几何部分本人还在完善学习中 三维的也有 还有翻阅的lunwen文献一起打包
- Android Studio Ladybug(android-studio-2024.2.1.12-windows-exe.zip.002)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功