删除配置文件拦截器改为代码控制
This commit is contained in:
@@ -82,7 +82,7 @@ bool fastweb::app::start(const std::string& config_filepath)
|
||||
return false;
|
||||
}
|
||||
auto website = m_center->website_byname(ws_config.name);
|
||||
auto router = website->router();
|
||||
router = website->router();
|
||||
|
||||
// 初始化脚本
|
||||
if (initialization_script() == false)
|
||||
@@ -92,10 +92,6 @@ bool fastweb::app::start(const std::string& config_filepath)
|
||||
|
||||
// 加载订阅
|
||||
subscribe->load(router);
|
||||
// 加载拦截器
|
||||
interceptor->load(router);
|
||||
|
||||
|
||||
|
||||
return m_center->start();
|
||||
}
|
||||
@@ -103,8 +99,10 @@ bool fastweb::app::start(const std::string& config_filepath)
|
||||
void fastweb::app::stop()
|
||||
{
|
||||
this->m_lastErrorDesc.clear();
|
||||
|
||||
subscribe->clear();
|
||||
interceptor->clear();
|
||||
router = nullptr;
|
||||
if (m_center != nullptr)
|
||||
{
|
||||
m_center->close();
|
||||
|
||||
@@ -31,6 +31,8 @@ namespace fastweb
|
||||
// 网站服务核心
|
||||
network::http::center* m_center = nullptr;
|
||||
public:
|
||||
// 网站路由
|
||||
network::http::router* router = nullptr;
|
||||
// 日志
|
||||
std::shared_ptr<fastweb::log> log;
|
||||
// 订阅管理器
|
||||
|
||||
@@ -109,21 +109,6 @@ void fastweb::config::cache()
|
||||
log.warn = m_ini.read("log", "warn") == "1";
|
||||
log.error = m_ini.read("log", "error") == "1";
|
||||
|
||||
{
|
||||
ylib::json interceptors = ylib::json::from(m_ini.read("website", "interceptor_scripts"));
|
||||
for (size_t i = 0; i < interceptors.size(); i++)
|
||||
{
|
||||
if (interceptors[i].size() != 2)
|
||||
{
|
||||
LOG_WARN("Interceptor configuration, if the "+std::to_string(i) + "th array has non 2 members, this interceptor will be invalidated.");
|
||||
continue;
|
||||
}
|
||||
website::__interceptor inr;
|
||||
inr.filepath = interceptors[i][0].to<std::string>();
|
||||
inr.regex_express = interceptors[i][1].to<std::string>();
|
||||
website.interceptor_scripts.push_back(inr);
|
||||
}
|
||||
}
|
||||
|
||||
// website 域名参数
|
||||
for(size_t i=0;i< website.domain.size();i++)
|
||||
|
||||
@@ -29,7 +29,6 @@ namespace fastweb
|
||||
std::string session_dir;
|
||||
uint32 session_timeout_sec;
|
||||
std::string Initialization_script;
|
||||
std::vector<__interceptor> interceptor_scripts;
|
||||
bool debug = false;
|
||||
std::vector<std::string> domain;
|
||||
};
|
||||
|
||||
@@ -23,11 +23,13 @@ extern "C" {
|
||||
#ifdef _WIN32
|
||||
DLL_EXPORT
|
||||
#endif
|
||||
void fastweb_close(void* app)
|
||||
{
|
||||
auto a = static_cast<fastweb::app*>(app);
|
||||
a->stop();
|
||||
delete a;
|
||||
}
|
||||
void fastweb_close(void* app)
|
||||
{
|
||||
if (app == nullptr)
|
||||
return;
|
||||
auto a = static_cast<fastweb::app*>(app);
|
||||
a->stop();
|
||||
delete a;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,25 +12,33 @@ fastweb::interceptor_manager::~interceptor_manager()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
void fastweb::interceptor_manager::load(network::http::router* router)
|
||||
bool fastweb::interceptor_manager::add(const std::string& regex_express, const std::string& filepath)
|
||||
{
|
||||
clear();
|
||||
m_router = router;
|
||||
for (size_t i = 0; i < app()->config->website.interceptor_scripts.size(); i++)
|
||||
if (interceptor_manager::interceptor.exist(regex_express))
|
||||
{
|
||||
interceptor_manager::interceptor.emplace(app()->config->website.interceptor_scripts[i].regex_express, app()->config->website.interceptor_scripts[i].filepath);
|
||||
router->interceptor()->add(app()->config->website.interceptor_scripts[i].regex_express, [&](network::http::reqpack* reqpack, const std::string& express_string)->bool {
|
||||
return this->callback(reqpack, express_string);
|
||||
});
|
||||
LOG_WARN("`" + regex_express + "` interceptor is invalid, duplicate rule, filepath: " + filepath);
|
||||
return false;
|
||||
}
|
||||
interceptor_manager::interceptor.add(regex_express, filepath);
|
||||
app()->router->interceptor()->add(regex_express, [this](network::http::reqpack* reqpack, const std::string& express_string)->bool {
|
||||
return this->callback(reqpack, express_string);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
bool fastweb::interceptor_manager::remove(const std::string& regex_express)
|
||||
{
|
||||
interceptor_manager::interceptor.del(regex_express);
|
||||
return app()->router->interceptor()->remove(regex_express);
|
||||
}
|
||||
bool fastweb::interceptor_manager::exist(const std::string& regex_express)
|
||||
{
|
||||
return interceptor_manager::interceptor.exist(regex_express);
|
||||
}
|
||||
|
||||
void fastweb::interceptor_manager::clear()
|
||||
{
|
||||
if(m_router != nullptr)
|
||||
m_router->interceptor()->clear();
|
||||
interceptor_manager::interceptor.clear();
|
||||
m_router = nullptr;
|
||||
if(app()->router != nullptr)
|
||||
app()->router->interceptor()->clear();
|
||||
}
|
||||
|
||||
bool fastweb::interceptor_manager::callback(network::http::reqpack* reqpack, const std::string& express_string)
|
||||
@@ -61,7 +69,7 @@ bool fastweb::interceptor_manager::callback(network::http::reqpack* reqpack, con
|
||||
{
|
||||
exception_string = e.what();
|
||||
if (app()->config->website.debug)
|
||||
LOG_ERROR("[subscribe_interceptor][" + reqpack->request()->filepath() + "]: " + e.what());
|
||||
LOG_ERROR("[interceptor][" + reqpack->request()->filepath() + "]: " + e.what());
|
||||
}
|
||||
lua->state->collect_garbage();
|
||||
app()->state->push(lua);
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "net/http_request.h"
|
||||
#include "net/http_response.h"
|
||||
#include "net/http_router.h"
|
||||
#include "util/map.hpp"
|
||||
namespace fastweb
|
||||
{
|
||||
/// <summary>
|
||||
@@ -17,9 +18,10 @@ namespace fastweb
|
||||
interceptor_manager(fastweb::app* app);
|
||||
~interceptor_manager();
|
||||
|
||||
void load(network::http::router* router);
|
||||
bool add(const std::string& regex_express,const std::string& filepath);
|
||||
bool remove(const std::string& regex_express);
|
||||
bool exist(const std::string& regex_express);
|
||||
void clear();
|
||||
|
||||
private:
|
||||
/// <summary>
|
||||
/// 服务回调
|
||||
@@ -28,7 +30,6 @@ namespace fastweb
|
||||
/// <param name="express_string"></param>
|
||||
bool callback(network::http::reqpack* reqpack, const std::string& express_string);
|
||||
private:
|
||||
network::http::router* m_router = nullptr;
|
||||
std::map<std::string, std::string> interceptor;
|
||||
ylib::map<std::string, std::string> interceptor;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "module/http/response.h"
|
||||
#include "module/http/session.h"
|
||||
#include "module/http/httpclient.h"
|
||||
#include "module/http/interceptor.h"
|
||||
#include "module/mysql.h"
|
||||
#ifdef _WIN32
|
||||
#include "module/mssql.h"
|
||||
@@ -119,6 +120,7 @@ void fastweb::module_manager::load_core(sol::state* lua)
|
||||
|
||||
module::request::regist(lua);
|
||||
module::response::regist(lua);
|
||||
module::interceptor::regist(lua);
|
||||
module::session::regist(lua);
|
||||
module::httpclient::regist(lua);
|
||||
module::mysql_regist(lua);
|
||||
|
||||
44
src/module/http/interceptor.cpp
Normal file
44
src/module/http/interceptor.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "interceptor.h"
|
||||
#include "core/app.h"
|
||||
|
||||
module::interceptor::interceptor()
|
||||
{
|
||||
}
|
||||
|
||||
module::interceptor::~interceptor()
|
||||
{
|
||||
}
|
||||
|
||||
bool module::interceptor::add(const std::string& express_string, const std::string& filepath, sol::this_state ts)
|
||||
{
|
||||
GET_APP;
|
||||
return app->interceptor->add(express_string,app->config->website.dir+ filepath);
|
||||
}
|
||||
|
||||
bool module::interceptor::remove(const std::string& express_string, sol::this_state ts)
|
||||
{
|
||||
GET_APP;
|
||||
return app->interceptor->remove(express_string);
|
||||
}
|
||||
|
||||
bool module::interceptor::exist(const std::string& express_string, sol::this_state ts)
|
||||
{
|
||||
GET_APP;
|
||||
return app->interceptor->exist(express_string);
|
||||
}
|
||||
|
||||
void module::interceptor::clear(sol::this_state ts)
|
||||
{
|
||||
GET_APP;
|
||||
return app->interceptor->clear();
|
||||
}
|
||||
|
||||
void module::interceptor::regist(sol::state* lua)
|
||||
{
|
||||
lua->new_usertype<module::interceptor>("interceptor",
|
||||
"add", &module::interceptor::add,
|
||||
"remove", &module::interceptor::remove,
|
||||
"exist", &module::interceptor::exist,
|
||||
"clear", &module::interceptor::clear
|
||||
);
|
||||
}
|
||||
39
src/module/http/interceptor.h
Normal file
39
src/module/http/interceptor.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
#include "net/http_request.h"
|
||||
#include "net/http_response.h"
|
||||
#include "sol/sol.hpp"
|
||||
|
||||
namespace module
|
||||
{
|
||||
class interceptor
|
||||
{
|
||||
public:
|
||||
interceptor();
|
||||
~interceptor();
|
||||
/// <summary>
|
||||
/// 添加
|
||||
/// </summary>
|
||||
/// <param name="express_string"></param>
|
||||
/// <param name="filepath"></param>
|
||||
/// <returns></returns>
|
||||
static bool add(const std::string& express_string,const std::string& filepath, sol::this_state ts);
|
||||
/// <summary>
|
||||
/// 移除
|
||||
/// </summary>
|
||||
/// <param name="express_string"></param>
|
||||
static bool remove(const std::string& express_string, sol::this_state ts);
|
||||
/// <summary>
|
||||
/// 是否存在
|
||||
/// </summary>
|
||||
/// <param name="express_string"></param>
|
||||
/// <returns></returns>
|
||||
static bool exist(const std::string& express_string, sol::this_state ts);
|
||||
/// <summary>
|
||||
/// 清空
|
||||
/// </summary>
|
||||
static void clear(sol::this_state ts);
|
||||
|
||||
static void regist(sol::state* lua);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user