diff --git a/.gitignore b/.gitignore index c8d5ae4..022a844 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ pgsql x64 /WebApp/WebApp/log /config.json +/build # Prerequisites *.d diff --git a/BUILD-Windows.md b/BUILD-Windows.md new file mode 100644 index 0000000..06f5bf0 --- /dev/null +++ b/BUILD-Windows.md @@ -0,0 +1,7 @@ +# Build + +*Refer to [readme](dependencies/README-Windows.md) for setting up the dependencies.* + +Use VS2019 to open `WebApp/WebApp.sln`, which is a sample project. Remember to properly configure the database and `config.json` before you `Run` the project. + +> `bserv` and `WebApp` should be built in `Debug` or `Release` (`x64`), NOT (`Win32`/`x86`). diff --git a/BUILD-ubuntu.md b/BUILD-ubuntu.md new file mode 100644 index 0000000..098bfbe --- /dev/null +++ b/BUILD-ubuntu.md @@ -0,0 +1,22 @@ +# Build + +*Refer to [readme](dependencies/README-ubuntu.md) for setting up the dependencies.* + +shell: +``` +mkdir build && cd build +cmake .. +cmake --build . +``` + +Assuming the shell is in `build`, you can run `WebApp` using: +``` +cd WebApp/WebApp +./WebApp ../../../config.json +``` +given that the database and `config.json` are properly configured. + +> If some of the dynamically linked libraries are missing, try: +> ``` +> sudo ldconfig +> ``` diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..62777cc --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.10) + +project(bserv_project) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release) +endif() + +link_directories( + dependencies/cryptopp + dependencies/libpqxx/src/.libs +) + +add_subdirectory(WebApp/bserv) +add_subdirectory(WebApp/WebApp) + diff --git a/README.md b/README.md index fd9e79b..ba7d6db 100644 --- a/README.md +++ b/README.md @@ -3,20 +3,14 @@ *A Boost Based C++ HTTP JSON Server.* -## Dependencies - -- VS2019 -- CMake -- PostgreSQL - > *The database may not be installed locally. You should be able to connect to it.* - -*Refer to [readme](dependencies/README.md) for setting up the dependencies.* - - ## Quick Start -Use VS2019 to open `WebApp/WebApp.sln`, which is a sample project. [`config-example.json`](config-example.json) is a sample config file for `WebApp`'s startup parameters. **It should be renamed to `config.json` before you `Run` the project.** +> To build the dependencies & the project, refer to [`BUILD-Windows.md`](BUILD-Windows.md) or [`BUILD-ubuntu.md`](BUILD-ubuntu.md) +- `WebApp/bserv` contains the source code for `bserv`. +- `WebApp/WebApp` is a sample project. +- [`config-Windows.json`](config-Windows.json) and [`config-ubuntu.json`](config-ubuntu.json) are two sample config file for `WebApp`'s startup parameters. **It should be configured and renamed to `config.json` before you start `WebApp`.** +- To use `WebApp`, you should setup the database as well. ### Database diff --git a/WebApp/WebApp/CMakeLists.txt b/WebApp/WebApp/CMakeLists.txt new file mode 100644 index 0000000..4e4cf7d --- /dev/null +++ b/WebApp/WebApp/CMakeLists.txt @@ -0,0 +1,21 @@ +add_executable( + WebApp + + handlers.cpp + rendering.cpp + WebApp.cpp +) + +target_include_directories( + WebApp PUBLIC + + ../../dependencies/inja/include + ../../dependencies/inja/third_party/include +) + +target_link_libraries( + WebApp PUBLIC + + bserv +) + diff --git a/WebApp/WebApp/WebApp.cpp b/WebApp/WebApp/WebApp.cpp index 3c446c1..1f0dcae 100644 --- a/WebApp/WebApp/WebApp.cpp +++ b/WebApp/WebApp/WebApp.cpp @@ -96,7 +96,7 @@ int main(int argc, char* argv[]) { bserv::placeholders::_1), // serving html template files - bserv::make_path("/", &index, + bserv::make_path("/", &index_page, bserv::placeholders::session, bserv::placeholders::response), bserv::make_path("/form_login", &form_login, diff --git a/WebApp/WebApp/handlers.cpp b/WebApp/WebApp/handlers.cpp index 51350e7..a000533 100644 --- a/WebApp/WebApp/handlers.cpp +++ b/WebApp/WebApp/handlers.cpp @@ -280,7 +280,7 @@ std::nullopt_t index( return render(response, template_path, context); } -std::nullopt_t index( +std::nullopt_t index_page( std::shared_ptr session_ptr, bserv::response_type& response) { boost::json::object context; diff --git a/WebApp/WebApp/handlers.h b/WebApp/WebApp/handlers.h index 87d4652..1b78311 100644 --- a/WebApp/WebApp/handlers.h +++ b/WebApp/WebApp/handlers.h @@ -47,7 +47,7 @@ std::nullopt_t serve_static_files( bserv::response_type& response, const std::string& path); -std::nullopt_t index( +std::nullopt_t index_page( std::shared_ptr session_ptr, bserv::response_type& response); diff --git a/WebApp/bserv/CMakeLists.txt b/WebApp/bserv/CMakeLists.txt new file mode 100644 index 0000000..b72d2f9 --- /dev/null +++ b/WebApp/bserv/CMakeLists.txt @@ -0,0 +1,36 @@ +add_library( + bserv + + pch.cpp + bserv.cpp + client.cpp + database.cpp + session.cpp + utils.cpp +) + +target_include_directories( + bserv PUBLIC + + ../../dependencies + ../../dependencies/libpqxx/include + include +) + +target_link_libraries( + bserv PUBLIC + + pthread + boost_thread + boost_context + boost_coroutine + boost_system + boost_filesystem + boost_log + boost_log_setup + boost_json + pqxx + pq + cryptopp +) + diff --git a/WebApp/bserv/include/bserv/common.hpp b/WebApp/bserv/include/bserv/common.hpp index 1c1cfdf..3be9e38 100644 --- a/WebApp/bserv/include/bserv/common.hpp +++ b/WebApp/bserv/include/bserv/common.hpp @@ -1,7 +1,9 @@ #ifndef _COMMON_HPP #define _COMMON_HPP +#ifdef _MSC_VER #define _WIN32_WINNT 0x0601 +#endif #include "client.hpp" #include "config.hpp" @@ -13,4 +15,4 @@ #include "utils.hpp" #include "websocket.hpp" -#endif // _COMMON_HPP \ No newline at end of file +#endif // _COMMON_HPP diff --git a/WebApp/bserv/include/bserv/logging.hpp b/WebApp/bserv/include/bserv/logging.hpp index fee95c1..babac4a 100644 --- a/WebApp/bserv/include/bserv/logging.hpp +++ b/WebApp/bserv/include/bserv/logging.hpp @@ -1,7 +1,9 @@ #ifndef _LOGGING_HPP #define _LOGGING_HPP -//#define BOOST_LOG_DYN_LINK +#if defined(__GNUC__) +#define BOOST_LOG_DYN_LINK +#endif #include #include @@ -54,4 +56,4 @@ namespace bserv { } // bserv -#endif // _LOGGING_HPP \ No newline at end of file +#endif // _LOGGING_HPP diff --git a/config-example.json b/config-Windows.json similarity index 100% rename from config-example.json rename to config-Windows.json diff --git a/config-ubuntu.json b/config-ubuntu.json new file mode 100644 index 0000000..51651ea --- /dev/null +++ b/config-ubuntu.json @@ -0,0 +1,8 @@ +{ + "port": 8080, + "thread-num": 2, + "conn-num": 4, + "conn-str": "postgresql://[username]:[password]@[url]:[port]/[db]", + "static_root": "../../../templates/statics", + "template_root": "../../../templates" +} diff --git a/dependencies/README.md b/dependencies/README-Windows.md similarity index 78% rename from dependencies/README.md rename to dependencies/README-Windows.md index 38d6924..68a44cd 100644 --- a/dependencies/README.md +++ b/dependencies/README-Windows.md @@ -1,6 +1,13 @@ # Dependencies +To build the dependencies & the project itself, The following must be installed: +- Microsoft Visual Studio 2019 (VS2019) +- CMake + +For each dependency, CMD is assumed to be in the directory of `dependencies`. So, the first command is always changing into the directory which contains the dependency. + + ## [Boost](https://www.boost.org/) CMD: @@ -22,6 +29,8 @@ b2 # [PostgreSQL 14.0](https://www.postgresql.org/) +*The database may not be installed locally. You should be able to connect to it.* + 1. Use this [link](https://get.enterprisedb.com/postgresql/postgresql-14.0-1-windows-x64-binaries.zip) to download the binaries. 2. Unzip the zip archive here. It should be named `pgsql` and contains `bin`, `include` and `lib`. diff --git a/dependencies/README-ubuntu.md b/dependencies/README-ubuntu.md new file mode 100644 index 0000000..5414379 --- /dev/null +++ b/dependencies/README-ubuntu.md @@ -0,0 +1,46 @@ +# Dependencies (Ubuntu) + + +To build the dependencies & the project itself, The following must be installed: +- g++ +- make +- CMake + +For each dependency, shell is assumed to be in the directory of `dependencies`. So, the first command is always changing into the directory which contains the dependency. + + +## [Boost](https://www.boost.org/) +shell: +``` +cd boost +./bootstrap +./b2 +sudo ./b2 install +``` + + +## [Crypto++](https://cryptopp.com/) +shell: + +``` +cd cryptopp +make +``` + + +# [PostgreSQL](https://www.postgresql.org/) +**The database should be installed locally.** + + +# [Libpqxx](https://github.com/jtv/libpqxx) +shell: +``` +cd libpqxx +./configure +make +``` + + +# [inja](https://github.com/pantor/inja) + +*Nothing should be done...*