From 790c9aea0d0d90bb2b451b4a4497f93eabd0c234 Mon Sep 17 00:00:00 2001 From: xx Date: Wed, 29 May 2024 21:07:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=98=AF=E5=90=A6=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E9=A2=84=E7=BC=96=E8=AF=91=E5=AD=97=E8=8A=82=E7=A0=81?= =?UTF-8?q?=E5=AE=8F=EF=BC=8C=E4=BC=98=E5=8C=96=E9=83=A8=E5=88=86=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/bytecodemanager.cpp | 5 +- src/core/bytecodemanager.h | 6 ++- src/core/define.h | 5 +- src/core/fastweb.cpp | 92 ++++++++++++++++++++++++++++++++++-- src/core/fastweb.h | 4 ++ src/core/statemanager.cpp | 3 +- src/module/file.cpp | 29 ++++++++++++ src/module/file.h | 17 +++++++ src/module/http/request.cpp | 7 ++- src/module/http/request.h | 1 + src/module/mssql.cpp | 45 ++++++++++++++---- src/module/mssql.h | 1 + src/module/mysql.cpp | 36 ++++++++++++-- src/module/mysql.h | 7 +-- 14 files changed, 233 insertions(+), 25 deletions(-) create mode 100644 src/module/file.cpp create mode 100644 src/module/file.h diff --git a/src/core/bytecodemanager.cpp b/src/core/bytecodemanager.cpp index caa79ef..4963f31 100644 --- a/src/core/bytecodemanager.cpp +++ b/src/core/bytecodemanager.cpp @@ -1,4 +1,6 @@ -#include "bytecodemanager.h" + +#include "bytecodemanager.h" +#if ENABLE_BYTECODE == 1 #include "fastweb.h" #include "utils/luautils.h" #include "util/file.h" @@ -75,3 +77,4 @@ bool bytecode_manager::run() return true; } +#endif \ No newline at end of file diff --git a/src/core/bytecodemanager.h b/src/core/bytecodemanager.h index 89f5a00..8b8e3dd 100644 --- a/src/core/bytecodemanager.h +++ b/src/core/bytecodemanager.h @@ -1,6 +1,7 @@ #pragma once - +#include "core/define.h" +#if ENABLE_BYTECODE == 1 #include "sol/sol.hpp" #include "base/error.h" @@ -40,4 +41,5 @@ private: // 通过 ithread 继承 bool run() override; -}; \ No newline at end of file +}; +#endif \ No newline at end of file diff --git a/src/core/define.h b/src/core/define.h index 714c06e..5804420 100644 --- a/src/core/define.h +++ b/src/core/define.h @@ -27,4 +27,7 @@ -#define VarType sol::object \ No newline at end of file +#define VarType sol::object + + +#define ENABLE_BYTECODE 0 \ No newline at end of file diff --git a/src/core/fastweb.cpp b/src/core/fastweb.cpp index 0acad29..df08af5 100644 --- a/src/core/fastweb.cpp +++ b/src/core/fastweb.cpp @@ -59,6 +59,7 @@ bool fastweb::start() // 初始化脚本 if (initialization_script() == false) return false; +#if ENABLE_BYTECODE == 1 // 加载服务脚本 { auto luas = ylib::file::traverse(sConfig->scripts.app_dir, "(.*\\.lua)"); @@ -73,6 +74,7 @@ bool fastweb::start() } } } + // 加载拦截器脚本 for_iter(iter, sConfig->website.interceptor_scripts) { if (interceptor_bytecode.create(iter->regex_express, iter->filepath, true) == false) @@ -81,6 +83,7 @@ bool fastweb::start() return false; } } + // 加入LUA服务映射 { auto map = service_bytecode.map(); @@ -146,10 +149,85 @@ bool fastweb::start() } } - +#else + // 加入LUA服务映射 + { + 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, '\\', '/'); + auto state = sStateMgr->get_state(); + std::string route_pattern; + network::http::method method = network::http::ALL; + try + { + auto result = state->script_file(sConfig->scripts.app_dir+"/"+ path); + if (result.valid()) { + auto router = (*state)["route"]; + auto type = router.get_type(); + if (router.is()) + { + sol::optional route_pattern_param = router[1]; + sol::optional 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()); + } + if (route_pattern.empty()) + route_pattern = sConfig->scripts.app_mapping_dir + path; + + // OutPutLog + { + std::string log; + log = "[subscribe] lua: " +path + "\t pattern: " + route_pattern + "\t method: "; + switch (method) + { + 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; + } + LOG_INFO(log); + } + router->subscribe(route_pattern, method, &fastweb::subscribe_service, new std::string(sConfig->scripts.app_dir+"/" +path)); + } + + } +#endif // 加入拦截器 - for(size_t i=0;iwebsite.interceptor_scripts.size();i++) - router->interceptor()->add(sConfig->website.interceptor_scripts[i].regex_express,&fastweb::subscribe_interceptor); + for (size_t i = 0; i < sConfig->website.interceptor_scripts.size(); i++) + { +#if ENABLE_BYTECODE == 0 + interceptor.emplace(sConfig->website.interceptor_scripts[i].regex_express,sConfig->website.interceptor_scripts[i].filepath); +#endif + router->interceptor()->add(sConfig->website.interceptor_scripts[i].regex_express, &fastweb::subscribe_interceptor); + } + router->other([&](network::http::request* request, network::http::response* response) { @@ -232,10 +310,14 @@ void fastweb::subscribe_service(network::http::request* request, network::http:: std::string exception_string; try { +#if ENABLE_BYTECODE == 1 auto bytecode = sFastWeb->service_bytecode.get(lua_name); if (bytecode.empty()) throw ylib::exception("Serious error: Bytecode not found, possibly due to pre compilation modification error. Please recheck the script file, "+lua_name); auto lbResult = lua->load_buffer(bytecode.data(), bytecode.length(), "bytecode"); +#else + auto lbResult = lua->load_file(lua_name); +#endif if (lbResult.valid() == false) { sol::error err = lbResult; @@ -276,11 +358,15 @@ bool fastweb::subscribe_interceptor(network::http::reqpack* reqpack, const std:: std::string exception_string; try { +#if ENABLE_BYTECODE == 1 const std::string& bytecode = sFastWeb->interceptor_bytecode.get(express_string); if (bytecode.empty()) throw ylib::exception("[interceptor] Serious error: Bytecode not found, possibly due to pre compilation modification error. Please recheck the script file, " + express_string); auto lbResult = lua->load_buffer(bytecode.data(), bytecode.length(), "bytecode"); +#else + auto lbResult = lua->load_file(sFastWeb->interceptor[express_string]); +#endif if (lbResult.valid() == false) { sol::error err = lbResult; diff --git a/src/core/fastweb.h b/src/core/fastweb.h index 5c952f0..3ea751f 100644 --- a/src/core/fastweb.h +++ b/src/core/fastweb.h @@ -42,8 +42,12 @@ private: private: network::http::center *m_center = nullptr; public: +#if ENABLE_BYTECODE == 1 // 服务字节码 bytecode_manager service_bytecode; // 拦截器字节码 bytecode_manager interceptor_bytecode; +#else + std::map interceptor; +#endif }; \ No newline at end of file diff --git a/src/core/statemanager.cpp b/src/core/statemanager.cpp index 7c0ad86..c45b5cb 100644 --- a/src/core/statemanager.cpp +++ b/src/core/statemanager.cpp @@ -21,7 +21,7 @@ #include "module/mutex.h" #include "module/codec.h" #include "module/time.h" - +#include "module/file.h" #define LOOP_STATE_USE 1 bool state_manager::start() { @@ -81,6 +81,7 @@ sol::state* state_manager::create_state() module::auto_lock::regist(lua); module::codec::regist(lua); module::time::regist(lua); + module::file::regist(lua); global::getInstance()->regist_lua(lua); return lua; diff --git a/src/module/file.cpp b/src/module/file.cpp new file mode 100644 index 0000000..14a12ed --- /dev/null +++ b/src/module/file.cpp @@ -0,0 +1,29 @@ +#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::regist(sol::state* lua) +{ + (*lua)["IS_FILE"] = (int)ylib::FileType::IS_FILE; + (*lua)["IS_DIRECTORY"] = (int)ylib::FileType::IS_DIRECTORY; + + + lua->new_usertype("file", + "list", &module::file::list + ); +} diff --git a/src/module/file.h b/src/module/file.h new file mode 100644 index 0000000..0974569 --- /dev/null +++ b/src/module/file.h @@ -0,0 +1,17 @@ +#pragma once +#include "sol/sol.hpp" +#include "imodule.h" + +namespace module +{ + /// + /// 文件 + /// + class file:public module::imodule { + public: + static sol::table list(const std::string& dirpath,const std::string& regex,sol::this_state s); + static void regist(sol::state* lua); + }; + +} + diff --git a/src/module/http/request.cpp b/src/module/http/request.cpp index da28fc9..88290ec 100644 --- a/src/module/http/request.cpp +++ b/src/module/http/request.cpp @@ -37,6 +37,10 @@ sol::table module::request::url_param(sol::this_state s) result_table[iter->first] = iter->second; return result_table; } +std::string module::request::body() +{ + return m_request->parser()->text(); +} void* module::request::website() { return m_request->website(); @@ -55,7 +59,8 @@ void module::request::regist(sol::state& state) "session", &module::request::session, "token", &module::request::token, "body_param", &module::request::body_param, - "url_param", &module::request::url_param + "url_param", &module::request::url_param, + "body", &module::request::body ); state["GET"] = (int)network::http::GET; state["POST"] = (int)network::http::POST; diff --git a/src/module/http/request.h b/src/module/http/request.h index 21a90d4..1f68eac 100644 --- a/src/module/http/request.h +++ b/src/module/http/request.h @@ -27,6 +27,7 @@ namespace module sol::table body_param(sol::this_state s); sol::table url_param(sol::this_state s); + std::string body(); void* website(); static void regist(sol::state& state); diff --git a/src/module/mssql.cpp b/src/module/mssql.cpp index 7a7d387..e85f21f 100644 --- a/src/module/mssql.cpp +++ b/src/module/mssql.cpp @@ -8,43 +8,70 @@ module::mssql::mssql(const std::string& connstring) module::mssql::~mssql() { - + } std::string module::mssql::get_str(const std::string& name) { if (m_iter == m_rows.end()) throw ylib::exception("no recorded data"); - return m_iter->get(name); + std::string result; + result = m_iter->get(name, result); + return result; +} + +std::string module::mssql::get_datetime(const std::string& name) +{ + if (m_iter == m_rows.end()) + throw ylib::exception("no recorded data"); + std::tm result; + memset(&result, 0, sizeof(result)); + result = m_iter->get(name, result); + if (result.tm_year != 0) + { + char buffer[80]; + strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", &result); + std::string dateTimeStr(buffer); + return dateTimeStr; + } + return ""; } int32 module::mssql::get_i32(const std::string& name) { if (m_iter == m_rows.end()) throw ylib::exception("no recorded data"); - return m_iter->get(name); + int32 result = 0; + result = m_iter->get(name, result); + return result; } int64 module::mssql::get_i64(const std::string& name) { if (m_iter == m_rows.end()) throw ylib::exception("no recorded data"); - return m_iter->get(name); + int64 result = 0; + result = m_iter->get(name, result); + return result; } double module::mssql::get_dob(const std::string& name) { if (m_iter == m_rows.end()) throw ylib::exception("no recorded data"); - return m_iter->get(name); + double result = 0.0f; + result = m_iter->get(name, result); + return result; } void module::mssql::query(const std::string& sql) { + std::cout << "[mssql][query]: " << sql << std::endl; m_read = false; m_rows = (m_session->prepare << sql); //m_iter = m_rows.begin(); } int64 module::mssql::update(const std::string& sql) { + std::cout << "[mssql][update]: " << sql << std::endl; soci::statement st = m_session->prepare << sql; st.execute(); return st.get_affected_rows(); @@ -54,12 +81,13 @@ bool module::mssql::next() if (m_read == false) { m_iter = m_rows.begin(); - return true; + + return m_iter != m_rows.end(); } if (m_iter == m_rows.end()) return false; m_iter++; - return true; + return m_iter != m_rows.end(); } @@ -73,7 +101,8 @@ void module::mssql::regist(sol::state& lua) "get_str", &module::mssql::get_str, "next", &module::mssql::next, "query", &module::mssql::query, - "update", &module::mssql::update + "update", &module::mssql::update, + "get_datetime", &module::mssql::get_datetime ); } #endif \ No newline at end of file diff --git a/src/module/mssql.h b/src/module/mssql.h index 5c92566..6ea2282 100644 --- a/src/module/mssql.h +++ b/src/module/mssql.h @@ -18,6 +18,7 @@ namespace module /// 取数据 /// std::string get_str(const std::string& name); + std::string get_datetime(const std::string& name); int32 get_i32(const std::string& name); int64 get_i64(const std::string& name); double get_dob(const std::string& name); diff --git a/src/module/mysql.cpp b/src/module/mysql.cpp index 1ce74c4..729f393 100644 --- a/src/module/mysql.cpp +++ b/src/module/mysql.cpp @@ -39,6 +39,12 @@ module::select& module::select::where_expression(const std::string& expression) return *this; } +module::select& module::select::where_like(const std::string& name, const std::string& value) +{ + m_select->where_like(name, value); + return *this; +} + module::select& module::select::table(const std::string& table_name) { m_select->table(table_name); @@ -79,7 +85,7 @@ module::select& module::select::orderby(const std::string& field, int sort) void module::select::clear() { - m_select = nullptr; + m_select->clear(); } std::shared_ptr module::select::query() @@ -108,6 +114,8 @@ void module::select::regist(sol::state& lua) "where_i64", &module::select::where_i64, "where_str", &module::select::where_str, "query", &module::select::query, + "clear", &module::select::clear, + "where_like", &module::select::where_like, "clear", &module::select::clear ); } @@ -210,6 +218,11 @@ uint64 module::update::exec() return m_update->exec(); } +void module::update::clear() +{ + m_update->clear(); +} + void module::update::regist(sol::state& lua) { lua.new_usertype("mysql_builder_update", @@ -228,7 +241,8 @@ void module::update::regist(sol::state& lua) "set_dob", &module::update::set_dob, "set_i32", &module::update::set_i32, "set_i64", &module::update::set_i64, - "set_str", &module::update::set_str + "set_str", &module::update::set_str, + "clear", &module::update::clear ); } @@ -282,6 +296,11 @@ uint64 module::insert::exec() return m_insert->exec(); } +void module::insert::clear() +{ + m_insert->clear(); +} + void module::insert::regist(sol::state& lua) { lua.new_usertype("mysql_builder_insert", @@ -292,7 +311,8 @@ void module::insert::regist(sol::state& lua) "set_dob", &module::insert::set_dob, "set_i32", &module::insert::set_i32, "set_i64", &module::insert::set_i64, - "set_str", &module::insert::set_str + "set_str", &module::insert::set_str, + "clear", &module::insert::clear ); } @@ -365,6 +385,11 @@ uint64 module::delete_::exec() return m_delete->exec(); } +void module::delete_::clear() +{ + m_delete->clear(); +} + void module::delete_::regist(sol::state& lua) { lua.new_usertype("mysql_builder_delete", @@ -378,7 +403,8 @@ void module::delete_::regist(sol::state& lua) "where_expression", &module::delete_::where_expression, "where_i32", &module::delete_::where_i32, "where_i64", &module::delete_::where_i64, - "where_str", &module::delete_::where_str + "where_str", &module::delete_::where_str, + "clear", &module::delete_::clear ); } @@ -529,7 +555,7 @@ VarType module::mysql_result::get(sol::object obj, sol::this_state s) GET_VALUE(get_int32); } - else if (type == "varchar" || type == "char" || type == "text") + else if (type == "varchar" || type == "char" || type == "text" || type == "datetime") { GET_VALUE(get_string); } diff --git a/src/module/mysql.h b/src/module/mysql.h index 04309d2..8514db9 100644 --- a/src/module/mysql.h +++ b/src/module/mysql.h @@ -76,6 +76,7 @@ namespace module module::select& where_dob(const std::string& name, const std::string& expression, double value); module::select& where_str(const std::string& name, const std::string& expression, const std::string& value); module::select& where_expression(const std::string& expression); + module::select& where_like(const std::string& name,const std::string& value); module::select& table(const std::string& table_name); module::select& field(sol::table table); module::select& page(uint32 page, uint32 count); @@ -107,7 +108,7 @@ namespace module module::update& limit(uint32 start, uint32 count); module::update& orderby(const std::string& field, int sort); uint64 exec(); - + void clear(); static void regist(sol::state& lua); private: std::shared_ptr m_update; @@ -123,7 +124,7 @@ namespace module module::insert& set_str(const std::string& name, const std::string& value); module::insert& set_not_ppst(const std::string& name, const std::string& value); uint64 exec(); - + void clear(); static void regist(sol::state& lua); private: std::shared_ptr m_insert; @@ -142,7 +143,7 @@ namespace module module::delete_& limit(uint32 start, uint32 count); module::delete_& orderby(const std::string& field, int sort); uint64 exec(); - + void clear(); static void regist(sol::state& lua); private: std::shared_ptr m_delete;