4 Star 8 Fork 2

樊智慧/faceHttpd

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.cpp 5.16 KB
一键复制 编辑 原始数据 按行查看 历史
樊智慧 提交于 2019-09-19 16:16 +08:00 . handler.cpp
#include <iostream>
#include <string>
#include "httplib.h"
#include <stdio.h>
#include "log/logger.h"
#include "handler.h"
#include "face_api_helper.h"
#include "return_json_object.h"
#include "baidu_face_api.h"
#include "face_container.h"
#include "config.h"
using namespace std;
using namespace httplib;
std::string dump_headers(const Headers &headers);
std::string log(const Request &req, const Response &res);
std::function<void(const Request&,Response&)> get_handler(const string& path,
std::function<shared_ptr<BaseObjectIf>(const string&)> handler);
void register_route(Server& svr);
void parse_argv(int argc, char* argv[]);
int main(int argc, char* argv[])
{
string process(argv[0]);
process = process.substr(2,-1); // remove "./"
LOG_OPEN( process.c_str(), LOG_PID, LOG_LOCAL0);
parse_argv(argc,argv);
FaceApiHelper::getInstance()->sdk_init(false);
shared_ptr<FaceSerializeIf> serial(new FileFaceSerialize(Config::face_db_file));
FaceContainer::getInstance()->Init(serial);
Server svr;
if (!svr.is_valid()) {
LOG(LOG_INFO,"server has an error...");
return -1;
}
//注册路由
register_route(svr);
LOG( LOG_INFO, "Face Api Http Server(%s,%d) starting...", Config::binding_ip.c_str(), Config::binding_port);
svr.listen(Config::binding_ip.c_str(), Config::binding_port);
return 0;
}
std::string dump_headers(const Headers &headers)
{
std::string s;
char buf[BUFSIZ];
for (auto it = headers.begin(); it != headers.end(); ++it) {
const auto &x = *it;
snprintf(buf, sizeof(buf), "%s: %s\n", x.first.c_str(), x.second.c_str());
s += buf;
}
return s;
}
std::string log(const Request &req, const Response &res)
{
std::string s;
char buf[BUFSIZ];
s += "================================\n";
snprintf(buf, sizeof(buf), "%s %s %s", req.method.c_str(),
req.version.c_str(), req.path.c_str());
s += buf;
std::string query;
for (auto it = req.params.begin(); it != req.params.end(); ++it) {
const auto &x = *it;
snprintf(buf, sizeof(buf), "%c%s=%s",
(it == req.params.begin()) ? '?' : '&', x.first.c_str(),
x.second.c_str());
query += buf;
}
snprintf(buf, sizeof(buf), "%s\n", query.c_str());
s += buf;
s += dump_headers(req.headers);
s += "--------------------------------\n";
snprintf(buf, sizeof(buf), "%d %s\n", res.status, res.version.c_str());
s += buf;
s += dump_headers(res.headers);
s += "\n";
if (!res.body.empty()) { s += res.body; }
s += "\n";
return s;
}
std::function<void(const Request&,Response&)> get_handler(const string& path,
std::function<shared_ptr<BaseObjectIf>(const string&)> handler)
{
return [path,handler](const Request& req, Response& res) {
shared_ptr<BaseObjectIf> obj = handler(req.body);
res.headers.emplace("Connection","close");
string res_body = Serialize(obj);
res.set_content(res_body, "application/json");
};
}
void register_route(Server& svr)
{
svr.Post("/face/attr", get_handler("/face/attr",handlerGetFaceAttr));
svr.Post("/face/quality", get_handler("/face/quality",handlerGetFaceQuality));
svr.Post("/face/feature", get_handler("/face/feature",handlerGetFaceFeature));
svr.Post("/face/matchByImg", get_handler("/face/matchByImg",handlerMatchFaceByImg));
svr.Post("/face/register", get_handler("/face/register",handlerRegisterFace));
svr.Post("/face/search", get_handler("/face/search",handlerSearchFace));
svr.Post("/face/multSearch", get_handler("/face/multSearch",handlerMultSearchFace));
svr.Post("/face/remove", get_handler("/face/remove",handlerRemoveFace));
svr.Post("/face/clear", get_handler("/face/clear",handlerClearFaces));
svr.Post("/face/multTrack",get_handler("/face/multTrack",handlerTrackFaces));
svr.set_error_handler([](const Request & req /*req*/, Response &res) {
shared_ptr<BaseObjectIf> obj = handlerNoHandler(req.body);
res.headers.emplace("Connection","close");
string res_body = Serialize(obj);
res.set_content(res_body, "application/json");
});
svr.set_logger([](const Request &req, const Response &res) {
fprintf(stderr,"%s",log(req, res).c_str());
LOG(LOG_INFO, "%s", log(req,res).c_str());
});
}
void parse_argv(int argc, char* argv[])
{
auto ch = 0;
while( (ch = getopt(argc, argv, "i:f:p:n:")) != -1)
{
fprintf(stderr,"optind: %d\n", optind);
switch (ch)
{
case 'i':
fprintf(stderr,"have option: -i\n");
fprintf(stderr,"the argument of -i is bind ip %s\n\n", optarg);
Config::binding_ip = optarg;
break;
case 'f':
fprintf(stderr,"have option: -f\n");
fprintf(stderr,"the argument of -f is face db file %s\n\n", optarg);
Config::face_db_file = optarg;
break;
case 'p':
fprintf(stderr,"HAVE option: -p\n");
fprintf(stderr,"the argument of -p is port %s\n\n",optarg);
Config::binding_port = atoi(optarg);
break;
case 'n':
fprintf(stderr,"HAVE option: -p\n");
fprintf(stderr,"the argument of -n faces per async task %s\n\n",optarg);
Config::faces_per_async_task = atoi(optarg);
break;
case '?':
fprintf(stderr,"Unknown option: %c\n",(char)optopt);
break;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/wisefan/faceHttpd.git
git@gitee.com:wisefan/faceHttpd.git
wisefan
faceHttpd
faceHttpd
20190718

搜索帮助