ソースを参照

支持多模块加载路径

你的用户名 3 週間 前
コミット
0c4d19c869
6 ファイル変更62 行追加36 行削除
  1. 2 2
      CMakeLists.txt
  2. 1 1
      config.ini
  3. 11 1
      src/core/config.cpp
  4. 1 1
      src/core/config.h
  5. 33 27
      src/core/modulemanager.cpp
  6. 14 4
      tests/fastweb.cpp

+ 2 - 2
CMakeLists.txt

@@ -104,7 +104,7 @@ else()
 	link_directories(/usr/local/lib)
 	target_link_libraries(${FASTWEBCORE} 
 			hpsocket
-			ylib_d
+			ylib
 			leveldb
 			crypto
 			/opt/lua54/lib/liblua.a
@@ -145,7 +145,7 @@ else()
 	target_link_libraries(${PROJECT_NAME} 
 			${FASTWEBCORE}
 			hpsocket
-			ylib_d
+			ylib
 			pthread
 			mysqlcppconn
 			ssl

+ 1 - 1
config.ini

@@ -5,7 +5,7 @@ base=${config_dir}
 www=${config_dir}/www
 
 [scripts]
-; 模块目录
+; 模块目录,多个路径用逗号分隔
 module_dir=${base}/module
 
 [website]

+ 11 - 1
src/core/config.cpp

@@ -65,7 +65,17 @@ ylib::json fastweb::config::read_runtime()
 void fastweb::config::cache()
 {
 
-	scripts.module_dir = PATH_EX(m_ini.read("scripts", "module_dir"));
+	{
+		auto dirs = strutils::split(m_ini.read("scripts", "module_dir"), ',');
+		scripts.module_dir.clear();
+		for (auto& d : dirs)
+		{
+			d = strutils::trim(d, { ' ', '\t' });
+			if (d.empty())
+				continue;
+			scripts.module_dir.push_back(PATH_EX(d));
+		}
+	}
 	scripts.lua_cache_size = ylib::stoi(m_ini.read("scripts", "lua_cache_size"));
 	scripts.auto_update_sec = ylib::stoi(m_ini.read("scripts", "auto_update_sec"));
 

+ 1 - 1
src/core/config.h

@@ -15,7 +15,7 @@ namespace fastweb
 		};
 		struct scripts {
 			//std::vector<std::string> lib_dir;
-			std::string module_dir;
+			std::vector<std::string> module_dir;
 			uint32 lua_cache_size = 0;
 			uint32 auto_update_sec = 0;
 		};

+ 33 - 27
src/core/modulemanager.cpp

@@ -130,27 +130,31 @@ void fastweb::module_manager::start()
 		{
 			
 			m_lua_include_path += ";" + app()->config->website.dir + "/?.lua";
-			m_lua_include_path += ";" + app()->config->scripts.module_dir + "/?.lua";
-
 			m_lua_include_cpath += ";" + app()->config->website.dir + "/?." + DLL_EXT;
-			m_lua_include_cpath += ";" + app()->config->scripts.module_dir + "/?." + DLL_EXT;
+			for (const auto& module_dir : app()->config->scripts.module_dir)
+			{
+				m_lua_include_path += ";" + module_dir + "/?.lua";
+				m_lua_include_cpath += ";" + module_dir + "/?." + DLL_EXT;
+			}
 		}
 		// FastWeb管理器
 		{
-			std::string search_path = app()->config->scripts.module_dir + "/.install";
-
-			std::map<std::string, bool> dirs;
-			ylib::file::list(search_path, dirs);
-			for_iter(iter, dirs)
+			for (const auto& module_dir : app()->config->scripts.module_dir)
 			{
-				if (iter->second == false)
-					continue;
-				if (iter->first == "." || iter->first == "..")
-					continue;
-				m_lua_include_path += ";" + search_path + "/" + iter->first + "/?.lua";
-				m_lua_include_cpath += ";" + search_path + "/" + iter->first + "/?." + DLL_EXT;
+				std::string search_path = module_dir + "/.install";
+
+				std::map<std::string, bool> dirs;
+				ylib::file::list(search_path, dirs);
+				for_iter(iter, dirs)
+				{
+					if (iter->second == false)
+						continue;
+					if (iter->first == "." || iter->first == "..")
+						continue;
+					m_lua_include_path += ";" + search_path + "/" + iter->first + "/?.lua";
+					m_lua_include_cpath += ";" + search_path + "/" + iter->first + "/?." + DLL_EXT;
+				}
 			}
-			
 		}
 	}
 }
