订阅模式改为直链访问模式和静态文件共享根目录

This commit is contained in:
xx
2024-06-05 15:34:16 +08:00
parent 6169348dee
commit 247780a33d
18 changed files with 155 additions and 198 deletions

View File

@@ -47,20 +47,6 @@ bool config::open(const std::string& ini_filepath)
cache();
return true;
}
std::vector<std::string> config::lua_app_files()
{
std::vector<std::string> results;
auto luas = ylib::file::traverse(sConfig->scripts.app_dir, "(.*\\.lua)");
for_iter(iter, luas)
{
if (iter->second == IS_DIRECTORY)
continue;
std::string path = strutils::replace(iter->first, '\\', '/');
results.push_back(path);
}
return results;
}
std::vector<std::string> config::lua_lib_files()
{
std::vector<std::string> results;
@@ -100,14 +86,12 @@ std::vector<std::string> config::extractVariableNames(const std::string& text)
void config::cache()
{
scripts.app_dir = m_ini.read("scripts","app_dir");
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.app_mapping_dir = m_ini.read("scripts", "app_mapping_dir");
scripts.auto_update_sec = ylib::stoi(m_ini.read("scripts", "auto_update_sec"));
website.static_dir = m_ini.read("website","static_dir");
website.dir = m_ini.read("website","dir");
website.default_404 = m_ini.read("website", "default_404");
website.default_index = ylib::json::from(m_ini.read("website", "default_index")).to<std::vector<std::string>>();
website.session_dir = m_ini.read("website", "session_dir");

View File

@@ -12,11 +12,9 @@ public:
network::http::ssl_config ssl;
};
struct scripts {
std::string app_dir;
std::vector<std::string> lib_dir;
std::string module_dir;
uint32 lua_cache_size = 0;
std::string app_mapping_dir;
uint32 auto_update_sec = 0;
};
struct website {
@@ -24,7 +22,7 @@ public:
std::string filepath;
std::string regex_express;
};
std::string static_dir;
std::string dir;
std::string default_404;
std::vector<std::string> default_index;
std::string session_dir;
@@ -38,7 +36,6 @@ public:
config();
bool open(const std::string& ini_filepath);
std::vector<std::string> lua_app_files();
std::vector<std::string> lua_lib_files();
private:
// INI配置文件

View File

@@ -155,6 +155,7 @@ void module_manager::load_lualib(sol::state* lua)
std::string current_path = (*lua)["package"]["path"]; // 获取当前的路径
for (size_t i = 0; i < sConfig->scripts.lib_dir.size(); i++)
current_path += ";" + sConfig->scripts.lib_dir[i] + "/?.lua"; // 添加新的路径
current_path += ";" + sConfig->website.dir + "/?.lua"; // 添加新的路径
(*lua)["package"]["path"] = current_path; // 设置修改后的路径
}

View File

