diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index 1ff0c42..0000000 --- a/.gitattributes +++ /dev/null @@ -1,63 +0,0 @@ -############################################################################### -# Set default behavior to automatically normalize line endings. -############################################################################### -* text=auto - -############################################################################### -# Set default behavior for command prompt diff. -# -# This is need for earlier builds of msysgit that does not have it on by -# default for csharp files. -# Note: This is only used by command line -############################################################################### -#*.cs diff=csharp - -############################################################################### -# Set the merge driver for project and solution files -# -# Merging from the command prompt will add diff markers to the files if there -# are conflicts (Merging from VS is not affected by the settings below, in VS -# the diff markers are never inserted). Diff markers may cause the following -# file extensions to fail to load in VS. An alternative would be to treat -# these files as binary and thus will always conflict and require user -# intervention with every merge. To do so, just uncomment the entries below -############################################################################### -#*.sln merge=binary -#*.csproj merge=binary -#*.vbproj merge=binary -#*.vcxproj merge=binary -#*.vcproj merge=binary -#*.dbproj merge=binary -#*.fsproj merge=binary -#*.lsproj merge=binary -#*.wixproj merge=binary -#*.modelproj merge=binary -#*.sqlproj merge=binary -#*.wwaproj merge=binary - -############################################################################### -# behavior for image files -# -# image files are treated as binary by default. -############################################################################### -#*.jpg binary -#*.png binary -#*.gif binary - -############################################################################### -# diff behavior for common document formats -# -# Convert binary document formats to text before diffing them. This feature -# is only available from the command line. Turn it on by uncommenting the -# entries below. -############################################################################### -#*.doc diff=astextplain -#*.DOC diff=astextplain -#*.docx diff=astextplain -#*.DOCX diff=astextplain -#*.dot diff=astextplain -#*.DOT diff=astextplain -#*.pdf diff=astextplain -#*.PDF diff=astextplain -#*.rtf diff=astextplain -#*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore index 70a5081..aeaa683 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,4 @@ # 此 .gitignore 文件已由 Microsoft(R) Visual Studio 自动创建。 ################################################################################ -/.vs -/session -/obj -/bin +/build diff --git a/3rdparty/ylib/include/db/mssql.h b/3rdparty/ylib/include/db/mssql.h index 5aaf9f9..6bf1dc2 100644 --- a/3rdparty/ylib/include/db/mssql.h +++ b/3rdparty/ylib/include/db/mssql.h @@ -19,7 +19,7 @@ namespace ylib::mssql class prepare_statement; struct mssql_conn_info { - mssql_conn_info(){ + mssql_conn_info() { } std::string odbcname; std::string username; @@ -68,19 +68,19 @@ namespace ylib::mssql std::map m_field_name_index; }; - class prepare_statement:public ylib::error_base{ + class prepare_statement :public ylib::error_base { public: prepare_statement(); ~prepare_statement(); - void set_boolean(uint32 index,bool value); - void set_datetime(uint32 index,const std::string& value); - void set_double(uint32 index,double value); - void set_int32(uint32 index,int32 value); - void set_uint32(uint32 index,uint32 value); - void set_int64(uint32 index,int64 value); - void set_uint64(uint32 index,uint64 value); + void set_boolean(uint32 index, bool value); + void set_datetime(uint32 index, const std::string& value); + void set_double(uint32 index, double value); + void set_int32(uint32 index, int32 value); + void set_uint32(uint32 index, uint32 value); + void set_int64(uint32 index, int64 value); + void set_uint64(uint32 index, uint64 value); void set_null(uint32 index); - void set_string(uint32 index,const std::string& value); + void set_string(uint32 index, const std::string& value); void clear(); uint64 update(); ylib::mssql::result* query(); @@ -90,12 +90,12 @@ namespace ylib::mssql ylib::mssql::result* m_result = nullptr; void* m_handle = nullptr; }; - class conn :public ylib::example,public ylib::error_base + class conn :public ylib::example, public ylib::error_base { public: conn(); ~conn(); - virtual EXAMPLE_START_RESULT start(const ylib::mssql::mssql_conn_info&info) override; + virtual EXAMPLE_START_RESULT start(const ylib::mssql::mssql_conn_info& info) override; virtual void close() override; @@ -110,14 +110,14 @@ namespace ylib::mssql void rollback(); friend class ylib::mssql::pool; private: - void *m_handle = nullptr; + void* m_handle = nullptr; ylib::mssql::mssql_conn_info m_info; class prepare_statement* m_ppst = nullptr; int m_sw = 0; }; - class pool: public ylib::pool + class pool : public ylib::pool { public: pool(); diff --git a/3rdparty/ylib/include/db/mysql.h b/3rdparty/ylib/include/db/mysql.h index 1518691..96c3d68 100644 --- a/3rdparty/ylib/include/db/mysql.h +++ b/3rdparty/ylib/include/db/mysql.h @@ -17,14 +17,14 @@ namespace ylib::mysql class result; class conn; class prepare_statement; - struct field{ + struct field { uint32 index = 0; std::string name; std::string type_name; }; struct mysql_conn_info { - mysql_conn_info(){ + mysql_conn_info() { port = 0; } std::string ipaddress; @@ -81,21 +81,21 @@ namespace ylib::mysql std::vector m_fields; }; - class prepare_statement:public ylib::error_base{ + class prepare_statement :public ylib::error_base { public: prepare_statement(); ~prepare_statement(); - void set_bigint(uint32 index,const std::string& value); - void set_boolean(uint32 index,bool value); - void set_datetime(uint32 index,const std::string& value); - void set_double(uint32 index,double value); - void set_int32(uint32 index,int32 value); - void set_uint32(uint32 index,uint32 value); - void set_int64(uint32 index,int64 value); - void set_uint64(uint32 index,uint64 value); + void set_bigint(uint32 index, const std::string& value); + void set_boolean(uint32 index, bool value); + void set_datetime(uint32 index, const std::string& value); + void set_double(uint32 index, double value); + void set_int32(uint32 index, int32 value); + void set_uint32(uint32 index, uint32 value); + void set_int64(uint32 index, int64 value); + void set_uint64(uint32 index, uint64 value); void set_null(uint32 index); - void set_string(uint32 index,const std::string& value); - void set_blob(uint32 index,const ylib::buffer& value); + void set_string(uint32 index, const std::string& value); + void set_blob(uint32 index, const ylib::buffer& value); void clear(); uint64 update(); ylib::mysql::result* query(); @@ -105,12 +105,12 @@ namespace ylib::mysql ylib::mysql::result* m_result = nullptr; void* m_handle = nullptr; }; - class conn :public ylib::example,public ylib::error_base + class conn :public ylib::example, public ylib::error_base { public: conn(); ~conn(); - virtual EXAMPLE_START_RESULT start(const ylib::mysql::mysql_conn_info&info) override; + virtual EXAMPLE_START_RESULT start(const ylib::mysql::mysql_conn_info& info) override; virtual void close() override; @@ -130,7 +130,7 @@ namespace ylib::mysql friend class ylib::mysql::pool; private: - void *m_handle = nullptr; + void* m_handle = nullptr; ylib::mysql::mysql_conn_info m_info; class prepare_statement* m_ppst = nullptr; // 事务状态 0=未开启 1=已开启 2=执行完毕 @@ -138,7 +138,7 @@ namespace ylib::mysql }; - class pool: public ylib::pool + class pool : public ylib::pool { public: pool() diff --git a/3rdparty/ylib/include/db/sqler.h b/3rdparty/ylib/include/db/sqler.h index e026ac7..274532c 100644 --- a/3rdparty/ylib/include/db/sqler.h +++ b/3rdparty/ylib/include/db/sqler.h @@ -21,7 +21,7 @@ namespace ylib }; struct order_by { std::string field; - sort sort; + ylib::sort sort; }; struct keyvalue { std::string name; @@ -31,7 +31,7 @@ namespace ylib /// /// MYSQL SELECT 查询器 /// - class select:public ylib::error_base { + class select :public ylib::error_base { public: select(mysql::conn* conn); ~select(); @@ -51,11 +51,11 @@ namespace ylib /// /// 条件[A=B] /// - select& where(const std::string& name,const std::string& expression,const std::any& value); + select& where(const std::string& name, const std::string& expression, const std::any& value); /// /// 条件[模糊查询] /// - select& where_like(const std::string& name,const std::string& value); + select& where_like(const std::string& name, const std::string& value); /// /// 条件[自定义] /// @@ -63,7 +63,7 @@ namespace ylib /// /// LIMIT[页] /// - select& page(uint32 page,uint32 count); + select& page(uint32 page, uint32 count); /// /// LIMIT /// @@ -96,7 +96,7 @@ namespace ylib /// /// /// - void make_sql(std::string& field_name,std::string& where,std::string& orderby,std::string& limit,std::vector& insert_values); + void make_sql(std::string& field_name, std::string& where, std::string& orderby, std::string& limit, std::vector& insert_values); private: mysql::conn* m_conn = nullptr; std::vector m_wheres; @@ -106,18 +106,6 @@ namespace ylib order_by m_orderby; }; -#if 0 - class Lselect :public ylib::select { - public: - Lselect(void* conn); - ~Lselect(); - void where_i32(const std::string& name, const std::string& expression,int32 value); - void where_i64(const std::string& name, const std::string& expression,int64 value); - void where_dob(const std::string& name, const std::string& expression,double value); - void where_str(const std::string& name, const std::string& expression, const std::string& value); - void where_expression(const std::string& expression); - }; -#endif /// /// MYSQL UPDATE /// @@ -189,26 +177,6 @@ namespace ylib order_by m_orderby; }; -#if 0 - class Lupdate :public ylib::update { - public: - Lupdate(mysql::conn* conn); - ~Lupdate(); - - void set_i32(const std::string& name, int32 value); - void set_i64(const std::string& name, int64 value); - void set_dob(const std::string& name, double value); - void set_str(const std::string& name, const std::string& value); - - void where_i32(const std::string& name, const std::string& expression, int32 value); - void where_i64(const std::string& name, const std::string& expression, int64 value); - void where_dob(const std::string& name, const std::string& expression, double value); - void where_str(const std::string& name, const std::string& expression, const std::string& value); - - void where_expression(const std::string& expression); - }; - -#endif /// /// MYSQL INSERT /// @@ -236,17 +204,6 @@ namespace ylib std::string m_table_name; std::vector m_sets; }; -#if 0 - class Linsert :public ylib::insert { - public: - Linsert(mysql::conn* conn); - ~Linsert(); - void i32(const std::string& name, int32 value); - void i64(const std::string& name, int64 value); - void dob(const std::string& name, double value); - void str(const std::string& name, const std::string& value); - }; -#endif /// /// MYSQL DELETE /// @@ -304,16 +261,4 @@ namespace ylib order_by m_orderby; }; -#if 0 - class Ldelete :public ylib::delete_ { - public: - Ldelete(mysql::conn* conn); - ~Ldelete(); - void where_i32(const std::string& name, const std::string& expression, int32 value); - void where_i64(const std::string& name, const std::string& expression, int64 value); - void where_dob(const std::string& name, const std::string& expression, double value); - void where_str(const std::string& name, const std::string& expression, const std::string& value); - void where_expression(const std::string& expression); - }; -#endif -} \ No newline at end of file +} diff --git a/3rdparty/ylib/include/net/http_router.h b/3rdparty/ylib/include/net/http_router.h index 484d5e7..17214ee 100644 --- a/3rdparty/ylib/include/net/http_router.h +++ b/3rdparty/ylib/include/net/http_router.h @@ -36,7 +36,7 @@ namespace ylib // 请求类型 network::http::method method; // 回调函数 - std::function callback; + std::function callback; // 控制器。为控制器则启动下面两个属性 bool controller; // 创建控制器类指针 @@ -49,6 +49,8 @@ namespace ylib // LUA虚拟机初始化回调 std::function lua_init_state; #endif + // 附加数据 + void* extra = nullptr; }; /************************************************************************* * class:路由中专服务 @@ -94,9 +96,10 @@ namespace ylib * 同一地址不允许订阅两次 ******************************************************************/ void subscribe( - const std::string& path, + const std::string& path, network::http::method method, - std::function callback + std::function callback, + void* extra = nullptr ); #define SUBSCRIBE(ROUTER,CONTROLLER,FUNCTION,PATH,METHOD) ROUTER->subscribe([]()->void*{return new CONTROLLER;},(ylib::network::http::HTTP_CTR_FUNCTION)&CONTROLLER::FUNCTION,PATH,METHOD) diff --git a/3rdparty/ylib/lib/Debug/ylib.lib b/3rdparty/ylib/lib/Debug/ylib.lib index 075ef1b..67a28e5 100644 Binary files a/3rdparty/ylib/lib/Debug/ylib.lib and b/3rdparty/ylib/lib/Debug/ylib.lib differ diff --git a/3rdparty/ylib/lib/Release/ylib.lib b/3rdparty/ylib/lib/Release/ylib.lib index 173114c..d21acec 100644 Binary files a/3rdparty/ylib/lib/Release/ylib.lib and b/3rdparty/ylib/lib/Release/ylib.lib differ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..0cc3fdc --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,138 @@ +cmake_minimum_required(VERSION 3.5) +project("fastweb") + +# 设置自定义配置类型 +if(MSVC) + set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Build config types" FORCE) +endif() +# C++等级 +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED TRUE) +# 警告等级 + +# Recursively get all source files +file(GLOB_RECURSE SOURCE_FILES + "${PROJECT_SOURCE_DIR}/src/*.cpp" +) +file(GLOB_RECURSE HEADER_FILES + "${PROJECT_SOURCE_DIR}/src/*.h" +) +foreach(source IN LISTS SOURCE_FILES) + get_filename_component(source_path "${source}" PATH) + file(RELATIVE_PATH source_path_rel "${PROJECT_SOURCE_DIR}" "${source_path}") + string(REPLACE "/" "\\" source_path_rel_win "${source_path_rel}") + source_group(${source_path_rel_win} FILES "${source}") +endforeach() +foreach(header IN LISTS HEADER_FILES) + get_filename_component(header_path "${header}" PATH) + file(RELATIVE_PATH header_path_rel "${PROJECT_SOURCE_DIR}" "${header_path}") + string(REPLACE "/" "\\" header_path_rel_win "${header_path_rel}") + source_group(${header_path_rel_win} FILES "${header}") +endforeach() + + +include_directories(${PROJECT_SOURCE_DIR}/src) +if(MSVC) + include_directories(${PROJECT_SOURCE_DIR}/src) + include_directories(${PROJECT_SOURCE_DIR}/3rdparty) + include_directories(${PROJECT_SOURCE_DIR}/3rdparty/HP-Socket/Windows/Include) + include_directories(${PROJECT_SOURCE_DIR}/3rdparty/lua) + include_directories(${PROJECT_SOURCE_DIR}/3rdparty/soci/include) + include_directories(${PROJECT_SOURCE_DIR}/3rdparty/mysql/include/jdbc) + include_directories(${PROJECT_SOURCE_DIR}/3rdparty/ylib/include) + add_compile_options(/W3 /wd4819) +else() + include_directories(${PROJECT_SOURCE_DIR}/3rdparty) + include_directories(/usr/include/lua5.4/) +endif() + + +# Add the executable or library target +add_executable(${PROJECT_NAME} ${SOURCE_FILES} ${HEADER_FILES}) +link_directories(/usr/lib/x86_64-linux-gnu) +link_directories(/usr/local/lib) + + +if(MSVC) + target_link_libraries(${PROJECT_NAME} + odbc32.lib + User32.lib + Advapi32.lib + IPHLPAPI.lib + WS2_32.lib + Shell32.lib + ${PROJECT_SOURCE_DIR}/3rdparty/libcrypto_static.lib + + $<$:${PROJECT_SOURCE_DIR}/3rdparty/HP-Socket/Lib/HPSocket_D.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/mysql/lib/Debug/mysqlcppconn.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/ylib/lib/Debug/leveldb.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/ylib/lib/Debug/libzip.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/ylib/lib/Debug/lua.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/ylib/lib/Debug/sqlite3.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/ylib/lib/Debug/ylib.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/ylib/lib/Debug/zlib.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/soci/lib/Debug/libsoci_core_4_1.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/soci/lib/Debug/libsoci_empty_4_1.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/soci/lib/Debug/libsoci_odbc_4_1.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/soci/lib/Debug/soci_core_4_1.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/soci/lib/Debug/soci_empty_4_1.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/soci/lib/Debug/soci_odbc_4_1.lib> + + $<$:${PROJECT_SOURCE_DIR}/3rdparty/HP-Socket/Lib/HPSocket.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/mysql/lib/Release/mysqlcppconn.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/ylib/lib/Release/leveldb.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/ylib/lib/Release/libzip.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/ylib/lib/Release/lua.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/ylib/lib/Release/sqlite3.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/ylib/lib/Release/ylib.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/ylib/lib/Release/zlib.lib> + + $<$:${PROJECT_SOURCE_DIR}/3rdparty/soci/lib/Release/libsoci_core_4_1.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/soci/lib/Release/libsoci_empty_4_1.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/soci/lib/Release/libsoci_odbc_4_1.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/soci/lib/Release/soci_core_4_1.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/soci/lib/Release/soci_empty_4_1.lib> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/soci/lib/Release/soci_odbc_4_1.lib> + + + + ) +else() + target_link_libraries(${PROJECT_NAME} + hpsocket + ylib + leveldb + soci_core + soci_firebird + soci_mysql + soci_odbc + soci_postgresql + soci_sqlite3 + crypto + lua5.4 + mysqlcppconn + ) + +endif() + + + +######################## 安装 ######################## +install(TARGETS ${PROJECT_NAME} DESTINATION bin) +install(FILES + $<$:${PROJECT_SOURCE_DIR}/3rdparty/HP-Socket/Lib/HPSocket_D.dll> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/mysql/lib/Debug/libcrypto-3-x64.dll> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/mysql/lib/Debug/libssl-3-x64.dll> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/mysql/lib/Debug/mysqlcppconn-9-vs14.dll> + + + $<$:${PROJECT_SOURCE_DIR}/3rdparty/HP-Socket/Lib/HPSocket.dll> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/mysql/lib/Release/libcrypto-3-x64.dll> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/mysql/lib/Release/libssl-3-x64.dll> + $<$:${PROJECT_SOURCE_DIR}/3rdparty/mysql/lib/Release/mysqlcppconn-9-vs14.dll> + config.ini +DESTINATION bin) +install(DIRECTORY ${PROJECT_SOURCE_DIR}/scripts DESTINATION bin) + + + diff --git a/LICENSE.txt b/LICENSE.txt deleted file mode 100644 index 8aa2645..0000000 --- a/LICENSE.txt +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) [year] [fullname] - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/README.md b/README.md deleted file mode 100644 index 83dec2e..0000000 --- a/README.md +++ /dev/null @@ -1,69 +0,0 @@ -# Fast Web (快速网站开发框架) - - -## 简介 -Fast Web 是使用 C++ 和 Lua 开发的网站框架,以快速开发、部署简单为理念,核心部分由C++实现,Lua 脚本语言处理业务逻辑, -Fast Web 旨在简化复杂的部署和环境配置,使开发者能够迅速启动并运行他们的网站。 - -QQ群:153076832 -## 特性 -- **LUA脚本**:通过Lua脚本实现业务逻辑开发,编写及维护更加容易 -- **易于部署**:只需一个可执行文件 -- **灵活易用**:适合各类个人博客、API接口到复杂的企业级项目 - -## 示例 - -URL: http://127.0.0.1:8888/scripts/index.lua?key=123456 - -#### 拦截器 -```lua -function access() - - if request:pstring("key") == "123456" then - return true - end - - response:send("密钥不正确,已被拦截器拦截.") - return false -end -``` -#### 业务代码 -```lua --- 通用入口函数 -function access() - - -- MYSQL执行SELECT查询map表 - local result = mysql:select():table("map"):query() - - -- 构建回复数据 - local data = result:table() - - -- 返回JSON数据 - json(data) -end -``` - -## 部署 -目前仅支持Windows部署,Linux将在下个版本支持 -```bash -# 克隆仓库 -git clone https://github.com/Liuccysdgg/fastweb.git -# 进入项目目录 -cd fastweb -# 编译项目 (确保你的机器上安装了VS2022集成开发环境) -# 运行 fastweb.sln 编译 -``` - -三方库均仅提供x64二进制发布版本,如需更新或修改请自行根据下方链接下载编译 - -## 🙇致谢 -如果没有社区已有的优秀软件帮助,fastweb就不可能构建出来 - -https://github.com/Liuccysdgg/ylib 跨平台快速开发库 - -https://github.com/ldcsaa/HP-Socket HPSocket高性能网络库 - -https://github.com/ThePhD/sol2 C++ 与 Lua 的绑定 - -https://github.com/lua/lua 脚本解释器 - diff --git a/config.ini b/config.ini index 0d8c5a4..b66711a 100644 --- a/config.ini +++ b/config.ini @@ -1,5 +1,5 @@ [variable] -base=D:/project/fastweb +base=${current_dir} [scripts] ; 应用目录(可执行脚本) ; 考虑安全性,请勿放置于网站静态文件目录 diff --git a/fastweb.sln b/fastweb.sln deleted file mode 100644 index 1cba18b..0000000 --- a/fastweb.sln +++ /dev/null @@ -1,31 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.9.34723.18 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "fastweb", "fastweb.vcxproj", "{30FF78DA-6DB2-4600-9DB1-8D581D3B38BA}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x64 = Debug|x64 - Debug|x86 = Debug|x86 - Release|x64 = Release|x64 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {30FF78DA-6DB2-4600-9DB1-8D581D3B38BA}.Debug|x64.ActiveCfg = Debug|x64 - {30FF78DA-6DB2-4600-9DB1-8D581D3B38BA}.Debug|x64.Build.0 = Debug|x64 - {30FF78DA-6DB2-4600-9DB1-8D581D3B38BA}.Debug|x86.ActiveCfg = Debug|Win32 - {30FF78DA-6DB2-4600-9DB1-8D581D3B38BA}.Debug|x86.Build.0 = Debug|Win32 - {30FF78DA-6DB2-4600-9DB1-8D581D3B38BA}.Release|x64.ActiveCfg = Release|x64 - {30FF78DA-6DB2-4600-9DB1-8D581D3B38BA}.Release|x64.Build.0 = Release|x64 - {30FF78DA-6DB2-4600-9DB1-8D581D3B38BA}.Release|x86.ActiveCfg = Release|Win32 - {30FF78DA-6DB2-4600-9DB1-8D581D3B38BA}.Release|x86.Build.0 = Release|Win32 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {3C07C2E6-C40B-4D78-900B-E25ADE05A28C} - EndGlobalSection -EndGlobal diff --git a/fastweb.vcxproj b/fastweb.vcxproj deleted file mode 100644 index 6099b88..0000000 --- a/fastweb.vcxproj +++ /dev/null @@ -1,202 +0,0 @@ - - - - - Debug - Win32 - - - Release - Win32 - - - Debug - x64 - - - Release - x64 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 17.0 - Win32Proj - {30ff78da-6db2-4600-9db1-8d581d3b38ba} - fastweb - 10.0 - - - - Application - true - v143 - Unicode - - - Application - false - v143 - true - Unicode - - - Application - true - v143 - Unicode - - - Application - false - v143 - true - Unicode - - - - - - - - - - - - - - - - - - - - - $(SolutionDir)bin\debug - $(SolutionDir)obj\debug - - - $(SolutionDir)bin\release - $(SolutionDir)obj\release - - - - Level3 - true - WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - - - - - Level3 - true - true - true - WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - - - Console - true - true - true - - - - - Level3 - false - _DEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - $(SolutionDir)3rdparty\soci\include;$(SolutionDir)src;$(SolutionDir)3rdparty;$(SolutionDir)3rdparty\lua;D:\lib\ylib\include;$(SolutionDir)3rdparty\HP-Socket\include - /utf-8 %(AdditionalOptions) - stdcpp20 - - - Console - true - libsoci_core_4_1.lib;libsoci_empty_4_1.lib;libsoci_odbc_4_1.lib;soci_core_4_1.lib;soci_empty_4_1.lib;soci_odbc_4_1.lib;odbc32.lib;User32.lib;Advapi32.lib;IPHLPAPI.lib;WS2_32.lib;Shell32.lib;leveldb.lib;libzip.lib;lua.lib;sqlite3.lib;D:\lib\ylib_vs_build\lib\Debug\ylib.lib;zlib.lib;mysqlcppconn.lib;HPSocket_D.lib;libcrypto_static.lib - $(SolutionDir)3rdparty\ylib\lib\$(Configuration);$(SolutionDir)3rdparty\mysql\lib\$(Configuration);$(SolutionDir)3rdparty\HP-Socket\lib;$(SolutionDir)3rdparty;$(SolutionDir)3rdparty\soci\lib\$(Configuration) - - - - - Level3 - true - true - false - NDEBUG;_CONSOLE;%(PreprocessorDefinitions) - true - $(SolutionDir)3rdparty\soci\include;$(SolutionDir)src;$(SolutionDir)3rdparty;$(SolutionDir)3rdparty\lua;D:\lib\ylib\include;$(SolutionDir)3rdparty\HP-Socket\include - /utf-8 %(AdditionalOptions) - stdcpp20 - - - Console - true - true - true - libsoci_core_4_1.lib;libsoci_empty_4_1.lib;libsoci_odbc_4_1.lib;soci_core_4_1.lib;soci_empty_4_1.lib;soci_odbc_4_1.lib;odbc32.lib;User32.lib;Advapi32.lib;IPHLPAPI.lib;WS2_32.lib;Shell32.lib;leveldb.lib;libzip.lib;lua.lib;sqlite3.lib;ylib.lib;zlib.lib;mysqlcppconn.lib;HPSocket.lib;libcrypto_static.lib - $(SolutionDir)3rdparty\ylib\lib\$(Configuration);$(SolutionDir)3rdparty\mysql\lib\$(Configuration);$(SolutionDir)3rdparty\HP-Socket\lib;$(SolutionDir)3rdparty;$(SolutionDir)3rdparty\soci\lib\$(Configuration) - - - - - - \ No newline at end of file diff --git a/fastweb.vcxproj.filters b/fastweb.vcxproj.filters deleted file mode 100644 index aff9245..0000000 --- a/fastweb.vcxproj.filters +++ /dev/null @@ -1,152 +0,0 @@ - - - - - {edadfc2f-873d-4876-be12-0f0a346c671a} - - - {a7a813c2-1819-4d9e-97b1-553df4fc20f3} - - - {feb78a4c-cec1-4b7c-850b-6d1451e57962} - - - {2b04f1f8-4027-4d87-a828-f18f2b918b8f} - - - {a6112717-e5e3-426e-9132-03cd4315deb0} - - - - - src\core - - - src\core - - - src\core - - - src\core - - - src\utils - - - src\module - - - src\module\http - - - src\module\http - - - src\module\http - - - src\core - - - src\utils - - - src\core - - - src\module - - - src\module - - - src\module - - - src\module - - - src\module - - - src\module - - - src\module\http - - - - - src\core - - - src\core - - - src\core - - - src\core - - - src\core - - - src\utils - - - src\module - - - src\module\http - - - src\module\http - - - src\module\http - - - src\core - - - src\utils - - - src\core - - - src\module - - - src\module - - - src\module - - - src\module - - - src\module - - - src\module - - - src\module - - - src\module\http - - - - - - - - - - - \ No newline at end of file diff --git a/fastweb.vcxproj.user b/fastweb.vcxproj.user deleted file mode 100644 index 88a5509..0000000 --- a/fastweb.vcxproj.user +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/root/index.html b/root/index.html deleted file mode 100644 index d738fa0..0000000 --- a/root/index.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - - FastWeb快速开发框架 - - -

