最近 sogou開源的workflow項目在github上得到了很大的關(guān)注,抱著學(xué)習(xí)的心態(tài)研究一下。https://github.com/sogou/workflow
一、編譯安裝
git clone --recursive https://github.com/sogou/workflow.git
cd workflow
./configure #在workflow目錄下創(chuàng)建build文件夾,整個是用cmake來編譯的。
cd build
make #編譯成功在workflow目錄下生成兩個文件夾_include _lib
編譯還是非常方便的,只需要依賴OpenSSL>=1.1
二、搭建一個簡單的服務(wù)端程序
/*
* @Author: Lei Jiang
* @Email: leijiang420@163.com
* @Date: 2020-10-30 15:53:26
* @Description: code description
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include "workflow/HttpMessage.h"
#include "workflow/HttpUtil.h"
#include "workflow/WFServer.h"
#include "workflow/WFHttpServer.h"
#include "workflow/WFFacilities.h"
#include <iostream>
using namespace std;
static WFFacilities::WaitGroup wait_group(1);
void process(WFHttpTask *server_task)
{
cout << "get one post" << endl;
protocol::HttpResponse *resp = server_task->get_resp();
string res_content = "fuck you baby";
resp->append_output_body(res_content.c_str(),res_content.size());
resp->set_status_code("200");
}
int main(int argc, char **argv)
{
unsigned short port;
if (argc != 2)
{
cerr << "USAGE:" << argv[0] << " 8080" << endl;
exit(1);
}
WFHttpServer server_obj(process);
port = atoi(argv[1]);
if (server_obj.start(port) == 0)
{
cout << "server start ok" << endl;
wait_group.wait();
server_obj.stop();
}
else
{
cout << "start server occurs problem" << endl;
}
return 0;
}
測試通過,簡單易使用。