@@ -257,20 +261,22 @@ std::vector<std::string> fastweb::module_manager::modules()
 		}
 		};
 
-	std::string search_path = app()->config->scripts.module_dir + "/.install";
-
-	insert_dll_path(app()->config->scripts.module_dir);
-
-	std::map<std::string, bool> dirs;
-	ylib::file::list(search_path, dirs);
-	for_iter(iter, dirs)
+	for (const auto& module_dir : app()->config->scripts.module_dir)
 	{
-		if (iter->second == false)
-			continue;
-		if (iter->first == "." || iter->first == "..")
-			continue;
+		insert_dll_path(module_dir);
+
+		std::string search_path = module_dir + "/.install";
+		std::map<std::string, bool> dirs;
+		ylib::file::list(search_path, dirs);
+		for_iter(iter, dirs)
+		{
+			if (iter->second == false)
+				continue;
+			if (iter->first == "." || iter->first == "..")
+				continue;
 
-		insert_dll_path(search_path+"/"+iter->first);
+			insert_dll_path(search_path+"/"+iter->first);
+		}
 	}
 
 //	auto luas = ylib::file::traverse(app()->config->scripts.module_dir, "(.*\\.dll)");

+ 14 - 4
tests/fastweb.cpp

@@ -26,7 +26,17 @@ If you have any questions, please contact us: 1585346868@qq.com Or visit our web
 #include "core/entry.h"
 #include "zip.h"
 
-
+static std::string first_module_dir(ylib::ini& ini)
+{
+    auto dirs = strutils::split(ini.read("scripts", "module_dir"), ',');
+    for (auto& d : dirs)
+    {
+        d = strutils::trim(d, { ' ', '\t' });
+        if (!d.empty())
+            return d;
+    }
+    return "";
+}
 
 bool exsit_install_software(std::string name){
     std::string command = "which " + name + " > /dev/null 2>&1";
@@ -241,7 +251,7 @@ void fastweb::uninstall_module(std::string ini_filepath, std::string name)
         return;
     }
 
-    std::string module_dirpath = m_ini.read("scripts","module_dir")+"/.install/"+info.id;
+    std::string module_dirpath = first_module_dir(m_ini)+"/.install/"+info.id;
     if(ylib::file::exist_dir(module_dirpath))
     {
         if(ylib::file::remove_dir(module_dirpath) == false)
@@ -380,7 +390,7 @@ void fastweb::install_module_linux(fastweb::module_info info)
             ylib:file::write(zip_filepath,client.response());
 
             
-            std::string new_module_dirpath = m_ini.read("scripts","module_dir")+"/.install/"+info.id;
+            std::string new_module_dirpath = first_module_dir(m_ini)+"/.install/"+info.id;
             ylib::file::create_dir(new_module_dirpath,true);
             std::string cmd = "unzip -o -q "+zip_filepath+" -d "+new_module_dirpath;
             
@@ -423,7 +433,7 @@ void fastweb::install_module_linux(fastweb::module_info info)
                 return;
             }
             
-            std::string new_module_dirpath = m_ini.read("scripts","module_dir")+"/.install/"+info.id;
+            std::string new_module_dirpath = first_module_dir(m_ini)+"/.install/"+info.id;
             ylib::file::create_dir(new_module_dirpath,true);
             ylib::file::copy_dir(tdir+"/target",new_module_dirpath);
             std::cout<<"安装成功"<<std::endl;