dmpservermanagerapi.cpp 2.0 KB
/**************************************************************************
* 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));
}