dmpservermanagerapi.cpp
2.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**************************************************************************
* file: dmpservermanagerapi.cpp
* Author: wanzhongping
* Date: 2021-07-15 11:07:43
* Email: zhongpingw@chinadci.com
* copyright: 广州城市信息研究所有限公司
***************************************************************************/
#include "dmpservermanagerapi.h"
#include <boost/regex.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <iostream>
#include "dmpserverapicontext.h"
#include "dmpserverrequest.h"
#include "dmpserverresponse.h"
DmpServerManagerApi::DmpServerManagerApi(const std::string &path, const std::string &name, const std::string &description, const std::string &version ):
path_(path),
name_(name),
description_(description),
version_(version)
{
}
DmpServerManagerApi::~DmpServerManagerApi()
{
}
void DmpServerManagerApi::executeRequest(const DmpServerApiContext &context)
{
bool hasMatch = false;
std::string path = context.request()->path();
for (const auto &handler : handlers_)
{
boost::cmatch what;
//不区分大小写
boost::regex reg(handler->Path(), boost::regex_constants::icase);
if(boost::regex_match(path.c_str(), what, reg))
{
hasMatch = true;
if(what.size() == 2) {
std::string operation = what[1].str();
boost::to_lower(operation);
handler->set_operation(operation);
}
handler->HandleRequest(context);
break;
}
}
if(!hasMatch) {
context.response()->writeHtml("错误,找不到处理的模块,请确认请求接口是否正确!");
}
}
void DmpServerManagerApi::registerHandler(DmpServerApiHandler *handler)
{
std::shared_ptr<DmpServerApiHandler> hp(handler);
handlers_.emplace_back(std::move(hp));
}