首页 > 编程知识 正文

restc: 极简的 RESTful Web 服务框架

时间:2023-11-21 16:43:54 阅读:293288 作者:PARU

restc 是一个基于 C++11 标准的,精简且易用的RESTful Web服务框架。

一、简介

restc 这个名字就已经很明显的表达其理念,它是一个以RESTful为核心的 Web 服务框架。

相较于其他 Web 服务框架,restc 具有简化、易用性两个特点:

一方面,restc 可以运行在 Linux、Windows、Mac 等多个操作系统上,并不依赖其他任何第三方库。

另一方面,restc 很小巧,代码非常精简,易于理解和扩展。它推崇 MVC 模式,并通过反射和模板实现了一套清晰的 WEB 开发框架。

二、核心组件

restc 的核心组件包含 3 个类,分别是 Server,Application 和 Controller。

1、Server

class Server
{
public:
    Server();
    virtual ~Server();

    void setDocRoot(const std::string& path);
    void setAddress(const std::string& ip, const int port);
    void start();
    void stop();

    template<typename ControllerType>
    void addController(const std::string& uri)
    {
        controllers_[uri] = std::make_shared<ControllerType>();
    }

private:
    std::string docRoot_;
    std::string addressIp_;
    int addressPort_;
    std::vector<std::thread> threads_;
    std::atomic<bool> isRunning_;
    std::unordered_map<std::string, std::shared_ptr<Controller>> controllers_;
};

Server 实现了服务端的基本功能,包括启动和停止,以及设置物理文件的根目录和监听地址,同时支持注册 Controller。

2、Application

class Application
{
public:
    Application() {}
    virtual ~Application() {}
    virtual void onRequest(HttpRequest& request, HttpResponse& response) {}
};

Application 是一个抽象类,定义了一系列虚方法,用来处理各种 HTTP 请求。一旦发现匹配的 Url,即会回调相应 Application 的实例方法。

3、Controller

class Controller
{
public:
    Controller() {}
    virtual ~Controller() {}

    virtual void onGet(HttpRequest& request, HttpResponse& response) {}
    virtual void onPost(HttpRequest& request, HttpResponse& response) {}
    virtual void onPut(HttpRequest& request, HttpResponse& response) {}
    virtual void onDelete(HttpRequest& request, HttpResponse& response) {}
};

Controller 定义了四个虚方法,用于接收 HTTP 方法请求,并最终生成响应。

三、使用案例

1、Hello World

使用 restc 搭建出来的 HelloWorld 应用程序,如下:

#include "restc.h"
using namespace restc;

class HelloWorldController : public Controller
{
public:
    void onGet(HttpRequest& request, HttpResponse& response)
    {
        response.setContentType("text/plain");
        response.setBody("Hello, world!");
    }
};

class HelloWorldApp : public Application
{
public:
    HelloWorldApp()
    {
        addController<HelloWorldController>("/");
    }
};

int main(int argc, char** argv)
{
    Server server;
    HelloWorldApp app;

    server.setAddress("0.0.0.0", 8080);
    server.addController<HelloWorldController>("/");
    server.start();

    return 0;
}

上述代码实现了一个简单的 Web 服务器,直接监听请求,并返回 “Hello, world!” 字符串。

2、文件上传

使用 restc 实现文件上传功能,如下:

#include "restc.h"
using namespace restc;

class UploadController : public Controller
{
public:
    void onPost(HttpRequest& request, HttpResponse& response)
    {
        auto files = request.getUploadFiles();
        if(files.size() == 1)
        {
            auto file = files[0];
            std::string content = file.getContent();
            response.setContentType("text/plain");
            response.setBody(std::move(content));
        }else{
            response.setContentType("text/plain");
            response.setBody("Multiple file uploads are not currently supported.");
        }
    }
};

class UploadApplication : public Application
{
public:
    UploadApplication()
    {
        addController<UploadController>("/upload");
    }
};

int main()
{
    Server server;
    UploadApplication app;

    server.setAddress("0.0.0.0", 8080);
    server.start();

    return 0;
}

上述代码中,实现了一个简单的文件上传功能。用户只需要 POST 上传一个文件即可,后台将自动将文件内容返回。

3、参数传递

使用 restc 实现参数传递功能,如下:

#include "restc.h"
using namespace restc;

class UserController : public Controller
{
public:
    void onGet(HttpRequest& request, HttpResponse& response)
    {
        auto name = request.getParam("name");
        response.setContentType("text/plain");
        response.setBody("Hello, " + name + "!");
    }
};

class UserApplication : public Application
{
public:
    UserApplication()
    {
        addController<UserController>("/user");
    }
};

int main()
{
    Server server;
    UserApplication app;

    server.setAddress("0.0.0.0", 8080);
    server.start();

    return 0;
}

上述代码中,实现了一个简单的参数传递功能。用户在请求 /user 页面时,会带上参数 name,后台程序会将参数包装成字符串,并返回相应的欢迎语。

四、总结

本文详细的介绍了 restc RESTful Web 服务框架的使用,从核心组件的介绍开始,到 Hello World、文件上传、参数传递等案例的实现,都做了详细的阐述。restc 具有简化、易用性两个特点,便于理解和扩展,可以让开发者轻松地构建出一个稳定、高效的 Web 服务应用。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。