Procházet zdrojové kódy

删除配置文件拦截器改为代码控制

xx před 2 roky
rodič
revize
1106e51cd1

+ 1 - 0
.gitignore

@@ -9,3 +9,4 @@
 /obj
 /bin
 
+/log

+ 0 - 2
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
 ; 绑定域名

+ 3 - 5
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();

+ 2 - 0
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<fastweb::log> log;
 		// 订阅管理器

+ 0 - 15
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<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++)

+ 0 - 1
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<std::string> domain;
 		};

+ 8 - 6
src/core/entry.cpp

@@ -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;
+	}
 }
 

+ 21 - 13
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);

+ 5 - 4
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
 {
 	/// <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;
 	};
 }

+ 2 - 0
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);

+ 44 - 0
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<module::interceptor>("interceptor",
+		"add", &module::interceptor::add,
+		"remove", &module::interceptor::remove,
+		"exist", &module::interceptor::exist,
+		"clear", &module::interceptor::clear
+	);
+}

+ 39 - 0
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();
+        /// <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);
+    };
+}
+

+ 2 - 1
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