Beauty项目使用教程

Beauty项目使用教程

beauty A Simple C++ Http server/client above Boost.Beast beauty 项目地址: https://gitcode.com/gh_mirrors/beauty7/beauty

1. 项目介绍

Beauty是一个基于Boost.Beast的简单Http服务器和客户端库。它提供了同步和异步的服务器及客户端创建功能,并包含了一些基于Boost.Asio的信号和定时器管理特性。Beauty支持Http或Https的服务器或客户端,以及WebSocket(目前仅支持服务器端和客户端,且尚属实验性质)。

2. 项目快速启动

首先,确保你已经安装了Boost和OpenSSL库。以下是基于Conan的Linux系统上的构建步骤:

git clone https://github.com/dfleury2/beauty.git
cd beauty/
conan install . -pr:h default -pr:b default -b missing -of build
cmake -S . -B build --preset conan-release
cmake --build build --preset conan-release

如果你不使用Conan,可以尝试以下步骤:

git clone https://github.com/dfleury2/beauty.git
cd beauty/
cmake .

快速启动一个Http服务器

以下是一个简单的Http服务器示例代码:

#include <beauty/beauty.hpp>

int main() {
    // 创建服务器实例
    beauty::server server;

    // 添加默认的'/'路由
    server.add_route("/").get([](const auto& req, auto& res) {
        res.body() = "It's work ;) ... it works! :)";
    });

    // 添加'/person/:id'路由
    server.add_route("/person/:id").get([](const auto& req, auto& res) {
        auto id = req["id"].as_string();
        res.body() = "You asked for the person id: " + id;
    });

    // 监听端口
    server.listen(8085);

    // 等待服务器停止
    server.wait();
}

编译并运行上述代码,你的Http服务器将在本地监听8085端口。

3. 应用案例和最佳实践

以下是一些使用Beauty库的应用案例:

同步客户端请求

#include <beauty/beauty.hpp>
#include <iostream>

int main() {
    // 创建客户端实例
    beauty::client client;

    // 发起请求
    auto [ec, response] = client.get("http://127.0.0.1:8085");

    // 检查结果
    if (!ec) {
        if (response.is_status_ok()) {
            // 打印响应体
            std::cout << response.body() << std::endl;
        } else {
            std::cout << response.status() << std::endl;
        }
    } else {
        // 发生错误
        std::cout << ec << ": " << ec.message() << std::endl;
    }
}

异步客户端请求

#include <beauty/beauty.hpp>
#include <iostream>
#include <chrono>

int main() {
    // 创建客户端实例
    beauty::client client;

    // 发起异步请求
    client.get("http://127.0.0.1:8085", [](auto ec, auto&& response) {
        if (!ec) {
            if (response.is_status_ok()) {
                // 打印响应体
                std::cout << response.body() << std::endl;
            } else {
                std::cout << response.status() << std::endl;
            }
        } else {
            // 发生错误
            std::cout << ec << ": " << ec.message() << std::endl;
        }
    });

    // 等待一段时间以接收响应
    for (int i = 0; i < 10; ++i) {
        std::cout << '.'; std::cout.flush();
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }
    std::cout << std::endl;
}

定时器使用

#include <beauty/beauty.hpp>
#include <iostream>

int main() {
    // 启动一个每250毫秒重复的定时器
    int timer_count = 4;
    beauty::repeat(0.250, [&timer_count]() {
        std::cout << "Tick..." << std::endl;
        if (--timer_count == 0) {
            std::cout << "Dring !" << std::endl;
            beauty::stop();
        }
    });

    // 启动一个600毫秒后的一次性定时器
    beauty::after(0.600, [] {
        std::cout << "Snooze !" << std::endl;
    });

    // 等待定时器结束
    beauty::wait();
}

4. 典型生态项目

目前Beauty项目的主要生态项目包括其依赖的Boost.Beast和OpenSSL库。此外,社区中可能已经有一些基于Beauty的项目,你可以通过搜索和探索来发现这些项目,并了解如何将Beauty集成到更广泛的应用程序中。

beauty A Simple C++ Http server/client above Boost.Beast beauty 项目地址: https://gitcode.com/gh_mirrors/beauty7/beauty

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

常煦梦Vanessa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值