美文网首页
c++ web框架wfrest使用示例

c++ web框架wfrest使用示例

作者: 一路向后 | 来源:发表于2022-02-13 21:03 被阅读0次

1.源码实现

#include <iostream>
#include <wfrest/HttpServer.h>

using namespace wfrest;

int main()
{
    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));
    });

    // 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());
    });

    if(svr.start(8888) == 0)
    {
        getchar();
        svr.stop();
    }
    else
    {
        fprintf(stderr, "Cannot start server");
        exit(1);
    }

    return 0;
}

2.编译源码

$ g++ -o test test.cpp -std=c++11 -lwfrest -lworkflow -lz -Wl,-rpath=/usr/local/lib

3.运行及其结果

$ ./test
$ curl http://localhost:8888/hello
world

相关文章

网友评论

      本文标题:c++ web框架wfrest使用示例

      本文链接:https://www.haomeiwen.com/subject/cvdqlrtx.html