重新整理服务框架

This commit is contained in:
xx
2024-06-06 21:31:05 +08:00
parent fde71009f2
commit f9174cd028
48 changed files with 919 additions and 652 deletions

View File

@@ -26,7 +26,10 @@
#include <sol/state_view.hpp>
#include <sol/thread.hpp>
namespace fastweb
{
class app;
}
namespace sol {
class state : private std::unique_ptr<lua_State, detail::state_deleter>, public state_view {

View File

@@ -8,7 +8,7 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# 设置自定义配置类型
if(MSVC)
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Build config types" FORCE)
endif()
endif()
# C++等级
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)

19
src/core/Interface.hpp Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
#include "base/error.h"
namespace fastweb
{
class app;
class Interface :public ylib::error_base
{
public:
Interface(fastweb::app* ptr)
{
this->m_ptr = ptr;
}
fastweb::app* app() { return this->m_ptr; }
public:
fastweb::app* m_ptr = nullptr;
};
}

View File

@@ -1,4 +1,4 @@
#include "fastweb.h"
#include "app.h"
#include <regex>
#include "net/http_website.h"
#include "net/http_router.h"
@@ -9,20 +9,34 @@
#include "module/globalfuns.h"
#include "core/config.h"
#include "core/global.h"
#include "core/statemanager.h"
#include "core/subscribemanager.h"
#include "core/interceptormanager.h"
fastweb::fastweb()
fastweb::app::app()
{
log = std::make_shared<fastweb::log>(this);
subscribe = std::make_shared<fastweb::subscribe_manager>(this);
interceptor = std::make_shared<fastweb::interceptor_manager>(this);
state = std::make_shared<fastweb::state_manager>(this);
config = std::make_shared<fastweb::config>(this);
global = std::make_shared<fastweb::global>(this);
gloabl_module = std::make_shared<fastweb::global_module>(this);
}
bool fastweb::start()
fastweb::app::~app()
{
stop();
}
bool fastweb::app::start(const std::string& config_filepath)
{
stop();
// 虚拟机管理器
if(sStateMgr->start() == false)
// 初始化配置
if (config->open(config_filepath) == false)
{
m_lastErrorDesc = sStateMgr->last_error();
m_lastErrorDesc = "open config failed, " + config->last_error();
return false;
}
// 虚拟机管理器
if(state->start() == false)
{
m_lastErrorDesc = state->last_error();
return false;
}
@@ -30,7 +44,7 @@ bool fastweb::start()
network::http::start_config config;
network::http::website_config ws_config;
for_iter(iter, sConfig->domain)
for_iter(iter, this->config->domain)
{
network::http::host_config host_config;
host_config.domain = iter->first;
@@ -45,17 +59,17 @@ bool fastweb::start()
url = "http://";
url += host_config.domain;
url += ":" + std::to_string(host_config.port);
LOG_WARN("URL: "+url);
log->warn("URL: " + url, __FILE__, __func__, __LINE__);
}
ws_config.name = "master";
ws_config.router.threadpool.size = 40;
ws_config.router.threadpool.queuemax = 10000;
ws_config.session.dirpath = sConfig->website.session_dir;
ws_config.session.timeout_sec = sConfig->website.session_timeout_sec;
ws_config.session.dirpath = this->config->website.session_dir;
ws_config.session.timeout_sec = this->config->website.session_timeout_sec;
config.website.push_back(ws_config);
for_iter(iter, sConfig->domain)
for_iter(iter, this->config->domain)
{
if(iter->second.https)
config.cert.emplace(iter->first, iter->second.ssl);
@@ -73,22 +87,24 @@ bool fastweb::start()
// 初始化脚本
if (initialization_script() == false)
return false;
// 加载订阅
m_subscribe.load(router);
subscribe->load(router);
// 加载拦截器
m_interceptor.load(router);
interceptor->load(router);
return m_center->start();
}
void fastweb::stop()
void fastweb::app::stop()
{
m_subscribe.clear();
m_interceptor.clear();
this->m_lastErrorDesc.clear();
subscribe->clear();
interceptor->clear();
if (m_center != nullptr)
{
m_center->close();
@@ -96,16 +112,16 @@ void fastweb::stop()
}
m_center = nullptr;
global::getInstance()->clear();
global->clear();
if (m_state_init != nullptr)
delete m_state_init;
m_state_init = nullptr;
sStateMgr->close();
state->close();
}
bool fastweb::initialization_script()
bool fastweb::app::initialization_script()
{
auto script_filepath = sConfig->website.Initialization_script;
auto script_filepath = this->config->website.Initialization_script;
if (script_filepath == "")
return true;
if (ylib::file::exist(script_filepath) == false)
@@ -113,18 +129,23 @@ bool fastweb::initialization_script()
m_lastErrorDesc = "Initialization script not found, filepath: " + script_filepath;
return false;
}
auto state = sStateMgr->get();
auto state = this->state->get();
try
{
state->state->set_function("global_regist", module::global_regist);
auto result = state->state->script_file(script_filepath);
sol::load_result script = state->state->load_file(script_filepath);
if (!script.valid()) {
sol::error err = script;
throw ylib::exception(err.what());
}
sol::protected_function_result result = script();
if (!result.valid()) {
sol::error err = result;
throw ylib::exception(err.what());
}
if (result.get<bool>() == false)
{
throw ylib::exception("user interrupt");
throw ylib::exception("user interrupt: `return false`");
}
}
catch (const std::exception& e)

49
src/core/app.h Normal file
View File

@@ -0,0 +1,49 @@
#pragma once
#include "define.h"
#include "base/error.h"
#include "net/http_center.h"
#include "core/subscribemanager.h"
#include "core/interceptormanager.h"
#include "core/statemanager.h"
#include "core/config.h"
#include "core/log.h"
#include "core/global.h"
#include "core/global_module.h"
namespace fastweb
{
class app :public ylib::error_base {
public:
app();
~app();
bool start(const std::string& config_filepath);
void stop();
private:
/// <summary>
/// 初始化执行脚本
/// </summary>
/// <returns></returns>
bool initialization_script();
private:
// 初始化脚本虚拟机
luastate* m_state_init = nullptr;
// 网站服务核心
network::http::center* m_center = nullptr;
public:
// 日志
std::shared_ptr<fastweb::log> log;
// 订阅管理器
std::shared_ptr<fastweb::subscribe_manager> subscribe;
// 拦截器管理器
std::shared_ptr<fastweb::interceptor_manager> interceptor;
// LUA状态管理器
std::shared_ptr<fastweb::state_manager> state;
// 配置
std::shared_ptr<fastweb::config> config;
// 应用全局变量
std::shared_ptr<fastweb::global> global;
// 全局托管模块
std::shared_ptr<fastweb::global_module> gloabl_module;
};
}

View File

@@ -1,9 +1,10 @@
#include "config.h"
#include <regex>
config::config()
#include "core/app.h"
fastweb::config::config(fastweb::app* ptr):Interface(ptr)
{
}
bool config::open(const std::string& ini_filepath)
bool fastweb::config::open(const std::string& ini_filepath)
{
if (ylib::file::exist(ini_filepath) == false)
{
@@ -47,17 +48,17 @@ bool config::open(const std::string& ini_filepath)
cache();
return true;
}
std::vector<std::string> config::lua_lib_files()
std::vector<std::string> fastweb::config::lua_lib_files()
{
std::vector<std::string> results;
for (size_t i = 0; i < sConfig->scripts.lib_dir.size(); i++)
for (size_t i = 0; i < app()->config->scripts.lib_dir.size(); i++)
{
auto luas = ylib::file::traverse(sConfig->scripts.lib_dir[i], "(.*\\.lua)");
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 = sConfig->scripts.lib_dir[i]+"/"+ strutils::replace(iter->first, '\\', '/');
std::string path = app()->config->scripts.lib_dir[i]+"/"+ strutils::replace(iter->first, '\\', '/');
results.push_back(path);
}
}
@@ -69,7 +70,7 @@ std::vector<std::string> config::lua_lib_files()
return results;
}
std::vector<std::string> config::extractVariableNames(const std::string& text)
std::vector<std::string> fastweb::config::extractVariableNames(const std::string& text)
{
std::regex pattern("\\$\\{([^}]+)\\}"); // 使用捕获组提取中间的内容
std::vector<std::string> results;
@@ -83,7 +84,7 @@ std::vector<std::string> config::extractVariableNames(const std::string& text)
return results;
}
void config::cache()
void fastweb::config::cache()
{
scripts.lib_dir = ylib::json::from(m_ini.read("scripts", "lib_dir")).to<std::vector<std::string>>();

View File

@@ -1,69 +1,73 @@
#pragma once
#include "define.h"
#include "base/error.h"
#include "base/singleton.hpp"
#include "util/ini.h"
#include "net/http_define.h"
class config:public ylib::error_base,public ylib::singleton<config>{
public:
struct domain {
bool https = false;
ushort port = 0;
network::http::ssl_config ssl;
};
struct scripts {
std::vector<std::string> lib_dir;
std::string module_dir;
uint32 lua_cache_size = 0;
uint32 auto_update_sec = 0;
};
struct website {
struct __interceptor {
std::string filepath;
std::string regex_express;
namespace fastweb
{
class config :public Interface {
public:
struct domain {
bool https = false;
ushort port = 0;
network::http::ssl_config ssl;
};
struct scripts {
std::vector<std::string> lib_dir;
std::string module_dir;
uint32 lua_cache_size = 0;
uint32 auto_update_sec = 0;
};
struct website {
struct __interceptor {
std::string filepath;
std::string regex_express;
};
std::string dir;
std::string default_404;
std::vector<std::string> default_index;
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;
};
std::string dir;
std::string default_404;
std::vector<std::string> default_index;
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;
};
struct log {
bool enable = false;
std::string dir;
std::string name;
bool succ = false;
bool info = false;
bool warn = false;
bool error = false;
};
public:
config();
bool open(const std::string& ini_filepath);
struct log {
bool enable = false;
std::string dir;
std::string name;
bool succ = false;
bool info = false;
bool warn = false;
bool error = false;
};
public:
config(fastweb::app* ptr);
bool open(const std::string& ini_filepath);
std::vector<std::string> lua_lib_files();
private:
// INI配置文件
ylib::ini m_ini;
private:
/// <summary>
/// 查询所有变量字符串
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
std::vector<std::string> extractVariableNames(const std::string& text);
/// <summary>
/// 缓存配置
/// </summary>
void cache();
public:
scripts scripts;
website website;
log log;
std::map<std::string, domain> domain;
};
std::vector<std::string> lua_lib_files();
private:
// INI配置文件
ylib::ini m_ini;
private:
/// <summary>
/// 查询所有变量字符串
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
std::vector<std::string> extractVariableNames(const std::string& text);
/// <summary>
/// 缓存配置
/// </summary>
void cache();
public:
scripts scripts;
website website;
log log;
std::map<std::string, domain> domain;
};
}

View File

@@ -2,31 +2,23 @@
#include <functional>
#include <mutex>
#include "sol/sol.hpp"
#include "base/define.h"
#include "util/system.h"
#include "util/codec.h"
#include "util/strutils.h"
#include "util/file.h"
#include "utils/logutils.h"
#include <variant>
#define LOG_ERROR(MSG) LogUtils::getInstance()->error(MSG,__FILE__ ,__func__,__LINE__ )
#define LOG_WARN(MSG) LogUtils::getInstance()->warn(MSG,__FILE__ ,__func__,__LINE__ )
#define LOG_INFO(MSG) LogUtils::getInstance()->info(MSG,__FILE__ ,__func__,__LINE__ )
#define LOG_SUCC(MSG) LogUtils::getInstance()->success(MSG,__FILE__ ,__func__,__LINE__ )
#define LOG_DEBUG(MSG) LogUtils::getInstance()->debug(MSG,__FILE__ ,__func__,__LINE__ )
#include "core/interface.hpp"
#define sConfig ::config::getInstance()
#define sFastWeb ::fastweb::getInstance()
#define sStateMgr ::state_manager::getInstance()
#define LOG_ERROR(MSG) app()->log->error(MSG,__FILE__ ,__func__,__LINE__ )
#define LOG_WARN(MSG) app()->log->warn(MSG,__FILE__ ,__func__,__LINE__ )
#define LOG_INFO(MSG) app()->log->info(MSG,__FILE__ ,__func__,__LINE__ )
#define LOG_SUCC(MSG) app()->log->success(MSG,__FILE__ ,__func__,__LINE__ )
#define LOG_DEBUG(MSG) app()->log->debug(MSG,__FILE__ ,__func__,__LINE__ )
#define VarType sol::object
#define GET_APP \
sol::state_view lua(ts); \
fastweb::app* app = lua["____app"]

View File

@@ -1,5 +1,5 @@
#include <iostream>
#include "core/fastweb.h"
#include "core/app.h"
#include "util/system.h"
#include "core/define.h"
#include "core/config.h"
@@ -8,29 +8,26 @@ extern "C" {
#ifdef _WIN32
DLL_EXPORT
#endif
int fastweb_start(const char* config_filepath)
void* fastweb_start(const char* config_filepath)
{
std::cout << "=========== [fastweb engine] ============" << std::endl;
if (sConfig->open(config_filepath) == false)
fastweb::app* app = new fastweb::app();
if (app->start(config_filepath) == false)
{
LOG_ERROR("open config failed," + sConfig->last_error());
return -1;
app->log->error("fastweb start failed," + app->last_error(), __FILE__, __func__, __LINE__);
delete app;
return nullptr;
}
if (fastweb::getInstance()->start() == false)
{
LOG_ERROR("fastweb start failed," + fastweb::getInstance()->last_error());
return -1;
}
LOG_SUCC("success");
return 0;
app->log->success("success", __FILE__, __func__, __LINE__);
return app;
}
#ifdef _WIN32
DLL_EXPORT
#endif
void fastweb_close()
{
fastweb::getInstance()->stop();
}
void fastweb_close(void* app)
{
auto a = static_cast<fastweb::app*>(app);
a->stop();
delete a;
}
}

View File

@@ -4,7 +4,7 @@
#define DLL_EXPORT __attribute__((visibility("default")))
#endif
extern "C" {
DLL_EXPORT int fastweb_start(const char* config_filepath);
DLL_EXPORT void fastweb_close();
DLL_EXPORT void* fastweb_start(const char* config_filepath);
DLL_EXPORT void fastweb_close(void* app);
}

View File

@@ -1,28 +0,0 @@
#pragma once
#include "define.h"
#include "base/error.h"
#include "base/singleton.hpp"
#include "net/http_center.h"
#include "core/subscribemanager.h"
#include "core/interceptormanager.h"
class fastweb:public ylib::error_base,public ylib::singleton<fastweb> {
public:
fastweb();
bool start();
void stop();
private:
/// <summary>
/// 初始化执行脚本
/// </summary>
/// <returns></returns>
bool initialization_script();
private:
// 初始化脚本虚拟机
luastate* m_state_init = nullptr;
// 网站服务核心
network::http::center *m_center = nullptr;
// 订阅管理器
subscribe_manager m_subscribe;
// 拦截器管理器
interceptor_manager m_interceptor;
};

View File

@@ -1,28 +1,32 @@
#include "global.h"
#include "module/imodule.h"
global::global()
#include "module/basemodule.h"
fastweb::global::global(fastweb::app* app):Interface(app)
{
}
void global::regist_lua(sol::state* lua)
fastweb::global::~global()
{
m_value_ptr.lock();
for_iter(iter, (*m_value_ptr.parent()))
clear();
}
void fastweb::global::regist(sol::state* lua)
{
m_ptrs.lock();
for_iter(iter, (*m_ptrs.parent()))
{
auto im = static_cast<module::imodule*>(iter->second);
auto im = static_cast<module::base*>(iter->second);
im->regist_global(iter->first, lua);
}
m_value_ptr.unlock();
m_ptrs.unlock();
}
void* global::get_ptr(const std::string& name)
void* fastweb::global::get_ptr(const std::string& name)
{
void* result = nullptr;
m_value_ptr.get(name,result);
m_ptrs.get(name,result);
return result;
}
bool global::regist_ptr(const std::string& name, void* value, sol::this_state ts)
bool fastweb::global::set_ptr(const std::string& name, void* value, sol::this_state ts)
{
sol::state_view lua(ts);
// INIT中先注册一次防止被销毁
@@ -30,12 +34,12 @@ bool global::regist_ptr(const std::string& name, void* value, sol::this_state ts
lua.registry()[name] = this;
lua[name] = this;
}
return m_value_ptr.add(name, value);
return m_ptrs.add(name, value);
}
VarType global::get(const std::string& name, sol::this_state s)
sol::object fastweb::global::get_obj(const std::string& name, sol::this_state s)
{
VarType value;
sol::object value;
if (m_values.get(name, value))
{
return value;
@@ -43,22 +47,18 @@ VarType global::get(const std::string& name, sol::this_state s)
return sol::make_object(s, sol::nil);
}
void global::set(const std::string& name, VarType value)
void fastweb::global::set_obj(const std::string& name, sol::object value)
{
m_values.set(name, value, true);
}
void global::clear()
{
m_values.clear();
m_value_ptr.clear();
//m_value_ptr.lock();
//for_iter(iter, (*m_value_ptr.parent()))
void fastweb::global::clear()
{
//for_iter(iter, (*m_ptrs.parent()))
//{
// auto im = static_cast<module::imodule*>(iter->second);
// im->delete_global();
// //auto base = ((module::base*)iter->second);
// //base->delete_global();
//}
//m_value_ptr.unlock();
//m_value_ptr.clear();
m_ptrs.clear();
m_values.clear();
}

View File

@@ -1,25 +1,57 @@
#pragma once
#include "define.h"
#include "base/error.h"
#include "base/singleton.hpp"
#include "util/map.hpp"
class global :public ylib::error_base,public ylib::singleton<global>{
public:
global();
namespace fastweb
{
/// <summary>
/// 应用全局变量
/// </summary>
class global : public Interface{
public:
global(fastweb::app* app);
~global();
/// <summary>
/// 注册变量
/// </summary>
/// <param name="lua"></param>
void regist(sol::state* lua);
/// <summary>
/// 取指针
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
void* get_ptr(const std::string& name);
/// <summary>
/// 置指针
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="ts"></param>
/// <returns></returns>
bool set_ptr(const std::string& name, void* value, sol::this_state ts);
void regist_lua(sol::state* lua);
void* get_ptr(const std::string& name);
bool regist_ptr(const std::string& name,void* value, sol::this_state ts);
/// <summary>
/// 取对象
/// </summary>
/// <param name="name"></param>
/// <param name="s"></param>
/// <returns></returns>
sol::object get_obj(const std::string& name, sol::this_state s);
/// <summary>
/// 置对象
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
void set_obj(const std::string& name, sol::object value);
/// <summary>
/// 清理
/// </summary>
void clear();
private:
ylib::map<std::string, void*> m_ptrs;
ylib::map<std::string, sol::object> m_values;
};
}
VarType get(const std::string& name, sol::this_state s);
void set(const std::string& name,VarType value);
void clear();
private:
ylib::map<std::string, void*> m_value_ptr;
ylib::map<std::string,VarType> m_values;
};

View File

@@ -0,0 +1,27 @@
#include "global_module.h"
fastweb::global_module::global_module(fastweb::app* app):Interface(app)
{
}
fastweb::global_module::~global_module()
{
clear();
}
bool fastweb::global_module::regist(const std::string& name, module::base* module_ptr)
{
if (m_ptrs.find(name) != m_ptrs.end())
return false;
m_ptrs.emplace(name,module_ptr);
return true;
}
void fastweb::global_module::clear()
{
for_iter(iter,m_ptrs)
{
delete iter->second;
}
m_ptrs.clear();
}

32
src/core/global_module.h Normal file
View File

@@ -0,0 +1,32 @@
#pragma once
#include "define.h"
#include "base/error.h"
#include "util/map.hpp"
namespace module
{
class base;
}
namespace fastweb
{
/// <summary>
/// 全局托管模块(程序启动创建、程序销毁释放)
/// </summary>
class global_module : public Interface{
public:
global_module(fastweb::app* app);
~global_module();
/// <summary>
/// 创建
/// </summary>
/// <param name="module_ptr"></param>
bool regist(const std::string& name,module::base* module_ptr);
/// <summary>
/// 清理
/// </summary>
void clear();
private:
std::map<std::string, module::base*> m_ptrs;
};
}

View File

@@ -1,29 +1,31 @@
#include "core/interceptormanager.h"
#include "core/config.h"
#include "core/app.h"
#include "core/statemanager.h"
#include "module/http/request.h"
#include "module/http/response.h"
#include "net/http_interceptor.h"
std::map<std::string, std::string> interceptor_manager::interceptor = std::map<std::string,std::string>();
interceptor_manager::interceptor_manager()
fastweb::interceptor_manager::interceptor_manager(fastweb::app* app):Interface(app)
{
}
interceptor_manager::~interceptor_manager()
fastweb::interceptor_manager::~interceptor_manager()
{
clear();
}
void interceptor_manager::load(network::http::router* router)
void fastweb::interceptor_manager::load(network::http::router* router)
{
clear();
m_router = router;
for (size_t i = 0; i < sConfig->website.interceptor_scripts.size(); i++)
for (size_t i = 0; i < app()->config->website.interceptor_scripts.size(); i++)
{
interceptor_manager::interceptor.emplace(sConfig->website.interceptor_scripts[i].regex_express, sConfig->website.interceptor_scripts[i].filepath);
router->interceptor()->add(sConfig->website.interceptor_scripts[i].regex_express, &interceptor_manager::callback);
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);
});
}
}
void interceptor_manager::clear()
void fastweb::interceptor_manager::clear()
{
if(m_router != nullptr)
m_router->interceptor()->clear();
@@ -31,36 +33,38 @@ void interceptor_manager::clear()
m_router = nullptr;
}
bool interceptor_manager::callback(network::http::reqpack* reqpack, const std::string& express_string)
bool fastweb::interceptor_manager::callback(network::http::reqpack* reqpack, const std::string& express_string)
{
bool ok_continue = false;
auto lua = sStateMgr->get();
auto lua = app()->state->get();
std::string exception_string;
try
{
auto lbResult = lua->state->load_file(interceptor_manager::interceptor[express_string]);
if (lbResult.valid() == false)
{
sol::error err = lbResult;
throw ylib::exception("[interceptor] Failed to load script, " + std::string(err.what()));
sol::load_result script = lua->state->load_file(interceptor_manager::interceptor[express_string]);
if (!script.valid()) {
sol::error err = script;
throw ylib::exception(err.what());
}
module::request m_request(reqpack->request());
module::response m_response(reqpack->response());
(*lua->state)["response"] = m_response;
(*lua->state)["request"] = m_request;
auto result = lbResult();
sol::protected_function_result result = script();
if (!result.valid()) {
sol::error err = result;
throw ylib::exception(err.what());
}
ok_continue = result.get<bool>();
}
catch (const std::exception& e)
{
exception_string = e.what();
if (sConfig->website.debug)
if (app()->config->website.debug)
LOG_ERROR("[subscribe_interceptor][" + reqpack->request()->filepath() + "]: " + e.what());
}
lua->state->collect_garbage();
sStateMgr->push(lua);
app()->state->push(lua);
if (exception_string.empty() == false)
throw ylib::exception(exception_string);

View File

@@ -1,31 +1,34 @@
#pragma once
#include "base/singleton.hpp"
#include "core/structs.h"
#include "core/define.h"
#include "net/http_reqpack.h"
#include "net/http_request.h"
#include "net/http_response.h"
#include "net/http_router.h"
/// <summary>
/// 拦截器管理器
/// </summary>
class interceptor_manager{
public:
interceptor_manager();
~interceptor_manager();
void load(network::http::router* router);
void clear();
private:
namespace fastweb
{
/// <summary>
/// 服务回调
/// 拦截器管理器
/// </summary>
/// <param name="reqpack"></param>
/// <param name="express_string"></param>
static bool callback(network::http::reqpack* reqpack, const std::string& express_string);
private:
network::http::router* m_router = nullptr;
public:
static std::map<std::string, std::string> interceptor;
};
class interceptor_manager:public Interface {
public:
interceptor_manager(fastweb::app* app);
~interceptor_manager();
void load(network::http::router* router);
void clear();
private:
/// <summary>
/// 服务回调
/// </summary>
/// <param name="reqpack"></param>
/// <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;
};
}

View File

@@ -1,16 +1,16 @@
#include "logutils.h"
#include "log.h"
#include "util/print.h"
#include "util/time.h"
#include "util/codec.h"
#include "util/file.h"
#include "core/define.h"
#include "core/config.h"
#include "core/app.h"
#ifdef _WIN32
#include <Windows.h>
#endif
#define DEBUG_INFO 0
void LogUtils::print(const std::string& type,const std::string& msg,const std::string& filepath, const std::string& func, int line,int color,bool error) {
void fastweb::log::print(const std::string& type,const std::string& msg,const std::string& filepath, const std::string& func, int line,int color,bool error) {
std::string __now_time = time::now_time("%m-%d %H:%M:%S");
printf("[%s] ", __now_time.c_str());
@@ -26,7 +26,7 @@ void LogUtils::print(const std::string& type,const std::string& msg,const std::s
ylib::println(msg, (ylib::ConsoleTextColor)color);
#endif
if (sConfig->log.enable)
if (app()->config->log.enable)
{
std::string logcontent = __now_time + " " + type + " " + msg
#ifdef _WIN32
@@ -34,17 +34,17 @@ void LogUtils::print(const std::string& type,const std::string& msg,const std::s
#else
+ "\n";
#endif
std::string new_time = time::now_time(sConfig->log.name);
std::string new_time = time::now_time(app()->config->log.name);
if (new_time == "")
{
std::cout << "error: The log file name is incorrect: " << sConfig->log.name << std::endl;
std::cout << "error: The log file name is incorrect: " << app()->config->log.name << std::endl;
return;
}
if (m_current_name != new_time)
{
ylib::file::create_dir(sConfig->log.dir, true);
std::string newfilepath = sConfig->log.dir + "/" + new_time;
ylib::file::create_dir(app()->config->log.dir, true);
std::string newfilepath = app()->config->log.dir + "/" + new_time;
m_file.close();
if (m_file.open(newfilepath) == false)
{
@@ -55,14 +55,14 @@ void LogUtils::print(const std::string& type,const std::string& msg,const std::s
m_file.appead(logcontent);
}
}
LogUtils::LogUtils()
fastweb::log::log(fastweb::app* ptr):Interface(ptr)
{
}
LogUtils::~LogUtils()
fastweb::log::~log()
{
}
void LogUtils::success(const std::string& msg, const std::string& filepath, const std::string& func, int line)
void fastweb::log::success(const std::string& msg, const std::string& filepath, const std::string& func, int line)
{
//log_error();
this->print("[SUCC ] ", msg, filepath, func, line, ylib::ConsoleTextColor::GREEN
@@ -72,13 +72,13 @@ void LogUtils::success(const std::string& msg, const std::string& filepath, cons
,false);
}
void LogUtils::info(const std::string& msg, const std::string& filepath, const std::string& func, int line)
void fastweb::log::info(const std::string& msg, const std::string& filepath, const std::string& func, int line)
{
this->print("[INFO ] ", msg, filepath, func, line, ylib::ConsoleTextColor::GREEN | ylib::ConsoleTextColor::RED | ylib::ConsoleTextColor::BLUE, false);
}
void LogUtils::error(const std::string& msg, const std::string& filepath, const std::string& func, int line)
void fastweb::log::error(const std::string& msg, const std::string& filepath, const std::string& func, int line)
{
//#ifdef _DEBUG
this->print("[ERROR] ", msg, filepath, func, line, ylib::ConsoleTextColor::RED
@@ -91,7 +91,7 @@ void LogUtils::error(const std::string& msg, const std::string& filepath, const
//#endif
}
void LogUtils::warn(const std::string& msg, const std::string& filepath, const std::string& func, int line)
void fastweb::log::warn(const std::string& msg, const std::string& filepath, const std::string& func, int line)
{
this->print("[WARN ] ", msg, filepath, func, line, ylib::ConsoleTextColor::YELLOW
#ifdef _WIN32
@@ -100,7 +100,7 @@ void LogUtils::warn(const std::string& msg, const std::string& filepath, const s
, false);
}
void LogUtils::debug(const std::string& msg, const std::string& filepath, const std::string& func,int line)
void fastweb::log::debug(const std::string& msg, const std::string& filepath, const std::string& func,int line)
{
this->print("[DEBUG] ", msg, filepath, func, line, ylib::ConsoleTextColor::BLUE
#ifdef _WIN32
@@ -108,3 +108,8 @@ void LogUtils::debug(const std::string& msg, const std::string& filepath, const
#endif
, false);
}
void fastweb::log::lua(const std::string& msg)
{
this->print("[LUA ] ", msg,"","",0, ylib::ConsoleTextColor::GREEN | ylib::ConsoleTextColor::RED | ylib::ConsoleTextColor::BLUE, false);
}

26
src/core/log.h Normal file
View File

@@ -0,0 +1,26 @@
#pragma once
#include <string>
#include "util/file.h"
#include "core/define.h"
namespace fastweb
{
class log :public Interface {
public:
log(fastweb::app* ptr);
~log();
void success(const std::string& msg, const std::string& filepath, const std::string& func, int line);
void info(const std::string& msg, const std::string& filepath, const std::string& func, int line);
void error(const std::string& msg, const std::string& filepath, const std::string& func, int line);
void warn(const std::string& msg, const std::string& filepath, const std::string& func, int line);
void debug(const std::string& msg, const std::string& filepath, const std::string& func, int line);
void lua(const std::string& msg);
private:
void print(const std::string& type, const std::string& msg, const std::string& filepath, const std::string& func, int line, int color, bool error);
private:
// 当前文件名
std::string m_current_name;
// 文件
ylib::file_io m_file;
};
}

View File

@@ -1,17 +1,18 @@
#include "lualibdetecter.h"
#include "core/config.h"
#include "core/app.h"
#include "util/file.h"
lualib_detecter::lualib_detecter()
fastweb::lualib_detecter::lualib_detecter(fastweb::app* app):Interface(app)
{
}
lualib_detecter::~lualib_detecter()
fastweb::lualib_detecter::~lualib_detecter()
{
}
bool lualib_detecter::changed()
bool fastweb::lualib_detecter::changed()
{
auto lib_files = sConfig->lua_lib_files();
auto lib_files = app()->config->lua_lib_files();
bool changed = false;
if (lib_files.size() == m_files.size())
{

View File

@@ -4,25 +4,27 @@
#include "sol/sol.hpp"
#include "base/error.h"
#include "base/singleton.hpp"
#include "util/thread.h"
#include "util/queue.hpp"
#include "util/map.hpp"
#include "core/structs.h"
#include "core/define.h"
/// <summary>
/// LUALIB库变动检测
/// </summary>
class lualib_detecter {
public:
lualib_detecter();
~lualib_detecter();
namespace fastweb
{
/// <summary>
/// 是否变化
/// LUALIB库变动检测
/// </summary>
/// <returns></returns>
bool changed();
private:
std::map<std::string, timestamp> m_files;
};
class lualib_detecter:public Interface {
public:
lualib_detecter(fastweb::app* app);
~lualib_detecter();
/// <summary>
/// 是否变化
/// </summary>
/// <returns></returns>
bool changed();
private:
std::map<std::string, timestamp> m_files;
};
}

View File

@@ -1,7 +1,7 @@
#include "modulemanager.h"
#include "util/file.h"
#include "core/app.h"
#include "core/config.h"
#include "core/global.h"
#ifdef _WIN32
@@ -23,22 +23,25 @@
#include "module/mutex.h"
#include "module/codec.h"
#include "module/time.h"
#include "module/file.h"
#include "module/filesystem.h"
#include "module/sys.h"
#include "module/timer.h"
#include "module/process.h"
#include "module/ini.h"
module_manager::module_manager()
fastweb::module_manager::module_manager(fastweb::app* app):Interface(app)
{
}
void module_manager::start()
fastweb::module_manager::~module_manager()
{
}
void fastweb::module_manager::start()
{
close();
auto ms = modules();
for (size_t i = 0; i < ms.size(); i++)
{
module_info mi;
std::string mod_filepath = sConfig->scripts.module_dir + "/" + ms[i];
std::string mod_filepath = app()->config->scripts.module_dir + "/" + ms[i];
#ifdef _WIN32
mi.dll = LoadLibrary(mod_filepath.c_str());
if (mi.dll == nullptr)
@@ -65,7 +68,7 @@ void module_manager::start()
}
}
void module_manager::close()
void fastweb::module_manager::close()
{
for_iter(iter, m_modules)
{
@@ -77,25 +80,25 @@ void module_manager::close()
m_modules.clear();
}
void module_manager::load(sol::state* lua)
void fastweb::module_manager::load(sol::state* lua)
{
load_core(lua);
load_lualib(lua);
load_3rdparty(lua);
}
std::string module_manager::search(const std::string& filepath)
std::string fastweb::module_manager::search(const std::string& filepath)
{
for (size_t i = 0; i < sConfig->scripts.lib_dir.size(); i++)
for (size_t i = 0; i < app()->config->scripts.lib_dir.size(); i++)
{
std::string path = sConfig->scripts.lib_dir[i] + "/" + filepath;
std::string path = app()->config->scripts.lib_dir[i] + "/" + filepath;
if (ylib::file::exist(path))
return path;
}
return "";
}
void module_manager::load_core(sol::state* lua)
void fastweb::module_manager::load_core(sol::state* lua)
{
lua->open_libraries(
sol::lib::base,
@@ -122,23 +125,23 @@ void module_manager::load_core(sol::state* lua)
#ifdef _WIN32
module::mssql::regist(lua);
#endif
module::regist_globalfuns(lua);
module::globalfuncs::regist(lua);
module::local_storage::regist(lua);
module::mutex::regist(lua);
module::auto_lock::regist(lua);
module::codec::regist(lua);
module::time::regist(lua);
module::file::regist(lua);
module::filesystem::regist(lua);
module::sys::regist(lua);
module::timer::regist(lua);
module::ini::regist(lua);
module::process::regist(lua);
global::getInstance()->regist_lua(lua);
app()->global->regist(lua);
}
void module_manager::load_3rdparty(sol::state* lua)
void fastweb::module_manager::load_3rdparty(sol::state* lua)
{
for_iter(iter, m_modules)
{
@@ -149,20 +152,20 @@ void module_manager::load_3rdparty(sol::state* lua)
}
}
void module_manager::load_lualib(sol::state* lua)
void fastweb::module_manager::load_lualib(sol::state* lua)
{
// 获取当前的package.path添加新的搜索路径
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"; // 添加新的路径
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; // 设置修改后的路径
}
std::vector<std::string> module_manager::modules()
std::vector<std::string> fastweb::module_manager::modules()
{
std::vector<std::string> results;
auto luas = ylib::file::traverse(sConfig->scripts.module_dir, "(.*\\.dll)");
auto luas = ylib::file::traverse(app()->config->scripts.module_dir, "(.*\\.dll)");
for_iter(iter, luas)
{
if (iter->second == IS_DIRECTORY)

View File

@@ -2,52 +2,57 @@
#include <map>
#include "sol/sol.hpp"
#include "core/structs.h"
#include "base/singleton.hpp"
#include "core/define.h"
typedef int (*fastweb_module_regist)(void*, void*);
struct module_info {
void* dll = nullptr;
fastweb_module_regist func = nullptr;
};
namespace fastweb
{
/// <summary>
/// 模块管理器
/// </summary>
class module_manager :public Interface {
public:
module_manager(fastweb::app* app);
~module_manager();
/// <summary>
/// 模块管理器
/// </summary>
class module_manager :public ylib::error_base, public ylib::singleton<module_manager> {
public:
module_manager();
void start();
void close();
/// <summary>
/// 加载虚拟机库
/// </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>
/// 加载核心库
/// </summary>
/// <param name="lua"></param>
void load_core(sol::state* lua);
/// <summary>
/// 加载三方库
/// </summary>
void load_3rdparty(sol::state* lua);
/// <summary>
/// 加载LUA库
/// </summary>
/// <param name="lua"></param>
void load_lualib(sol::state* lua);
/// <summary>
/// 取模块文件列表
/// </summary>
/// <returns></returns>
std::vector<std::string> modules();
private:
std::map<std::string, module_info> m_modules;
};
void start();
void close();
/// <summary>
/// 加载虚拟机库
/// </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>
/// 加载核心库
/// </summary>
/// <param name="lua"></param>
void load_core(sol::state* lua);
/// <summary>
/// 加载三方库
/// </summary>
void load_3rdparty(sol::state* lua);
/// <summary>
/// 加载LUA库
/// </summary>
/// <param name="lua"></param>
void load_lualib(sol::state* lua);
/// <summary>
/// 取模块文件列表
/// </summary>
/// <returns></returns>
std::vector<std::string> modules();
private:
// DLL模块
std::map<std::string, module_info> m_modules;
};
}

View File

@@ -7,36 +7,47 @@
#include "core/define.h"
#include "core/config.h"
#include "core/global.h"
#include "core/app.h"
#define LOOP_STATE_USE 1
bool state_manager::start()
fastweb::state_manager::state_manager(fastweb::app* app):Interface(app)
{
lib_detecter = std::make_shared<fastweb::lualib_detecter>(app);
module_manager = std::make_shared<fastweb::module_manager>(app);
}
fastweb::state_manager::~state_manager()
{
}
bool fastweb::state_manager::start()
{
close();
::ithread::start();
m_module_manager.start();
module_manager->start();
return true;
}
void state_manager::close()
void fastweb::state_manager::close()
{
::ithread::stop();
::ithread::wait();
luastate* state = nullptr;
while (m_states.pop(state))
delete state;
m_module_manager.close();
module_manager->close();
}
luastate* state_manager::create()
luastate* fastweb::state_manager::create()
{
luastate* lua = new luastate();
lua->flag = m_flag;
(*lua->state)["____app"] = app();
// 加载库或模块
m_module_manager.load(lua->state);
module_manager->load(lua->state);
return lua;
}
luastate* state_manager::get()
luastate* fastweb::state_manager::get()
{
luastate* result = nullptr;
while (m_states.pop(result))
@@ -49,7 +60,7 @@ luastate* state_manager::get()
return create();
}
void state_manager::push(luastate* state)
void fastweb::state_manager::push(luastate* state)
{
if (state == nullptr)
return;
@@ -62,11 +73,11 @@ void state_manager::push(luastate* state)
}
bool state_manager::run()
bool fastweb::state_manager::run()
{
if (m_lib_detecter.changed())
if (lib_detecter->changed())
m_flag++;
system::sleep_msec(sConfig->scripts.auto_update_sec*1000);
system::sleep_msec(app()->config->scripts.auto_update_sec * 1000);
return true;
}

View File

@@ -4,7 +4,6 @@
#include "sol/sol.hpp"
#include "base/error.h"
#include "base/singleton.hpp"
#include "util/thread.h"
#include "util/queue.hpp"
#include "util/map.hpp"
@@ -12,47 +11,52 @@
#include "core/structs.h"
#include "core/lualibdetecter.h"
#include "core/modulemanager.h"
/// <summary>
/// LUA状态管理器
/// </summary>
class state_manager:public ylib::singleton<state_manager>,private ylib::ithread,public ylib::error_base {
public:
state_manager() = default;
namespace fastweb
{
/// <summary>
/// 启动
/// LUA状态管理器
/// </summary>
bool start();
void close();
/// <summary>
/// 取虚拟机
/// </summary>
/// <returns></returns>
luastate* get();
/// <summary>
/// 归还虚拟机
/// </summary>
/// <param name="state"></param>
void push(luastate* state);
private:
// 虚拟机
ylib::queue<luastate*> m_states;
// 版本FLAT
size_t m_flag = 0;
// LIB变化检测
lualib_detecter m_lib_detecter;
// 模块管理器
module_manager m_module_manager;
private:
// 通过 ithread 继承
bool run() override;
/// <summary>
/// 创建虚拟机
/// </summary>
/// <returns></returns>
luastate* create();
class state_manager :private ylib::ithread,public Interface {
public:
state_manager(fastweb::app* app);
~state_manager();
/// <summary>
/// 启动
/// </summary>
bool start();
void close();
/// <summary>
/// 取虚拟机
/// </summary>
/// <returns></returns>
luastate* get();
/// <summary>
/// 归还虚拟机
/// </summary>
/// <param name="state"></param>
void push(luastate* state);
public:
// 模块管理器
std::shared_ptr<fastweb::module_manager> module_manager;
private:
// 虚拟机
ylib::queue<luastate*> m_states;
// 版本FLAT
size_t m_flag = 0;
// LIB变化检测
std::shared_ptr<fastweb::lualib_detecter> lib_detecter;
private:
// 通过 ithread 继承
bool run() override;
/// <summary>
/// 创建虚拟机
/// </summary>
/// <returns></returns>
luastate* create();
};
};
}

View File

@@ -1,23 +1,26 @@
#include "subscribemanager.h"
#include "core/config.h"
#include "core/app.h"
#include "core/statemanager.h"
#include "module/http/request.h"
#include "module/http/response.h"
subscribe_manager::subscribe_manager()
fastweb::subscribe_manager::subscribe_manager(fastweb::app* ptr):Interface(ptr)
{
}
subscribe_manager::~subscribe_manager()
fastweb::subscribe_manager::~subscribe_manager()
{
clear();
}
void subscribe_manager::load(network::http::router* router)
void fastweb::subscribe_manager::load(network::http::router* router)
{
clear();
m_router = router;
router->other(&subscribe_manager::other);
router->other([&](network::http::request* request, network::http::response* response) {
this->other(request,response);
});
}
void subscribe_manager::clear()
void fastweb::subscribe_manager::clear()
{
if (m_router != nullptr)
{
@@ -28,11 +31,11 @@ void subscribe_manager::clear()
m_subextra.clear();
m_router = nullptr;
}
void subscribe_manager::other(network::http::request* request, network::http::response* response)
void fastweb::subscribe_manager::other(network::http::request* request, network::http::response* response)
{
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)
auto send_404 = [&](network::http::response* response) {
std::string default_404 = app()->config->website.dir + "\\" + app()->config->website.default_404;
if (app()->config->website.default_404 == "" || ylib::file::exist(default_404) == false)
{
response->send((std::string)"404 Not Found", 404, "Not Found");
}
@@ -61,22 +64,22 @@ void subscribe_manager::other(network::http::request* request, network::http::re
std::string filepath;
if (strutils::right(request->filepath(),1) == "/")
{
for (size_t i = 0; i < sConfig->website.default_index.size(); i++)
for (size_t i = 0; i < app()->config->website.default_index.size(); i++)
{
filepath = sConfig->website.dir + request->filepath() + sConfig->website.default_index[i];
filepath = app()->config->website.dir + request->filepath() + app()->config->website.default_index[i];
if (ylib::file::exist(filepath))
break;
}
}
else
filepath = sConfig->website.dir + request->filepath();
filepath = app()->config->website.dir + request->filepath();
send_file(response, filepath);
}
void subscribe_manager::exec(const std::string& filepath, network::http::request* request, network::http::response* response)
void fastweb::subscribe_manager::exec(const std::string& filepath, network::http::request* request, network::http::response* response)
{
auto lua = sStateMgr->get();
auto lua = app()->state->get();
std::string exception_string;
try
{
@@ -96,12 +99,12 @@ void subscribe_manager::exec(const std::string& filepath, network::http::request
catch (const std::exception& e)
{
exception_string = e.what();
if (sConfig->website.debug)
if (app()->config->website.debug)
LOG_ERROR("[subscribe_service][" + request->filepath() + "]: " + e.what());
}
// 清理
lua->state->collect_garbage();
sStateMgr->push(lua);
app()->state->push(lua);
if (exception_string.empty() == false)
throw ylib::exception(exception_string);

View File

@@ -2,37 +2,40 @@
#include "sol/sol.hpp"
#include "base/singleton.hpp"
#include "core/structs.h"
#include "core/define.h"
#include "net/http_request.h"
#include "net/http_response.h"
#include "net/http_router.h"
/// <summary>
/// 订阅管理器
/// </summary>
class subscribe_manager{
public:
subscribe_manager();
~subscribe_manager();
namespace fastweb
{
/// <summary>
/// 订阅管理器
/// </summary>
class subscribe_manager:public Interface {
public:
subscribe_manager(fastweb::app* ptr);
~subscribe_manager();
void load(network::http::router* router);
void clear();
private:
/// <summary>
/// 其它
/// </summary>
/// <param name="request"></param>
/// <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;
};
void load(network::http::router* router);
void clear();
private:
/// <summary>
/// 其它
/// </summary>
/// <param name="request"></param>
/// <param name="response"></param>
/// <param name="extra"></param>
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>
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

@@ -7,15 +7,18 @@ namespace module
/// <summary>
/// 模块继承接口
/// </summary>
class imodule {
class base {
public:
virtual ~imodule() {};
virtual ~base() {};
/// <summary>
/// 注册全局变量
/// 注册全局变量(global类管理)
/// </summary>
/// <param name="name"></param>
/// <param name="lua"></param>
virtual void regist_global(const std::string& name,sol::state* lua) = 0;
/// <summary>
/// 释放全局变量(global类管理)
/// </summary>
virtual void delete_global() = 0;
/// <summary>
/// 全局变量阶段获取自身指针
@@ -25,3 +28,4 @@ namespace module
};
}

View File

@@ -1,13 +1,13 @@
#pragma once
#include "sol/sol.hpp"
#include "imodule.h"
#include "basemodule.h"
namespace module
{
/// <summary>
/// 编解码
/// </summary>
class codec:public module::imodule {
class codec{
public:
/// <summary>
/// URL解码

View File

@@ -1,90 +0,0 @@
#include "file.h"
#include "util/file.h"
sol::table module::file::list(const std::string& dirpath, const std::string& regex, sol::this_state s)
{
auto map = ylib::file::traverse(dirpath, regex);
sol::state_view lua(s);
sol::table result_table = lua.create_table();
int row_num = 1;
for_iter(iter, map)
{
sol::table row = lua.create_table();
row["file"] = iter->first;
row["type"] = (int)iter->second;
result_table[row_num++] = row;
}
return result_table;
}
void module::file::copy_dir(const std::string& src_dir, const std::string& dst_dir)
{
ylib::file::copy_dir(src_dir, dst_dir);
}
bool module::file::create_dir(const std::string& dirpath)
{
return ylib::file::create_dir(dirpath,true);
}
sol::object module::file::read(const std::string& filepath, sol::this_state s)
{
sol::object result;
ylib::buffer data;
if (ylib::file::read(filepath, data) == false)
result = sol::make_object(s,data.to_string());
else
result = sol::make_object(s, sol::nil);
return result;
}
bool module::file::write(const std::string& filepath, const std::string& data)
{
return ylib::file::write(filepath, data);
}
bool module::file::remove(const std::string& filepath)
{
return ylib::file::remove(filepath);
}
bool module::file::remove_dir(const std::string& dirpath, bool recycle)
{
return ylib::file::remove_dir(dirpath,recycle);
}
bool module::file::exist(const std::string& filepath)
{
return ylib::file::exist(filepath);
}
bool module::file::exist_dir(const std::string& dirpath)
{
return ylib::file::exist_dir(dirpath);
}
int64 module::file::size(const std::string& filepath)
{
return ylib::file::size(filepath);
}
bool module::file::copy(const std::string& src, const std::string& dst)
{
return ylib::file::copy(src,dst);
}
timestamp module::file::last_write_time(const std::string& filepath)
{
return ylib::file::last_write_time(filepath);
}
void module::file::regist(sol::state* lua)
{
(*lua)["IS_FILE"] = (int)ylib::FileType::IS_FILE;
(*lua)["IS_DIRECTORY"] = (int)ylib::FileType::IS_DIRECTORY;
lua->new_usertype<module::file>("fs",
"list", &module::file::list,
"copy_dir", &module::file::copy_dir,
"create_dir", &module::file::create_dir,
"read",module::file::read,
"write",module::file::write,
"remove",module::file::remove,
"remove_dir",module::file::remove_dir,
"exist",module::file::exist,
"exist_dir",module::file::exist_dir,
"size",module::file::size,
"copy",module::file::copy,
"last_write_time",module::file::last_write_time
);
}

90
src/module/filesystem.cpp Normal file
View File

@@ -0,0 +1,90 @@
#include "filesystem.h"
#include "util/file.h"
sol::table module::filesystem::list(const std::string& dirpath, const std::string& regex, sol::this_state s)
{
auto map = ylib::file::traverse(dirpath, regex);
sol::state_view lua(s);
sol::table result_table = lua.create_table();
int row_num = 1;
for_iter(iter, map)
{
sol::table row = lua.create_table();
row["file"] = iter->first;
row["type"] = (int)iter->second;
result_table[row_num++] = row;
}
return result_table;
}
void module::filesystem::copy_dir(const std::string& src_dir, const std::string& dst_dir)
{
ylib::file::copy_dir(src_dir, dst_dir);
}
bool module::filesystem::create_dir(const std::string& dirpath)
{
return ylib::file::create_dir(dirpath,true);
}
sol::object module::filesystem::read(const std::string& filepath, sol::this_state s)
{
sol::object result;
ylib::buffer data;
if (ylib::file::read(filepath, data) == false)
result = sol::make_object(s,data.to_string());
else
result = sol::make_object(s, sol::nil);
return result;
}
bool module::filesystem::write(const std::string& filepath, const std::string& data)
{
return ylib::file::write(filepath, data);
}
bool module::filesystem::remove(const std::string& filepath)
{
return ylib::file::remove(filepath);
}
bool module::filesystem::remove_dir(const std::string& dirpath, bool recycle)
{
return ylib::file::remove_dir(dirpath,recycle);
}
bool module::filesystem::exist(const std::string& filepath)
{
return ylib::file::exist(filepath);
}
bool module::filesystem::exist_dir(const std::string& dirpath)
{
return ylib::file::exist_dir(dirpath);
}
int64 module::filesystem::size(const std::string& filepath)
{
return ylib::file::size(filepath);
}
bool module::filesystem::copy(const std::string& src, const std::string& dst)
{
return ylib::file::copy(src,dst);
}
timestamp module::filesystem::last_write_time(const std::string& filepath)
{
return ylib::file::last_write_time(filepath);
}
void module::filesystem::regist(sol::state* lua)
{
(*lua)["IS_FILE"] = (int)ylib::FileType::IS_FILE;
(*lua)["IS_DIRECTORY"] = (int)ylib::FileType::IS_DIRECTORY;
lua->new_usertype<module::filesystem>("fs",
"list", &module::filesystem::list,
"copy_dir", &module::filesystem::copy_dir,
"create_dir", &module::filesystem::create_dir,
"read",module::filesystem::read,
"write",module::filesystem::write,
"remove",module::filesystem::remove,
"remove_dir",module::filesystem::remove_dir,
"exist",module::filesystem::exist,
"exist_dir",module::filesystem::exist_dir,
"size",module::filesystem::size,
"copy",module::filesystem::copy,
"last_write_time",module::filesystem::last_write_time
);
}

View File

@@ -1,13 +1,12 @@
#pragma once
#include "sol/sol.hpp"
#include "imodule.h"
#include "core/define.h"
namespace module
{
/// <summary>
/// 文件
/// </summary>
class file:public module::imodule {
class filesystem {
public:
static sol::table list(const std::string& dirpath,const std::string& regex,sol::this_state s);
static void copy_dir(const std::string& src_dir,const std::string& dst_dir);

View File

@@ -3,41 +3,65 @@
#include "util/codec.h"
#include "util/time.h"
#include "core/global.h"
#include "core/app.h"
static ylib::counter<uint64> s_counter_guid;
void module::regist_globalfuns(sol::state* lua)
void module::globalfuncs::regist(sol::state* lua)
{
lua->set_function("global_get", module::global_get);
lua->set_function("global_set", module::global_set);
lua->set_function("make_software_guid", module::make_software_guid);
lua->set_function("throw_string", module::throw_string);
lua->set_function("set_ptr", module::globalfuncs::set_ptr);
lua->set_function("get_obj", module::globalfuncs::get_obj);
lua->set_function("set_obj", module::globalfuncs::set_obj);
lua->set_function("make_software_guid", module::globalfuncs::make_software_guid);
lua->set_function("throw_string", module::globalfuncs::throw_string);
lua->set_function("print", module::globalfuncs::print);
}
std::string module::make_software_guid()
std::string module::globalfuncs::make_software_guid()
{
return codec::md5(std::to_string(time::now_msec()) + std::to_string(s_counter_guid.make()));
}
bool module::global_regist(const std::string& name, void* ptr, sol::this_state ts)
void module::globalfuncs::print(sol::variadic_args args, sol::this_state ts)
{
return global::getInstance()->regist_ptr(name,ptr, ts);
GET_APP;
std::ostringstream oss;
for (auto arg : args) {
if (arg.get_type() == sol::type::string) {
oss << arg.as<std::string>();
}
else if (arg.get_type() == sol::type::number) {
oss << arg.as<double>(); // 可以处理整数和浮点数
}
else if (arg.get_type() == sol::type::boolean) {
oss << (arg.as<bool>() ? "true" : "false");
}
else if (arg.get_type() == sol::type::nil) {
oss << "nil";
}
else {
oss << "unsupported type";
}
oss << " "; // 添加一个空格分隔符
}
app->log->lua(oss.str());
}
bool module::globalfuncs::set_ptr(const std::string& name, void* ptr, sol::this_state ts)
{
GET_APP;
return app->global->set_ptr(name,ptr, ts);
}
void module::global_set(const std::string& name, VarType value)
void module::globalfuncs::set_obj(const std::string& name, sol::object value, sol::this_state ts)
{
global::getInstance()->set(name,value);
GET_APP;
return app->global->set_obj(name,value);
}
VarType module::global_get(const std::string& name, sol::this_state s)
sol::object module::globalfuncs::get_obj(const std::string& name, sol::this_state ts)
{
return global::getInstance()->get(name,s);
GET_APP;
return app->global->get_obj(name, ts);
}
#if 0
void* module::global_get(const std::string& name)
{
return global::getInstance()->get_ptr(name);
}
#endif
void module::throw_string(const std::string& msg)
void module::globalfuncs::throw_string(const std::string& msg)
{
throw ylib::exception(msg);
}

View File

@@ -5,24 +5,49 @@
/// </summary>
namespace module
{
void regist_globalfuns(sol::state* lua);
/// <summary>
/// 生成软件唯一GUID
/// </summary>
/// <returns></returns>
std::string make_software_guid();
/// <summary>
/// 注册全局指针(仅允许初始化lua使用)
/// </summary>
/// <returns></returns>
bool global_regist(const std::string& name, void* ptr, sol::this_state ts);
class globalfuncs {
public:
/// <summary>
/// 生成软件唯一GUID
/// </summary>
/// <returns></returns>
static std::string make_software_guid();
/// <summary>
/// 接管打印
/// </summary>
/// <param name="args"></param>
static void print(sol::variadic_args args, sol::this_state ts);
/// <summary>
/// 置全局指针
/// </summary>
/// <param name="name"></param>
/// <param name="ptr"></param>
/// <param name="ts"></param>
/// <returns></returns>
static bool set_ptr(const std::string& name, void* ptr, sol::this_state ts);
void global_set(const std::string& name, VarType value);
VarType global_get(const std::string& name, sol::this_state s);
/// <summary>
/// 抛出异常
/// </summary>
/// <param name="msg"></param>
void throw_string(const std::string& msg);
/// <summary>
/// 置全局对象
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
static void set_obj(const std::string& name, sol::object value, sol::this_state ts);
/// <summary>
/// 取全局对象
/// </summary>
/// <param name="name"></param>
/// <param name="s"></param>
/// <returns></returns>
static sol::object get_obj(const std::string& name, sol::this_state s);
/// <summary>
/// 抛出异常
/// </summary>
/// <param name="msg"></param>
static void throw_string(const std::string& msg);
static void regist(sol::state* lua);
};
}

View File

@@ -88,7 +88,7 @@ std::string module::request::host()
{
return m_request->host();
}
VarType module::request::param(const std::string& name, bool throw_,sol::this_state s)
sol::object module::request::param(const std::string& name, bool throw_,sol::this_state s)
{
std::string value;
bool result = request_param(name, value);

View File

@@ -17,7 +17,7 @@ namespace module
network::http::method method();
std::string filepath();
std::string host();
VarType param(const std::string& name,bool throw_,sol::this_state s);
sol::object param(const std::string& name,bool throw_,sol::this_state s);
std::string remote_ipaddress();
ushort remote_port();

View File

@@ -1,12 +1,12 @@
#pragma once
#include "util/localstorage.h"
#include "imodule.h"
#include "basemodule.h"
namespace module
{
/// <summary>
/// 本地缓存(leveldb)
/// </summary>
class local_storage : public ylib::local_storage,public module::imodule {
class local_storage : public ylib::local_storage,public module::base {
public:
local_storage();
~local_storage() override;

View File

@@ -1,6 +1,7 @@
#ifdef _WIN32
#include "mssql.h"
#include "soci/odbc/soci-odbc.h"
#include "core/define.h"
module::mssql::mssql(const std::string& connstring)
{
m_session = std::make_shared<soci::session>(soci::odbc, connstring);

View File

@@ -1,8 +1,8 @@
#pragma once
#ifdef _WIN32
#include "sol/sol.hpp"
#include "imodule.h"
#include "soci/soci.h"
#include "core/define.h"
namespace module
{
/// <summary>

View File

@@ -1,13 +1,13 @@
#pragma once
#include "sol/sol.hpp"
#include "imodule.h"
#include "module/basemodule.h"
namespace module
{
/// <summary>
/// 互斥锁
/// </summary>
class mutex:public module::imodule {
class mutex:public module::base {
public:
mutex();
~mutex() override;

View File

@@ -534,7 +534,7 @@ bool module::mysql_result::next()
return m_result->next();
}
VarType module::mysql_result::get(sol::object obj, sol::this_state s)
sol::object module::mysql_result::get(sol::object obj, sol::this_state s)
{
std::string type;
if (obj.get_type() == sol::type::string)

View File

@@ -2,7 +2,7 @@
#include "db/mysql.h"
#include "db/sqler.h"
#include "sol/sol.hpp"
#include "imodule.h"
#include "module/basemodule.h"
namespace module
{
/// <summary>
@@ -50,7 +50,7 @@ namespace module
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
VarType get(sol::object obj, sol::this_state s);
sol::object get(sol::object obj, sol::this_state s);
/// <summary>
/// 取结果集到table
/// </summary>
@@ -151,7 +151,7 @@ namespace module
/// <summary>
/// MYSQL连接池
/// </summary>
class mysql :public imodule {
class mysql :public module::base {
public:
mysql();
~mysql() override;

View File

@@ -1,13 +1,12 @@
#pragma once
#include "sol/sol.hpp"
#include "imodule.h"
#include "core/define.h"
namespace module
{
/// <summary>
/// 系统
/// </summary>
class sys:public module::imodule {
class sys {
public:
static std::string current_dir();
static void sleep_msec(uint32 msec);

View File

@@ -1,13 +1,12 @@
#pragma once
#include "sol/sol.hpp"
#include "imodule.h"
#include "core/define.h"
namespace module
{
/// <summary>
/// 时间
/// </summary>
class time:public module::imodule {
class time {
public:
static uint64 now_msec();
static uint64 now_sec();

View File

@@ -3,35 +3,41 @@
#include "util/system.h"
#include "util/file.h"
#include "core/config.h"
#include "core/app.h"
#include "core/statemanager.h"
module::timer::~timer()
{
::ithread::stop();
::ithread::wait();
}
std::string module::timer::add(const std::string& name, const std::string& filepath, const std::string& funname, int msec, bool loop)
std::string module::timer::add(const std::string& name, const std::string& filepath, const std::string& funname, int msec, bool loop, sol::this_state ts)
{
GET_APP;
std::string filepath2;
if (ylib::file::exist(sConfig->website.dir + "/" + filepath))
filepath2 = sConfig->website.dir + "/" + filepath;
if (ylib::file::exist(app->config->website.dir + "/" + filepath))
filepath2 = app->config->website.dir + "/" + filepath;
else
{
filepath2 = module_manager::getInstance()->search(filepath);
filepath2 = app->state->module_manager->search(filepath);
if (filepath2.empty())
{
std::string result;
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");
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;
}
}
std::unique_lock<std::mutex> uni(module::timer::getInstance()->m_mutex);
std::unique_lock<std::mutex> uni(m_mutex);
m_app = app;
auto iter = module::timer::getInstance()->m_list.find(name);
if (iter != module::timer::getInstance()->m_list.end())
auto iter = m_list.find(name);
if (iter != m_list.end())
return "exist name `" + name + "`";
timer_info ti;
@@ -42,24 +48,29 @@ std::string module::timer::add(const std::string& name, const std::string& filep
ti.msec = msec;
ti.exec_msec = time::now_msec() + msec;
module::timer::getInstance()->m_list.emplace(name, ti);
module::timer::getInstance()->m_insert = true;
m_list.emplace(name, ti);
m_insert = true;
return "";
}
void module::timer::remove(const std::string& name)
void module::timer::remove(const std::string& name, sol::this_state ts)
{
module::timer::getInstance()->m_removed.push(name);
//std::unique_lock<std::mutex> uni(module::timer::getInstance()->m_mutex);
//module::timer::getInstance()->m_list.erase(name);
m_removed.push(name);
}
void module::timer::regist(sol::state* lua)
{
lua->new_usertype<module::timer>("timer",
"new", sol::constructors<module::timer()>(),
"add", &module::timer::add,
"remove", &module::timer::remove
"remove", &module::timer::remove,
"self", &module::timer::self
);
}
void module::timer::regist_global(const std::string& name, sol::state* lua)
{
lua->registry()[name] = this;
(*lua)[name] = this;
}
module::timer::timer()
{
::ithread::start();
@@ -67,7 +78,11 @@ module::timer::timer()
bool module::timer::run()
{
if (m_app == nullptr)
{
system::sleep_msec(100);
return true;
}
auto comp_wait_msec = [&]()->timestamp {
timestamp wait_msec = 0;
auto now_msec = time::now_msec();
@@ -90,7 +105,7 @@ bool module::timer::run()
// 加载LUA文件
if (ti.lua == nullptr)
{
ti.lua = sStateMgr->get();
ti.lua = m_app->state->get();
auto lbResult = ti.lua->state->load_file(ti.filepath);
if (lbResult.valid() == false)
{
@@ -117,8 +132,8 @@ bool module::timer::run()
catch (const std::exception& e)
{
exception_string = e.what();
if (sConfig->website.debug)
LOG_ERROR("[timer][" + ti.filepath + "]: " + e.what());
if (m_app->config->website.debug)
m_app->log->error("[timer][" + ti.filepath + "]: " + e.what(), __FILE__, __func__, __LINE__);
return false;
}
return false;
@@ -127,11 +142,11 @@ bool module::timer::run()
auto wait_msec = comp_wait_msec();
for (size_t i = 0; i < wait_msec/10; i++)
{
if (module::timer::getInstance()->m_insert)
if (m_insert || m_state == 1)
break;
system::sleep_msec(10);
}
module::timer::getInstance()->m_insert = false;
m_insert = false;
std::unique_lock<std::mutex> uni(m_mutex);
auto now_msec = time::now_msec();
@@ -166,7 +181,7 @@ bool module::timer::run()
if (iter != m_list.end())
{
iter->second.lua->state->collect_garbage();
sStateMgr->push(iter->second.lua);
m_app->state->push(iter->second.lua);
m_list.erase(iter);
}
}

View File

@@ -1,10 +1,9 @@
#pragma once
#include "sol/sol.hpp"
#include "imodule.h"
#include "core/structs.h"
#include "base/singleton.hpp"
#include "util/thread.h"
#include "util/queue.hpp"
#include "module/basemodule.h"
#include <map>
#include <mutex>
namespace module
@@ -22,7 +21,7 @@ namespace module
/// <summary>
/// 定时器
/// </summary>
class timer:public ylib::singleton<timer>,private ylib::ithread {
class timer:private ylib::ithread,public module::base {
public:
timer();
~timer();
@@ -35,19 +34,20 @@ namespace module
/// <param name="msec"></param>
/// <param name="loop"></param>
/// <returns></returns>
static std::string add(const std::string& name,const std::string& filepath,const std::string& funname,int msec,bool loop);
std::string add(const std::string& name,const std::string& filepath,const std::string& funname,int msec,bool loop, sol::this_state ts);
/// <summary>
/// 移除
/// </summary>
/// <param name="name"></param>
static void remove(const std::string& name);
void remove(const std::string& name, sol::this_state ts);
static void regist(sol::state* lua);
virtual void regist_global(const std::string& name, sol::state* lua) override;
virtual void delete_global() { delete this; }
private:
virtual bool run();
public:
fastweb::app* m_app = nullptr;
std::map<std::string,timer_info> m_list;
ylib::queue<std::string> m_removed;
std::mutex m_mutex;

View File

@@ -1,22 +0,0 @@
#pragma once
#include <string>
#include "base/singleton.hpp"
#include "util/file.h"
class LogUtils :public ylib::singleton<LogUtils> {
public:
LogUtils();
~LogUtils();
void success(const std::string& msg, const std::string& filepath, const std::string& func, int line);
void info(const std::string& msg, const std::string& filepath, const std::string& func, int line);
void error(const std::string& msg, const std::string& filepath, const std::string& func, int line);
void warn(const std::string& msg, const std::string& filepath, const std::string& func, int line);
void debug(const std::string& msg, const std::string& filepath, const std::string& func, int line);
private:
void print(const std::string& type, const std::string& msg, const std::string& filepath, const std::string& func, int line, int color, bool error);
private:
// 当前文件名
std::string m_current_name;
// 文件
ylib::file_io m_file;
};

View File

@@ -3,13 +3,16 @@
#include "core/entry.h"
#include <filesystem>
std::string config_filepath;
bool start()
void* fastweb_app_ptr = nullptr;
void start()
{
return fastweb_start(config_filepath.c_str()) == 0;
fastweb_app_ptr = fastweb_start(config_filepath.c_str());
}
void close()
{
fastweb_close();
if(fastweb_app_ptr == nullptr)
fastweb_close(fastweb_app_ptr);
fastweb_app_ptr = nullptr;
}
int main()
{
@@ -23,7 +26,8 @@ int main()
std::cout << "closing..." << std::endl;
close();
std::cout << "starting..." << std::endl;
if (start() == false)
start();
if (fastweb_app_ptr == nullptr)
{
std::cin.get();
return -1;