创建post接口
说明
使用C++ 的 httplib创建http服务,演示了一个创建了post接口的函数。包含日志模块
具体的逻辑代码没有贴进来,根据需求实现
main.cpp
#include "http/httplib.h"
#include "http/detect.hpp"
#include "ocr/ocr.hpp"
#include "http/serverLog.hpp"
#include "plog/Log.h"
#include "plog/Initializers/RollingFileInitializer.h"
#include <csignal>
void signalHandler( int signum )
{
PLOGW << "Shut down http server";
exit(signum);
}
int main()
{
signal(SIGINT, signalHandler);
plog::init(plog::info, "ocr_match.log", 10000000, 2);
ocr::TemplateOcr tocr;
httplib::Server svr;
PLOGI << "Start server";
svr.set_logger(server_logger);
svr.Post("/ocr/structed", [&](const httplib::Request& req, httplib::Response& res) {
{
PLOGI << "Receive request";
detect(req, res, tocr);
PLOGI << "End request";
}
});
PLOGI << "Server listen on 8000";
svr.listen("0.0.0.0", 8000);
return 0;
}
serverLog.hpp + serverLog.cpp
#ifndef SERVERLOG_HPP
#define SERVERLOG_HPP
#include "http/httplib.h"
#include "plog/Log.h"
void server_logger(const httplib::Request& req, const httplib::Response& res);
#endif
#include "http/serverLog.hpp"
void server_logger(const httplib::Request& req, const httplib::Response& res)
{
std::string line;
if (res.status == 200)
{
line = std::string("\033[32m")
+ std::to_string(res.status)
+ std::string(" ")
+ std::string(httplib::detail::status_message(res.status))
+ std::string("\033[0m");
}
else
{
line = std::string("\033[31m")
+ std::to_string(res.status)
+ std::string(" ")
+ std::string(httplib::detail::status_message(res.status))
+ std::string("\033[0m");
}
PLOGI << req.remote_addr << ":"
<< req.remote_port << " - "
<< "\"" << req.method << " "
<< req.path << " "
<< res.version << "\" "
<< line;
}
具体的实现放在detect函数里面,将入参和出参全部传入。
网友评论