提到 C++ 写 Web 服务,你脑海中浮现的可能是满屏的模板报错、手动解析 HTTP 报文、以及回调嵌套到看不清缩进的代码。但在 2026 年,C++20 协程 + PMR 内存池 + C++26 反射的组合,已经让 C++ Web 开发体验发生了质变。本文用 Hical 框架带你体验:10 行代码启动 HTTP 服务器,40 行代码搞定完整 REST API。
10 行代码,启动 HTTP 服务器
cpp
#include "core/HttpServer.h"
using namespace hical;
int main()
{
HttpServer server(8080);
server.router().get("/", [](const HttpRequest&) -> HttpResponse {
return HttpResponse::ok("Hello, hical!");
});
server.start();
}
bash
curl http://localhost:8080/
# Hello, hical!
没有工厂类,没有 Builder 链,没有 XML 配置。创建服务器、注册路由、启动 ------ 三步完事。
40 行代码,完整 REST API
实际项目当然不止一个路由。下面是一个包含 JSON 响应、路径参数、请求体读取、日志中间件的完整示例:
cpp
#include "core/HttpServer.h"
using namespace hical;
int main()
{
HttpServer server(8080);
// 日志中间件 ------ 洋葱模型,请求前后各打一行日志
server.use(
[](HttpRequest& req, MiddlewareNext next) -> Awaitable<HttpResponse>
{
std::cout << httpMethodToString(req.method()) << " "
<< req.path() << std::endl;
auto res = co_await next(req);
std::cout << " -> " << static_cast<int>(res.statusCode())
<< std::endl;
co_return res;
});
// GET /api/status ------ JSON 响应
server.router().get("/api/status",
[](const HttpRequest&) -> HttpResponse {
return HttpResponse::json(
{{"status", "running"}, {"version", "2.0.0"},
{"framework", "hical"}});
});
// POST /api/echo ------ 读取请求体并回写
server.router().post("/api/echo",
[](const HttpRequest& req) -> HttpResponse {
return HttpResponse::ok(req.body());
});
// GET /users/{id} ------ 路径参数自动提取
server.router().get("/users/{id}",
[](const HttpRequest& req) -> HttpResponse {
return HttpResponse::json(
{{"userId", req.param("id")},
{"name", "User " + req.param("id")}});
});
server.start();
}
测试一下:
bash
# JSON 响应
curl http://localhost:8080/api/status
# {"status":"running","version":"2.0.0","framework":"hical"}
# 路径参数
curl http://localhost:8080/users/42
# {"userId":"42","name":"User 42"}
# POST 回写
curl -X POST -d "Hello" http://localhost:8080/api/echo
# Hello
这 40 行代码覆盖了 REST API 最常见的场景:JSON 返回、路径参数、请求体处理、请求日志。如果你用过 Express.js 或 Flask,会发现 API 风格非常相似 ------ 只是语言换成了 C++。
为什么选 Hical?
协程异步,告别回调地狱
注意上面中间件里的 co_await next(req)。这不是回调,不是 Promise,而是 C++20 原生协程。异步代码写起来跟同步一样顺畅:
cpp
server.router().get("/async", [](const HttpRequest&) -> Awaitable<HttpResponse> {
co_await hical::sleep(0.1); // 异步等待,不阻塞线程
co_return HttpResponse::ok("done");
});
PMR 内存池,性能开箱即用
Hical 内置三层 PMR(Polymorphic Memory Resource)内存池:全局同步池、线程本地无锁池、请求级单调缓冲区。HTTP 请求处理中的缓冲区、JSON 对象、响应体全部走 PMR 分配,请求结束时整体释放 ------ 零碎片、零锁争用。你不需要做任何配置,默认就启用了。
C++26 反射就绪,一行代码搞定序列化
定义一个 DTO 结构体,加一行 HICAL_JSON 宏,就能自动序列化/反序列化 JSON:
cpp
struct UserDTO
{
std::string name;
int age;
std::string email;
HICAL_JSON(UserDTO, name, age, email) // 就这一行
};
// 序列化
UserDTO user{"Alice", 30, "alice@example.com"};
auto json = meta::toJson(user); // -> {"name":"Alice","age":30,"email":"alice@example.com"}
// 反序列化(从 HTTP 请求体)
auto user = req.readJson<UserDTO>();
当编译器支持 C++26 反射时,连这行宏都不需要 ------ Hical 会自动切换到原生反射路径。
安装
vcpkg(推荐):
bash
vcpkg install hical61-hical
cmake
find_package(hical CONFIG REQUIRED)
target_link_libraries(my_app PRIVATE hical::hical_core)
源码构建:
bash
git clone https://github.com/Hical61/Hical.git
cd Hical
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
CMake FetchContent:
cmake
include(FetchContent)
FetchContent_Declare(hical
GIT_REPOSITORY https://github.com/Hical61/Hical.git
GIT_TAG v2.0.0)
FetchContent_MakeAvailable(hical)
target_link_libraries(my_app PRIVATE hical_core)
想深入了解?
本文只展示了 Hical 最基础的用法。框架还支持 WebSocket 双向通信、SSL/TLS 加密、Cookie/Session 管理、静态文件服务、Multipart 文件上传等完整功能。
深度教学系列(从架构设计到性能调优):
GitHub: https://github.com/Hical61/Hical
如果觉得 Hical 有意思,欢迎给个 Star,这是对开源项目最好的支持。