删除lua lib_dir参数,简化开发配置

This commit is contained in:
xx
2024-06-09 23:37:40 +08:00
parent 2944522053
commit e9e080fbb8
8 changed files with 14 additions and 50 deletions

View File

@@ -5,14 +5,8 @@ base=${current_dir}
www=${current_dir}/www
[scripts]
; LUA库目录
lib_dir=["${www}/api/lib"]
; 模块目录
module_dir=${base}/module
; LUA虚拟机缓存数量(并发越高越大)-建议10
lua_cache_size=3000
; 自动检测文件修改时间(秒)
auto_update_sec=3
[website]
; 网站静态文件目录

View File

@@ -118,6 +118,9 @@ bool fastweb::app::initialization_script()
auto script_filepath = this->config->website.Initialization_script;
if (script_filepath == "")
return true;
if (script_filepath[0] != '/')
script_filepath = "/" + script_filepath;
script_filepath = config->website.dir + script_filepath;
if (ylib::file::exist(script_filepath) == false)
{
m_lastErrorDesc = "Initialization script not found, filepath: " + script_filepath;

View File

@@ -51,16 +51,13 @@ bool fastweb::config::open(const std::string& ini_filepath)
std::vector<std::string> fastweb::config::lua_lib_files()
{
std::vector<std::string> results;
for (size_t i = 0; i < app()->config->scripts.lib_dir.size(); i++)
auto luas = ylib::file::traverse(app()->config->website.dir, "(.*\\.lua)");
for_iter(iter, luas)
{
auto luas = ylib::file::traverse(app()->config->scripts.lib_dir[i], "(.*\\.lua)");
for_iter(iter, luas)
{
if (iter->second == IS_DIRECTORY)
continue;
std::string path = app()->config->scripts.lib_dir[i]+"/"+ strutils::replace(iter->first, '\\', '/');
results.push_back(path);
}
if (iter->second == IS_DIRECTORY)
continue;
std::string path = app()->config->website.dir +"/"+ strutils::replace(iter->first, '\\', '/');
results.push_back(path);
}
// 去重
std::sort(results.begin(), results.end());
@@ -87,7 +84,6 @@ std::vector<std::string> fastweb::config::extractVariableNames(const std::string
void fastweb::config::cache()
{
scripts.lib_dir = ylib::json::from(m_ini.read("scripts", "lib_dir")).to<std::vector<std::string>>();
scripts.module_dir = m_ini.read("scripts", "module_dir");
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"));

View File

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

View File

@@ -84,18 +84,6 @@ void fastweb::module_manager::load(sol::state* lua)
load_lualib(lua);
load_3rdparty(lua);
}
std::string fastweb::module_manager::search(const std::string& filepath)
{
for (size_t i = 0; i < app()->config->scripts.lib_dir.size(); i++)
{
std::string path = app()->config->scripts.lib_dir[i] + "/" + filepath;
if (ylib::file::exist(path))
return path;
}
return "";
}
void fastweb::module_manager::load_core(sol::state* lua)
{
lua->open_libraries(
@@ -151,8 +139,8 @@ void fastweb::module_manager::load_lualib(sol::state* lua)
{
// 获取当前的package.path添加新的搜索路径
std::string current_path = (*lua)["package"]["path"]; // 获取当前的路径
for (size_t i = 0; i < app()->config->scripts.lib_dir.size(); i++)
current_path += ";" + app()->config->scripts.lib_dir[i] + "/?.lua"; // 添加新的路径
//for (size_t i = 0; i < app()->config->scripts.lib_dir.size(); i++)
// current_path += ";" + app()->config->scripts.lib_dir[i] + "/?.lua"; // 添加新的路径
current_path += ";" + app()->config->website.dir + "/?.lua"; // 添加新的路径
(*lua)["package"]["path"] = current_path; // 设置修改后的路径
}

View File

@@ -25,12 +25,6 @@ namespace fastweb
/// </summary>
/// <returns></returns>
void load(sol::state* lua);
/// <summary>
/// 搜索LUA
/// </summary>
/// <param name="filepath"></param>
/// <returns></returns>
std::string search(const std::string& filepath);
private:
/// <summary>
/// 加载核心库

View File

@@ -77,7 +77,7 @@ bool fastweb::state_manager::run()
{
if (lib_detecter->changed())
m_flag++;
system::sleep_msec(app()->config->scripts.auto_update_sec * 1000);
system::sleep_msec(3000);
return true;
}

View File

@@ -18,18 +18,7 @@ std::string module::timer::add(const std::string& name, const std::string& filep
filepath2 = app->config->website.dir + "/" + filepath;
else
{
filepath2 = app->state->module_manager->search(filepath);
if (filepath2.empty())
{
std::string result;
result = "not found script lua, Root: " + std::string(app->config->website.dir + "/" + filepath) + "\r\n Lib: ";
for (size_t i = 0; i < app->config->scripts.lib_dir.size(); i++)
result.append(std::string(app->config->scripts.lib_dir[i] + "/" + filepath)+"\r\n");
return result;
}
return "not found script lua: " + std::string(app->config->website.dir + "/" + filepath);
}
std::unique_lock<std::mutex> uni(m_mutex);