FastWeb快速开发框架

- - diff --git a/scripts/app/index.lua b/scripts/app/index.lua index 3cf155c..1d85446 100644 --- a/scripts/app/index.lua +++ b/scripts/app/index.lua @@ -1,17 +1,10 @@ require "website" - -- 通用入口函数 +route = {"/",GET} function access() - - -- MYSQL执行SELECT查询map表 - --local result = mysql:select():table("map"):query() - - -- 构建回复数据 - --local data = result:table() - local data = { name = "Fast Web 快速网站开发框架" } -- 返回JSON数据 - json(data) + reply(200,"OK",data) end \ No newline at end of file diff --git a/scripts/init.lua b/scripts/init.lua index 966d3bd..4c80de8 100644 --- a/scripts/init.lua +++ b/scripts/init.lua @@ -1,19 +1,6 @@ -require "website" --- MYSQL数据库池 + -function initMysql() - local pool = mysql_pool.new() - if pool:start("127.0.0.1","test","123456","test","utf8mb4",3306,10) == false then - print("start mysql pool failed") - return false - end - global_regist("mysql",pool:self()) - return true -end +print("init fast web success!") -if initMysql() == false then - return false -end - return true \ No newline at end of file diff --git a/scripts/interceptor.lua b/scripts/interceptor.lua index df9a5b7..df7961b 100644 --- a/scripts/interceptor.lua +++ b/scripts/interceptor.lua @@ -1,9 +1,7 @@ -function access() - - if request:pstring("key") == "123456" then - return true +require "website" +function access() + if param("key") == nil or param("key") ~= "123465" then + print("is not found key") end - - response:send("密钥不正确,已被拦截器拦截.") - return false + return true end \ No newline at end of file diff --git a/scripts/lib/website.lua b/scripts/lib/website.lua index 6825a56..35cb629 100644 --- a/scripts/lib/website.lua +++ b/scripts/lib/website.lua @@ -1,8 +1,23 @@ local dkjson = require 'dkjson' function json(data) + response:header("Content-Type","application/json") response:send(dkjson.encode(data)) end function session() return request:session(request:token()) end +function param(name) + return request:param(name,false) +end +function param_throw(name) + return request:param(name,true) +end +function reply(code,msg,data) + local data ={ + code =code, + msg = msg, + data = data + } + json(data) +end \ No newline at end of file diff --git a/src/core/config.cpp b/src/core/config.cpp index 7bf77a8..1a0249e 100644 --- a/src/core/config.cpp +++ b/src/core/config.cpp @@ -14,6 +14,8 @@ bool config::open(const std::string& ini_filepath) return false; } std::string src_content = ylib::file::read(temp_filepath); + src_content = strutils::replace(src_content, "${current_dir}", strutils::replace(system::current_dir(),'\\','/')); + ylib::file::write(temp_filepath,src_content); if (m_ini.open(temp_filepath)) { auto vars = extractVariableNames(ylib::file::read(temp_filepath)); diff --git a/src/core/fastweb.cpp b/src/core/fastweb.cpp index 19b3bc6..8a1060c 100644 --- a/src/core/fastweb.cpp +++ b/src/core/fastweb.cpp @@ -75,7 +75,66 @@ bool fastweb::start() { auto map = service_bytecode.map(); for_iter(iter, map) - router->subscribe(sConfig->scripts.app_mapping_dir + iter->first, ALL, &fastweb::subscribe_service); + { + auto state = sStateMgr->get_state(); + std::string route_pattern; + network::http::method method = network::http::ALL; + try + { + auto result = state->script_file(iter->second->filepath); + 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 + iter->first; + + // OutPutLog + { + std::string log; + log = "[subscribe] lua: " + iter->first + "\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(iter->first)); + } + } // 加入拦截器 @@ -132,11 +191,7 @@ bool fastweb::initialization_script() try { state->set_function("global_regist", module::global_regist); - auto result = state->script_file(script_filepath); - - - if (!result.valid()) { sol::error err = result; throw ylib::exception(err.what()); @@ -155,10 +210,12 @@ bool fastweb::initialization_script() return m_lastErrorDesc == ""; } -void fastweb::subscribe_service(network::http::request* request, network::http::response* response) +void fastweb::subscribe_service(network::http::request* request, network::http::response* response,void *extra) { + std::string lua_name = *(std::string*)extra; + // 文件原路径(非绝对路径) - std::string lua_name = strutils::right(request->filepath(), request->filepath().length() - sConfig->scripts.app_mapping_dir.length()); + //std::string lua_name = strutils::right(request->filepath(), request->filepath().length() - sConfig->scripts.app_mapping_dir.length()); auto lua = sStateMgr->get_state(); diff --git a/src/core/fastweb.h b/src/core/fastweb.h index d8d53c8..5c952f0 100644 --- a/src/core/fastweb.h +++ b/src/core/fastweb.h @@ -34,7 +34,7 @@ private: /// /// /// - static void subscribe_service(network::http::request* request, network::http::response* response); + static void subscribe_service(network::http::request* request, network::http::response* response, void* extra); /// /// 拦截器回调 /// diff --git a/src/core/main.cpp b/src/core/main.cpp index 363ce52..727a5d6 100644 --- a/src/core/main.cpp +++ b/src/core/main.cpp @@ -9,7 +9,7 @@ int main() std::cout << "=========== [fastweb engine] ============" << std::endl; - if(sConfig->open(system::current_dir() + "\\config.ini") == false) + if(sConfig->open(system::current_dir() + "/config.ini") == false) { LOG_ERROR("open config failed,"+sConfig->last_error()); return -1; @@ -25,4 +25,4 @@ int main() while (true) std::cin.get(); return 0; -} \ No newline at end of file +} diff --git a/src/core/statemanager.cpp b/src/core/statemanager.cpp index 31a500f..7c0ad86 100644 --- a/src/core/statemanager.cpp +++ b/src/core/statemanager.cpp @@ -13,7 +13,9 @@ #include "module/http/session.h" #include "module/http/httpclient.h" #include "module/mysql.h" +#ifdef _WIN32 #include "module/mssql.h" +#endif #include "module/localstorage.h" #include "module/globalfuns.h" #include "module/mutex.h" @@ -70,7 +72,9 @@ sol::state* state_manager::create_state() module::session::regist(*lua); module::httpclient::regist(*lua); module::mysql_regist(*lua); + #ifdef _WIN32 module::mssql::regist(*lua); + #endif module::regist_globalfuns(*lua); module::local_storage::regist(lua); module::mutex::regist(lua); diff --git a/src/module/codec.cpp b/src/module/codec.cpp index 191bec0..015a01c 100644 --- a/src/module/codec.cpp +++ b/src/module/codec.cpp @@ -10,12 +10,12 @@ std::string module::codec::url_en(const std::string& value) return ylib::codec::url::en(value); } -std::string module::codec::gbk_to_utf8(const std::string& value) +std::string module::codec::to_utf8(const std::string& value) { return ylib::codec::to_utf8(value); } -std::string module::codec::utf8_to_gbk(const std::string& value) +std::string module::codec::to_gbk(const std::string& value) { return ylib::codec::to_gbk(value); } @@ -30,8 +30,8 @@ void module::codec::regist(sol::state* lua) lua->new_usertype("codec", "url_de", &module::codec::url_de, "url_en", &module::codec::url_en, - "gbk_to_utf8", &module::codec::gbk_to_utf8, - "utf8_to_gbk", &module::codec::utf8_to_gbk, + "to_utf8", &module::codec::to_utf8, + "to_gbk", &module::codec::to_gbk, "md5", &module::codec::md5 ); } diff --git a/src/module/codec.h b/src/module/codec.h index 1c93d35..5c8e5e8 100644 --- a/src/module/codec.h +++ b/src/module/codec.h @@ -21,8 +21,8 @@ namespace module /// GBK转UTF8 /// /// - static std::string gbk_to_utf8(const std::string& value); - static std::string utf8_to_gbk(const std::string& value); + static std::string to_utf8(const std::string& value); + static std::string to_gbk(const std::string& value); /// /// MD5校验 /// diff --git a/src/module/http/request.cpp b/src/module/http/request.cpp index 105fade..da28fc9 100644 --- a/src/module/http/request.cpp +++ b/src/module/http/request.cpp @@ -57,6 +57,11 @@ void module::request::regist(sol::state& state) "body_param", &module::request::body_param, "url_param", &module::request::url_param ); + state["GET"] = (int)network::http::GET; + state["POST"] = (int)network::http::POST; + state["DEL"] = (int)network::http::DEL; + state["HEAD"] = (int)network::http::HEAD; + state["PUT"] = (int)network::http::PUT; } diff --git a/src/module/mssql.cpp b/src/module/mssql.cpp index 4c66f62..7a7d387 100644 --- a/src/module/mssql.cpp +++ b/src/module/mssql.cpp @@ -1,4 +1,5 @@ -#include "mssql.h" +#ifdef _WIN32 +#include "mssql.h" #include "soci/odbc/soci-odbc.h" module::mssql::mssql(const std::string& connstring) { @@ -42,9 +43,10 @@ void module::mssql::query(const std::string& sql) m_rows = (m_session->prepare << sql); //m_iter = m_rows.begin(); } -uint64 module::mssql::update(const std::string& sql) +int64 module::mssql::update(const std::string& sql) { - soci::statement st = (m_session->prepare << sql); + soci::statement st = m_session->prepare << sql; + st.execute(); return st.get_affected_rows(); } bool module::mssql::next() @@ -74,3 +76,4 @@ void module::mssql::regist(sol::state& lua) "update", &module::mssql::update ); } +#endif \ No newline at end of file diff --git a/src/module/mssql.h b/src/module/mssql.h index 8977268..5c92566 100644 --- a/src/module/mssql.h +++ b/src/module/mssql.h @@ -1,4 +1,5 @@ #pragma once +#ifdef _WIN32 #include "sol/sol.hpp" #include "imodule.h" #include "soci/soci.h" @@ -35,7 +36,7 @@ namespace module bool next(); - uint64 update(const std::string& sql); + int64 update(const std::string& sql); /// /// 注册 /// @@ -52,3 +53,4 @@ namespace module } +#endif \ No newline at end of file diff --git a/src/module/time.cpp b/src/module/time.cpp index 9f31f08..b2bf9b6 100644 --- a/src/module/time.cpp +++ b/src/module/time.cpp @@ -9,10 +9,16 @@ uint64 module::time::now_sec() return ylib::time::now_sec(); } +std::string module::time::now_time(const std::string& format) +{ + return ylib::time::now_time(format); +} + void module::time::regist(sol::state* lua) { lua->new_usertype("time", "now_msec", &module::time::now_msec, - "now_sec", &module::time::now_sec + "now_sec", &module::time::now_sec, + "now_time", &module::time::now_time ); } diff --git a/src/module/time.h b/src/module/time.h index fcfb5c5..847103b 100644 --- a/src/module/time.h +++ b/src/module/time.h @@ -9,12 +9,9 @@ namespace module /// class time:public module::imodule { public: - /// - /// URL解码 - /// static uint64 now_msec(); static uint64 now_sec(); - + static std::string now_time(const std::string& format); static void regist(sol::state* lua); }; diff --git a/src/utils/logutils.cpp b/src/utils/logutils.cpp index a13a9b5..a52349f 100644 --- a/src/utils/logutils.cpp +++ b/src/utils/logutils.cpp @@ -3,7 +3,9 @@ #include "util/time.h" #include "util/codec.h" #include "util/file.h" +#ifdef _WIN32 #include +#endif #define DEBUG_INFO 0 void print(const std::string& type,const std::string& msg,const std::string& filepath, const std::string& func, int line,int color) { @@ -23,7 +25,11 @@ void print(const std::string& type,const std::string& msg,const std::string& fil } void LogUtils::success(const std::string& msg, const std::string& filepath, const std::string& func, int line) { - ::print("[SUCC ] ", msg, filepath, func, line, ylib::ConsoleTextColor::GREEN | FOREGROUND_INTENSITY); + ::print("[SUCC ] ", msg, filepath, func, line, ylib::ConsoleTextColor::GREEN + #ifdef _WIN32 + |FOREGROUND_INTENSITY + #endif + ); } void LogUtils::info(const std::string& msg, const std::string& filepath, const std::string& func, int line) @@ -35,7 +41,11 @@ void LogUtils::info(const std::string& msg, const std::string& filepath, const s void LogUtils::error(const std::string& msg, const std::string& filepath, const std::string& func, int line) { //#ifdef _DEBUG - ::print("[ERROR] ", msg, filepath, func, line, ylib::ConsoleTextColor::RED | FOREGROUND_INTENSITY); + ::print("[ERROR] ", msg, filepath, func, line, ylib::ConsoleTextColor::RED + #ifdef _WIN32 + | FOREGROUND_INTENSITY + #endif + ); //#else //#endif @@ -43,10 +53,18 @@ void LogUtils::error(const std::string& msg, const std::string& filepath, const void LogUtils::warn(const std::string& msg, const std::string& filepath, const std::string& func, int line) { - ::print("[WARN ] ", msg, filepath, func, line, ylib::ConsoleTextColor::YELLOW | FOREGROUND_INTENSITY); + ::print("[WARN ] ", msg, filepath, func, line, ylib::ConsoleTextColor::YELLOW + #ifdef _WIN32 + | FOREGROUND_INTENSITY + #endif + ); } void LogUtils::debug(const std::string& msg, const std::string& filepath, const std::string& func,int line) { - ::print("[DEBUG] ", msg, filepath, func, line, ylib::ConsoleTextColor::BLUE | FOREGROUND_INTENSITY); + ::print("[DEBUG] ", msg, filepath, func, line, ylib::ConsoleTextColor::BLUE + #ifdef _WIN32 + | FOREGROUND_INTENSITY + #endif + ); }