@@ -14,13 +14,6 @@ void subscribe_manager::load(network::http::router* router)
{
clear();
m_router = router;
// 初始化全部订阅
auto files = sConfig->lua_app_files();
for (size_t i = 0; i < files.size(); i++)
{
init_subscribe(files[i]);
}
// 其它绑定
router->other(&subscribe_manager::other);
}
@@ -35,83 +28,59 @@ void subscribe_manager::clear()
m_subextra.clear();
m_router = nullptr;
}
void subscribe_manager::init_subscribe(const std::string& filepath)
void subscribe_manager::other(network::http::request* request, network::http::response* response)
{
auto state = sStateMgr->get();
std::string route_pattern;
network::http::method method = network::http::ALL;
try
{
auto result = state->state->script_file(sConfig->scripts.app_dir + "/" + filepath);
if (result.valid()) {
auto router = (*state->state)["route"];
auto type = router.get_type();
if (router.is<sol::table>())
{
sol::optional<std::string> route_pattern_param = router[1];
sol::optional<int> method_param = router[2];
if (route_pattern_param && route_pattern_param->empty() == false)
route_pattern = *route_pattern_param;
if (method_param)
method = (network::http::method)*method_param;
}
}
}
catch (const std::exception& e)
{
LOG_ERROR(e.what());
}
sStateMgr->push(state);
if (route_pattern.empty())
route_pattern = sConfig->scripts.app_mapping_dir + filepath;
// OutPutLog
{
std::string log;
log = "[subscribe] lua: " + filepath + "\t pattern: " + route_pattern + "\t method: ";
switch (method)
auto send_404 = [](network::http::response* response) {
std::string default_404 = sConfig->website.dir + "\\" + sConfig->website.default_404;
if (sConfig->website.default_404 == "" || ylib::file::exist(default_404) == false)
{
case ylib::network::http::GET:
log.append("GET");
break;
case ylib::network::http::POST:
log.append("POST");
break;
case ylib::network::http::PUT:
log.append("PUT");
break;
case ylib::network::http::DEL:
log.append("DEL");
break;
case ylib::network::http::HEAD:
log.append("HEAD");
break;
case ylib::network::http::ALL:
log.append("ALL");
break;
default:
break;
response->send((std::string)"404 Not Found", 404, "Not Found");
}
else
{
response->send_file(default_404, -1, 404, "Not Found");
}
};
auto send_file = [&](network::http::response* response, std::string filepath)
{
if (ylib::file::exist(filepath))
{
if (ylib::file::ext(filepath) == "lua")
{
exec(filepath,request,response);
return;
}
response->send_file(filepath);
}
else
{
send_404(response);
}
};
std::string filepath;
if (request->filepath() == "/")
{
for (size_t i = 0; i < sConfig->website.default_index.size(); i++)
{
filepath = sConfig->website.dir + request->filepath() + sConfig->website.default_index[i];
if (ylib::file::exist(filepath))
break;
}
LOG_INFO(log);
}
std::string* extra = new std::string(sConfig->scripts.app_dir + "/" + filepath);
m_subextra.push_back(extra);
m_router->subscribe(route_pattern, method, &subscribe_manager::callback, extra);
else
filepath = sConfig->website.dir + request->filepath();
send_file(response, filepath);
}
void subscribe_manager::callback(network::http::request* request, network::http::response* response, void* extra)
void subscribe_manager::exec(const std::string& filepath, network::http::request* request, network::http::response* response)
{
std::string lua_filepath = *(std::string*)extra;
auto lua = sStateMgr->get();
std::string exception_string;
try
{
auto lbResult = lua->state->load_file(lua_filepath);
auto lbResult = lua->state->load_file(filepath);
if (lbResult.valid() == false)
{
sol::error err = lbResult;
@@ -120,16 +89,9 @@ void subscribe_manager::callback(network::http::request* request, network::http:
module::request m_request(request);
module::response m_response(response);
lbResult();
(*lua->state)["response"] = &m_response;
(*lua->state)["request"] = &m_request;
auto result = (*lua->state)["access"]();
if (!result.valid()) {
sol::error err = result;
throw ylib::exception(err.what());
}
lbResult();
}
catch (const std::exception& e)
{
@@ -144,51 +106,3 @@ void subscribe_manager::callback(network::http::request* request, network::http:
if (exception_string.empty() == false)
throw ylib::exception(exception_string);
}
void subscribe_manager::other(network::http::request* request, network::http::response* response)
{
auto send_404 = [](network::http::response* response) {
std::string default_404 = sConfig->website.static_dir + "\\" + sConfig->website.default_404;
if (sConfig->website.default_404 == "" || ylib::file::exist(default_404) == false)
{
response->send((std::string)"404 Not Found", 404, "Not Found");
}
else
{
response->send_file(default_404, -1, 404, "Not Found");
}
};
auto send_file = [&](network::http::response* response, std::string filepath)
{
filepath = sConfig->website.static_dir + filepath;
if (ylib::file::exist(filepath))
{
response->send_file(filepath);
}
else
{
send_404(response);
}
};
if (request->filepath() == "/")
{
bool find = false;
for (size_t i = 0; i < sConfig->website.default_index.size(); i++)
{
std::string filepath = sConfig->website.static_dir + request->filepath() + sConfig->website.default_index[i];
if (ylib::file::exist(filepath))
{
find = true;
response->send_file(filepath);
break;
}
}
if (find == false)
{
send_404(response);
}
return;
}
send_file(response, request->filepath());
}

View File

@@ -18,19 +18,6 @@ public:
void load(network::http::router* router);
void clear();
private:
/// <summary>
/// 初始化订阅
/// </summary>
/// <param name="filepath"></param>
/// <returns></returns>
void init_subscribe(const std::string& filepath);
private:
/// <summary>
/// 服务回调
/// </summary>
/// <param name="request"></param>
/// <param name="response"></param>
static void callback(network::http::request* request, network::http::response* response, void* extra);
/// <summary>
/// 其它
/// </summary>
@@ -38,6 +25,13 @@ private:
/// <param name="response"></param>
/// <param name="extra"></param>
static void other(network::http::request* request, network::http::response* response);
/// <summary>
/// 执行lua
/// </summary>
/// <param name="filepath"></param>
/// <param name="request"></param>
/// <param name="response"></param>
static void exec(const std::string& filepath,network::http::request* request, network::http::response* response);
private:
network::http::router* m_router = nullptr;
std::vector<std::string*> m_subextra;

View File

@@ -12,15 +12,15 @@ module::timer::~timer()
std::string module::timer::add(const std::string& name, const std::string& filepath, const std::string& funname, int msec, bool loop)
{
std::string filepath2;
if (ylib::file::exist(sConfig->scripts.app_dir + "/" + filepath))
filepath2 = sConfig->scripts.app_dir + "/" + filepath;
if (ylib::file::exist(sConfig->website.dir + "/" + filepath))
filepath2 = sConfig->website.dir + "/" + filepath;
else
{
filepath2 = module_manager::getInstance()->search(filepath);
if (filepath2.empty())
{
std::string result;
result = "not found script lua, App: " + std::string(sConfig->scripts.app_dir + "/" + filepath) + "\r\n Lib: ";
result = "not found script lua, Root: " + std::string(sConfig->website.dir + "/" + filepath) + "\r\n Lib: ";
for (size_t i = 0; i < sConfig->scripts.lib_dir.size(); i++)
result.append(std::string(sConfig->scripts.lib_dir[i] + "/" + filepath)+"\r\n");
return result;