diff --git a/.gitignore b/.gitignore index 01e8e83..1b7baf3 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ cmake-build-debug/ .idea/ example/build +dist/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index b24834b..fc55a14 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,8 @@ cmake_minimum_required(VERSION 3.21) -project(VWeb) +set(version 1.0) +project(VWeb VERSION ${version}) +set(project_lower vweb) + set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUGSOFT") @@ -22,9 +25,29 @@ set(SOURCE_FILES include_directories(${CMAKE_SOURCE_DIR}/) add_library(VWeb ${SOURCE_FILES}) -set(mode Release) -if (CMAKE_BUILD_TYPE STREQUAL "Debug") - set(mode Debug) -endif () -set(target_file ${CMAKE_SOURCE_DIR}/dist/libVWeb.${mode}.a) -add_custom_command(TARGET VWeb POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy $ ${target_file}) +include(GNUInstallDirs) +set(config_install_dir "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") +set(generated_dir "${CMAKE_CURRENT_BINARY_DIR}/generated") +set(version_config "${generated_dir}/${PROJECT_NAME}ConfigVersion.cmake") +set(project_config "${generated_dir}/${PROJECT_NAME}Config.cmake") + +install(FILES VWeb.h DESTINATION include/VWeb-${version}) +install(TARGETS ${PROJECT_NAME} + EXPORT ${PROJECT_NAME}-targets + LIBRARY DESTINATION lib/$ + ARCHIVE DESTINATION lib/$ +) +install(EXPORT ${PROJECT_NAME}-targets + DESTINATION lib/${PROJECT_NAME}-${version}) + +configure_file( + ${PROJECT_SOURCE_DIR}/pkg/${project_lower}-config.cmake.in + ${PROJECT_BINARY_DIR}/pkg/${project_lower}-config.cmake @ONLY) + +configure_file( + ${PROJECT_SOURCE_DIR}/${project_lower}-config-version.cmake.in + ${PROJECT_BINARY_DIR}/${project_lower}-config-version.cmake @ONLY) + +install(FILES ${PROJECT_BINARY_DIR}/pkg/${project_lower}-config.cmake + ${PROJECT_BINARY_DIR}/${project_lower}-config-version.cmake + DESTINATION lib/${PROJECT_NAME}-${version}) \ No newline at end of file diff --git a/Source/EPollManager.cpp b/Source/EPollManager.cpp index a8b6f19..3d76d38 100644 --- a/Source/EPollManager.cpp +++ b/Source/EPollManager.cpp @@ -30,6 +30,6 @@ bool EPollManager::UpdateEvents(int sock, uint32_t eventType) const { } int EPollManager::Wait(int maxEvents, epoll_event *events) const { - return epoll_wait(m_EpollID, events, maxEvents, 10); + return epoll_wait(m_EpollID, events, maxEvents, -1); } } // namespace VWeb \ No newline at end of file diff --git a/Source/InbuildMiddleWare.cpp b/Source/InbuildMiddleWare.cpp index 9d4f2d1..d6b068b 100644 --- a/Source/InbuildMiddleWare.cpp +++ b/Source/InbuildMiddleWare.cpp @@ -45,8 +45,8 @@ SessionManager::SessionManager() { m_Counter = -1; } m_Counter++; + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); } - std::this_thread::sleep_for(std::chrono::milliseconds(1000)); }); } SessionManager::~SessionManager() { @@ -106,7 +106,7 @@ PreMiddleWareReturn CookieManager::PreHandle(Request &request) { auto &cookies = request.CookieData; if (cookieHeaders.Size() > 0) { auto &values = cookieHeaders.Values(); - for (auto &rawCookie : cookieHeaders.Values()) { + for (auto &rawCookie : values) { auto splitCookies = String::Split(rawCookie, ";"); for (auto &cookie : splitCookies) { auto split = String::Split(cookie, "="); diff --git a/Source/Server.cpp b/Source/Server.cpp index 59327e2..02f7268 100644 --- a/Source/Server.cpp +++ b/Source/Server.cpp @@ -8,7 +8,7 @@ Server::Server() { m_ServerConfig->EPoll = CreateRef(); m_ServerConfig->Socket = CreateRef(m_ServerConfig); m_RequestHandler = CreateRef(m_ServerConfig->Socket); - auto& middleWare = m_RequestHandler->Middleware(); + auto &middleWare = m_RequestHandler->Middleware(); middleWare->Create(); middleWare->Create(); middleWare->Create(); diff --git a/Source/ThreadPool.cpp b/Source/ThreadPool.cpp index 7748058..1ad0e31 100644 --- a/Source/ThreadPool.cpp +++ b/Source/ThreadPool.cpp @@ -1,8 +1,9 @@ #include #include +#include namespace VWeb { -ThreadPool::ThreadPool(const std::string &name) : m_Name(name) {} +ThreadPool::ThreadPool(std::string name) : m_Name(std::move(name)) {} void ThreadPool::Create() { if (m_IsCreated) return; @@ -10,9 +11,10 @@ void ThreadPool::Create() { m_IsCreated = true; m_Queue.Open(); for (int i = 0; i < m_ThreadCount; ++i) { - m_Threads.push_back(std::thread(&ThreadPool::Execute, this)); - }; - printf("[ThreadPool] >> Created %d Threads for Pool \"%s\"\n", m_ThreadCount, m_Name.c_str()); + m_Threads.emplace_back(&ThreadPool::Execute, this); + } + printf("[ThreadPool] >> Created %d Threads for Pool \"%s\"\n", m_ThreadCount, + m_Name.c_str()); } void ThreadPool::Stop() { m_IsDone = true; diff --git a/VWeb.h b/VWeb.h index f5681ce..42e2790 100644 --- a/VWeb.h +++ b/VWeb.h @@ -164,7 +164,7 @@ struct WorkerJob { }; class ThreadPool { public: - explicit ThreadPool(const std::string &name); + explicit ThreadPool(std::string name); void SetThreadCount(int); void Dispatch(const Ref &); void Create(); diff --git a/dist/libVWeb.Debug.a b/dist/libVWeb.Debug.a deleted file mode 100644 index b4002cb..0000000 Binary files a/dist/libVWeb.Debug.a and /dev/null differ diff --git a/dist/libVWeb.Release.a b/dist/libVWeb.Release.a deleted file mode 100644 index 64dfcf7..0000000 Binary files a/dist/libVWeb.Release.a and /dev/null differ diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index b14eeff..d4901ce 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -7,9 +7,15 @@ add_executable(VWeb_Example main.cpp) include_directories(${CMAKE_SOURCE_DIR}/..) -set(mode release) +set(mode Release) if (CMAKE_BUILD_TYPE STREQUAL "Debug") - set(mode debug) + set(mode Debug) endif () set(vweb_lib ${CMAKE_SOURCE_DIR}/../dist/libVWeb.${mode}.a) + +SET_SOURCE_FILES_PROPERTIES( + main.cpp + PROPERTIES OBJECT_DEPENDS ${vweb_lib} +) + target_link_libraries(VWeb_Example Threads::Threads ${vweb_lib}) diff --git a/example/main.cpp b/example/main.cpp index 59584d6..de24dd5 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -23,6 +23,8 @@ int main() { using namespace VWeb; VWeb::Server server; auto& router = server.GetRouter(); + // For debugging and profiling more than 1 thread can be hard. + server.GetServerConfig()->WorkerThreads = 1; router->Get("/test", [](const Request&, Response& response) { response << "NICE"; return true; diff --git a/pkg/vweb-config.cmake.in b/pkg/vweb-config.cmake.in new file mode 100644 index 0000000..99b32e2 --- /dev/null +++ b/pkg/vweb-config.cmake.in @@ -0,0 +1,9 @@ +# Compute installation prefix relative to this file. +get_filename_component(_dir "${CMAKE_CURRENT_LIST_FILE}" PATH) +get_filename_component(_prefix "${_dir}/../.." ABSOLUTE) + +# Import the targets. +include("${_prefix}/lib/VWeb-@version@/VWeb-targets.cmake") + +# Report other information. +set(VWeb_INCLUDE_DIRS "${_prefix}/include/VWeb-@version@") \ No newline at end of file diff --git a/vweb-config-version.cmake.in b/vweb-config-version.cmake.in new file mode 100644 index 0000000..4c0088b --- /dev/null +++ b/vweb-config-version.cmake.in @@ -0,0 +1,7 @@ +set(PACKAGE_VERSION "@version@") +if(NOT "${PACKAGE_FIND_VERSION}" VERSION_GREATER "@version@") + set(PACKAGE_VERSION_COMPATIBLE 1) # compatible with older + if("${PACKAGE_FIND_VERSION}" VERSION_EQUAL "@version@") + set(PACKAGE_VERSION_EXACT 1) # exact match for this version + endif() +endif() \ No newline at end of file