diff --git a/.gitignore b/.gitignore index 333beab..929bd02 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ /obj /bin +/log diff --git a/config.ini b/config.ini index 2ff7247..f54ee2b 100644 --- a/config.ini +++ b/config.ini @@ -27,8 +27,6 @@ session_dir=${base}/session session_timeout_sec=86400 ; 初始化加载脚本(网站程序启动) Initialization_script=${www}/api/lib/init.lua -; 拦截器 -interceptor_scripts= [["${www}/api/lib/interceptor.lua","/api/*.*"]] ; 调试模式,开启后启用LUA错误信息 (0=关闭 1=开启) debug=1 ; 绑定域名 diff --git a/src/core/app.cpp b/src/core/app.cpp index 6c8b057..2866a91 100644 --- a/src/core/app.cpp +++ b/src/core/app.cpp @@ -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(); diff --git a/src/core/app.h b/src/core/app.h index 6ce624c..73bb5c0 100644 --- a/src/core/app.h +++ b/src/core/app.h @@ -31,6 +31,8 @@ namespace fastweb // 网站服务核心 network::http::center* m_center = nullptr; public: + // 网站路由 + network::http::router* router = nullptr; // 日志 std::shared_ptr log; // 订阅管理器 diff --git a/src/core/config.cpp b/src/core/config.cpp index 5e89936..bfc4efb 100644 --- a/src/core/config.cpp +++ b/src/core/config.cpp @@ -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(); - inr.regex_express = interceptors[i][1].to(); - website.interceptor_scripts.push_back(inr); - } - } // website 域名参数 for(size_t i=0;i< website.domain.size();i++) diff --git a/src/core/config.h b/src/core/config.h index a15bd8e..c5f404d 100644 --- a/src/core/config.h +++ b/src/core/config.h @@ -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 domain; }; diff --git a/src/core/entry.cpp b/src/core/entry.cpp index 2247516..297b34a 100644 --- a/src/core/entry.cpp +++ b/src/core/entry.cpp @@ -23,11 +23,13 @@ extern "C" { #ifdef _WIN32 DLL_EXPORT #endif - void fastweb_close(void* app) - { - auto a = static_cast(app); - a->stop(); - delete a; - } + void fastweb_close(void* app) + { + if (app == nullptr) + return; + auto a = static_cast(app); + a->stop(); + delete a; + } } diff --git a/src/core/interceptormanager.cpp b/src/core/interceptormanager.cpp index 71fd99c..c5d25b9 100644 --- a/src/core/interceptormanager.cpp +++ b/src/core/interceptormanager.cpp @@ -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); diff --git a/src/core/interceptormanager.h b/src/core/interceptormanager.h index d38fdcf..e89cfa3 100644 --- a/src/core/interceptormanager.h +++ b/src/core/interceptormanager.h @@ -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 { /// @@ -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: /// /// 服务回调 @@ -28,7 +30,6 @@ namespace fastweb /// bool callback(network::http::reqpack* reqpack, const std::string& express_string); private: - network::http::router* m_router = nullptr; - std::map interceptor; + ylib::map interceptor; }; } diff --git a/src/core/modulemanager.cpp b/src/core/modulemanager.cpp index 8b04647..c0b6b35 100644 --- a/src/core/modulemanager.cpp +++ b/src/core/modulemanager.cpp @@ -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); diff --git a/src/module/http/interceptor.cpp b/src/module/http/interceptor.cpp new file mode 100644 index 0000000..321fc23 --- /dev/null +++ b/src/module/http/interceptor.cpp @@ -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("interceptor", + "add", &module::interceptor::add, + "remove", &module::interceptor::remove, + "exist", &module::interceptor::exist, + "clear", &module::interceptor::clear + ); +} diff --git a/src/module/http/interceptor.h b/src/module/http/interceptor.h new file mode 100644 index 0000000..0d2f7fc --- /dev/null +++ b/src/module/http/interceptor.h @@ -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(); + /// + /// 添加 + /// + /// + /// + /// + static bool add(const std::string& express_string,const std::string& filepath, sol::this_state ts); + /// + /// 移除 + /// + /// + static bool remove(const std::string& express_string, sol::this_state ts); + /// + /// 是否存在 + /// + /// + /// + static bool exist(const std::string& express_string, sol::this_state ts); + /// + /// 清空 + /// + static void clear(sol::this_state ts); + + static void regist(sol::state* lua); + }; +} + diff --git a/www/api/lib/init.lua b/www/api/lib/init.lua index c57cd06..6aa0a15 100644 --- a/www/api/lib/init.lua +++ b/www/api/lib/init.lua @@ -4,5 +4,6 @@ print("init fast web success!") -- 自定义模块演示 local hello = hello.new() print("Custom Module: "..hello:name()) - +-- 增加拦截器 +interceptor.add("/api/*.*","/api/lib/interceptor.lua") return true \ No newline at end of file