workflow学习笔记(二):wfrest

1.项目简介

wfrest 是一款基于 C++ Workflow 打造的异步轻量级 C++ Web 框架,主打 REST API 开发,核心特点是高性能、高效、易用,依托搜狗开源的 C++ Workflow(并行计算与异步网络引擎)实现异步网络处理,兼顾性能与开发效率。

1.1 核心定位

  • 面向 RESTful API 开发的微 Web 框架,专为 C++ 开发者设计;
  • 基于异步引擎(C++ Workflow),避免同步阻塞带来的性能损耗;
  • 轻量化设计,无冗余依赖,聚焦 Web 开发核心场景。

1.2核心特性

(1) 基础 Web 能力全覆盖

  • 支持 HTTP 方法(GET/POST/PUT/DELETE 等)、路径参数 / 查询参数解析、POST 表单处理;
  • 支持 Header/Cookie 操作、文件上传 / 下载 / 保存、JSON 序列化 / 反序列化(内置 Json API);
  • 支持静态文件服务、重定向、自定义服务器配置(最大连接数、超时时间等)。

(2) 扩展能力丰富

  • 蓝图(Blueprint):支持路由模块化管理,便于大型项目拆分;
  • 数据库集成:原生支持 MySQL、Redis 异步操作,无需额外封装;
  • 高级特性:HTTPS、代理、定时器、Server Push(SSE / 服务器推送)、AOP(面向切面编程);
  • 性能优化:内置文件缓存(FileCache),可缓存静态文件提升访问效率,支持缓存大小配置、缓存清理 / 监控;
  • 异步任务串联:支持 Series 接口,可在请求处理中添加异步任务(如定时器),贴合 Workflow 异步模型。

1.3 易用性设计

  • 极简的路由注册方式,支持 Lambda 表达式,代码简洁;
  • 丰富的示例代码(覆盖所有核心特性),降低上手成本;
  • 支持中文 / 英文文档,便于不同地区开发者使用。

2. 编译使用

  • (1)安装依赖
1
>> apt-get install build-essential cmake zlib1g-dev libssl-dev libgtest-dev -y
  • (2) 下载源码编译安装
1
2
3
4
# 1.下载源码
>> git clone --recursive https://github.com/wfrest/wfrest
# 2.编译安装
>> cd wfrest && make -j 16 && make install
  • (3) 简单使用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* @Author: chenjingyu
* @Date: 2026-02-02 17:26:55
* @Contact: 2458006466@qq.com
* @Description: main
*/
#include <csignal>
#include <workflow/WFFacilities.h>

#include <wfrest/HttpServer.h>

using namespace wfrest;

static WFFacilities::WaitGroup wait_group(1);

void sig_handler(int signo) { wait_group.done(); }

int main() {
signal(SIGINT, sig_handler);

HttpServer svr;

// curl -v http://ip:port/hello
svr.GET("/hello",
[](const HttpReq *req, HttpResp *resp) { resp->String("world\n"); });
// curl -v http://ip:port/data
svr.GET("/data", [](const HttpReq *req, HttpResp *resp) {
std::string str = "Hello world";
resp->String(std::move(str));
});

svr.ROUTE("/multi",
[](const HttpReq *req, HttpResp *resp) {
std::string method(req->get_method());
resp->String(std::move(method));
},
{"GET", "POST"});

// curl -v http://ip:port/post -d 'post hello world'
svr.POST("/post", [](const HttpReq *req, HttpResp *resp) {
// reference, no copy here
std::string &body = req->body();
fprintf(stderr, "post data : %s\n", body.c_str());
resp->String(body);
});

// curl -v http://ip:port/any_path
svr.set_default_route("/data");

if (svr.track().start(8888) == 0) {
svr.list_routes();
wait_group.wait();
svr.stop();
} else {
fprintf(stderr, "Cannot start server");
exit(1);
}
return 0;
}

 编译使用:

1
2
3
4
5
6
7
>> g++ main.cc -o main -lworkflow -lwfrest -lpthread -lssl -lcrypto
>> ./main
[WFREST] POST /post
[WFREST] GET /multi
[WFREST] POST /multi
[WFREST] GET /hello
[WFREST] GET /data

 测试:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
>> curl http://127.0.0.1:8888/hello
world
>> curl http://127.0.0.1:8888/data
Hello world#
>> curl http://127.0.0.1:8888/multi
GET#
>> curl -X POST http://127.0.0.1:8888/multi
POST#
>> curl -X POST http://127.0.0.1:8888/post -d "Post Hello World"
Post Hello World#
# 默认路径为/data
>> curl http://127.0.0.1:8888 Hello world#
>> curl http://127.0.0.1:8888/111 Hello world#
>> curl http://127.0.0.1:8888/222
Hello world#

参考资料