10.引入cpphttplib库|使用测试完成调用httplib(C++)

创建http_server.cc

touch http_server.cc

![[Pasted image 20250218085105.png]]

http_server.cc

#include "searcher.hpp"
  
int main()
{


	return 0;
}

makefile

PARSER=parser
DUG=debug
HTTP_SERVER=http_server
cc=g++

.PHONY:all
all:$(PARSER) $(DUG) $(HTTP_SERVER)

$(PARSER):parser.cc
	$(cc) -o $@ $^ -lboost_system -lboost_filesystem -std=c++11
$(DUG):debug.cc
	$(cc) -o $@ $^ -ljsoncpp -std=c++11
$(HTTP_SERVER):http_server.cc
	$(cc) -o $@ $^ -ljsoncpp -std=c++11
.PHONY:clean
clean:
	rm -f $(PARSER) $(DUG) $(HTTP_SERVER)

![[Pasted image 20250218094949.png]]

引入cpp-httplib库

地址:
GitHub - yhirose/cpp-httplib: A C++ header-only HTTP/HTTPS server and client library
这里引入0.7.15版本
![[Pasted image 20250218102123.png]]

直接将文件拖入
![[Pasted image 20250218102245.png]]

unzip cpp-httplib-0.7.15.zip

解压缩文件
![[Pasted image 20250218102826.png]]

将cpp-httplib引入

ln -s ./test/cpp-httplib-0.7.15 cpp-httplib
使用httplib库

makefile引入pthread库

PARSER=parser
DUG=debug
HTTP_SERVER=http_server
cc=g++

.PHONY:all
all:$(PARSER) $(DUG) $(HTTP_SERVER)

$(PARSER):parser.cc
        $(cc) -o $@ $^ -lboost_system -lboost_filesystem -std=c++11
$(DUG):debug.cc
        $(cc) -o $@ $^ -ljsoncpp -std=c++11
$(HTTP_SERVER):http_server.cc
        $(cc) -o $@ $^ -ljsoncpp -lpthread -std=c++11
.PHONY:clean
clean:
        rm -f $(PARSER) $(DUG) $(HTTP_SERVER)

http_server.cc

#include "searcher.hpp"
#include "cpp-httplib/httplib.h"

int main()
{
	httplib::Server svr;


	svr.Get("/hi", [](const httplib::Request &req, httplib::Response &rsp){
				rsp.set_content("hello world!", "text/plain; charset=utf-8");
				});
	
	svr.listen("0.0.0.0", 8081);
	return 0;
}

测试

运行http_server

netstat -ntlp

![[Pasted image 20250218121830.png]]

有一个8081的服务,处于listen状态
![[Pasted image 20250218162353.png]]

新建一个wwwroot目录

mkdir wwwroot

![[Pasted image 20250218162547.png]]

在wwwroot文件夹里创建一个index.html
![[Pasted image 20250218164116.png]]

<!DOCTYPE html>
<html>
<head>
        <meta charset="UTF-8">
        <title>for test</title>
</head>
<body>
        <h1>你好,世界</h1>
        <p>这是个httplib的测试</p>
</body>
</html>

![[Pasted image 20250218165051.png]]

完成http调用
#include "cpp-httplib/httplib.h"
#include "searcher.hpp"

const std::string input = "data/raw_html/raw.txt";
const std::string root_path = "./wwwroot";

int main()
{
    ns_searcher::Searcher search;
    search.InitSearcher(input);

    httplib::Server svr;
    svr.set_base_dir(root_path.c_str());
    svr.Get("/s", [&search](const httplib::Request &req, httplib::Response &rsp){
		if(!req.has_param("word")){
			rsp.set_content("必须要有搜索关键字!", "text/plain; charset=utf-8");
			return;
		}
		std::string word = req.get_param_value("word");
		std::cout << "用户在搜索:" << word << std::endl;

		std::string json_string;
		search.Search(word, &json_string);
		rsp.set_content(json_string, "application/json");
		//rsp.set_content("你好,世界!", "text/plain; charset=utf-8");
		});

    svr.listen("0.0.0.0", 8081);
    return 0;
}

![[Pasted image 20250218173919.png]]

![[Pasted image 20250218174014.png]]

![[Pasted image 20250218174048.png]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值