Skip to content

Commit

Permalink
reworked CMake lists and allowed installing, changed the TCP stream t…
Browse files Browse the repository at this point in the history
…imeout to 30 seconds
  • Loading branch information
catink123 committed Jan 7, 2024
1 parent d2c0930 commit f3d19df
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 32 deletions.
29 changes: 5 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
cmake_minimum_required(VERSION 3.14 FATAL_ERROR)

set(GATE_CONTROL_PROJECT_NAME GateControl)
set(GATE_CONTROL_SOURCE_DIR src)

set(GATE_CONTROL_SOURCE
${GATE_CONTROL_SOURCE_DIR}/common.hpp
${GATE_CONTROL_SOURCE_DIR}/main.cpp
${GATE_CONTROL_SOURCE_DIR}/http_listener.hpp
${GATE_CONTROL_SOURCE_DIR}/http_listener.cpp
${GATE_CONTROL_SOURCE_DIR}/http_session.hpp
${GATE_CONTROL_SOURCE_DIR}/http_session.cpp
project(
GateControl
VERSION 1.0.0
LANGUAGES CXX
)

include("cmake/modules/CPM.cmake")
Expand All @@ -29,17 +23,4 @@ CPMAddPackage(
"BOOST_ENABLE_CMAKE ON"
)

project(${GATE_CONTROL_PROJECT_NAME})

add_executable(${GATE_CONTROL_PROJECT_NAME} ${GATE_CONTROL_SOURCE})

set_property(TARGET ${GATE_CONTROL_PROJECT_NAME} PROPERTY CXX_STANDARD 17)

# link libraries

target_include_directories(${GATE_CONTROL_PROJECT_NAME} PRIVATE ${BOOST_LIBRARIES})

target_link_libraries(
${GATE_CONTROL_PROJECT_NAME} PRIVATE
Boost::beast nlohmann_json::nlohmann_json
)
add_subdirectory(${PROJECT_SOURCE_DIR}/src)
48 changes: 46 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ $ git clone https://github.com/catink123/gate-control
$ cd gate-control
```

Then, run CMake (version >= 3.14) in the repo's root directory with one of the provided presets:
Then, configure and build the project using CMake (version >= 3.14) in the repo's root directory with one of the provided presets:

```sh
$ cmake --preset <build-preset>
$ cmake --build --preset <build-preset>
```

Expand All @@ -31,7 +32,50 @@ After CMake successfully builds the server, the binaries should be located in th

### Release build

You can obtain prebuilt binaries from the [Releases tab](https://github.com/catink123/gate-control/releases).
You can obtain prebuilt binaries from the [Releases](https://github.com/catink123/gate-control/releases) section.

## Installing

As in the Building section, there are two ways of installing the server: installing from source and downloading from Releases section.

### Installing from source

First, clone the repo to your local machine:

```sh
$ git clone https://github.com/catink123/gate-control
$ cd gate-control
```

Then, configure and install the project using CMake (version >= 3.14) in the repo's root directory with one of the provided presets:

```sh
$ cmake --preset <build-preset>
$ cmake --build --preset <build-preset> --target install
```

After CMake successfully builds the server, installed binaries should be located in the `out/install/<build-preset>` directory.

Alternatively, if you want to use another directory to install to, specify the `CMAKE_INSTALL_PREFIX` variable in the configure (the first) command to point to your preferred install directory:

```sh
$ cmake --preset <build-preset> -DCMAKE_INSTALL_PREFIX=<preferred-install-dir>
```

Substitute `<preferred-install-dir>` with your preferred directory, wrapping the path in quotes if it contains whitespaces.

For example, on Windows, if you want to install the server to `C:/Program Files/GateControl`, you need to run these commands:

```sh
$ cmake --preset windows-x64-release -DCMAKE_INSTALL_PREFIX="C:/Program Files/GateControl"
$ cmake --build --preset windows-x64-release --target install
```

You might need administrative privileges to install to protected paths like `C:/Program Files` on Windows.

### Downloading prebuilt binaries

Instead of building manually you have the option to download a prebuilt release. To do so, head on over to the [Releases](https://github.com/catink123/gate-control/releases) section and download the latest one.

## Architecture

Expand Down
39 changes: 39 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/version.hpp.in ${CMAKE_CURRENT_SOURCE_DIR}/version.hpp @ONLY)

add_executable(
${PROJECT_NAME}
main.cpp
version.hpp
common.hpp
http_listener.hpp
http_listener.cpp
http_session.hpp
http_session.cpp
)

set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 17)

# boost windows-specific setting
if (WIN32)
target_compile_definitions(${PROJECT_NAME} PRIVATE _WIN32_WINNT=0x0601)
endif()

target_link_libraries(
${PROJECT_NAME}
Boost::beast
nlohmann_json::nlohmann_json
)

# install server binary to bin

install(
TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin
)

# install the client webapp to bin/client

install(
DIRECTORY client
DESTINATION bin
)
1 change: 1 addition & 0 deletions src/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <boost/asio/strand.hpp>
#include <boost/config.hpp>
#include <iostream>
#include "version.hpp"

namespace beast = boost::beast;
namespace http = beast::http;
Expand Down
12 changes: 6 additions & 6 deletions src/http_session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void http_session::do_read() {
// clear the request
req = {};

stream.expires_after(std::chrono::seconds(3));
stream.expires_after(std::chrono::seconds(30));

http::async_read(stream, buffer, req,
beast::bind_front_handler(
Expand Down Expand Up @@ -147,7 +147,7 @@ http::message_generator handle_request(
req.version()
};

res.set(http::field::server, "Gate Control Server");
res.set(http::field::server, VERSION);
res.set(http::field::content_type, "text/html");
res.keep_alive(req.keep_alive());
res.body() = std::string(why);
Expand All @@ -163,7 +163,7 @@ http::message_generator handle_request(
req.version()
};

res.set(http::field::server, "Gate Control Server");
res.set(http::field::server, VERSION);
res.set(http::field::content_type, "text/html");
res.keep_alive(req.keep_alive());
res.body() = "The resource '" + std::string(target) + "' was not found.";
Expand All @@ -179,7 +179,7 @@ http::message_generator handle_request(
req.version()
};

res.set(http::field::server, "Gate Control Server");
res.set(http::field::server, VERSION);
res.set(http::field::content_type, "text/html");
res.keep_alive(req.keep_alive());
res.body() = "An error occured: '" + std::string(what) + "'";
Expand Down Expand Up @@ -236,7 +236,7 @@ http::message_generator handle_request(
req.version()
};

res.set(http::field::server, "Gate Control Server");
res.set(http::field::server, VERSION);
res.set(http::field::content_type, mime_type(path));
res.content_length(size);
res.keep_alive(req.keep_alive());
Expand All @@ -251,7 +251,7 @@ http::message_generator handle_request(
std::make_tuple(http::status::ok, req.version())
};

res.set(http::field::server, "Gate Control Server");
res.set(http::field::server, VERSION);
res.set(http::field::content_type, mime_type(path));
res.content_length(size);
res.keep_alive(req.keep_alive());
Expand Down
6 changes: 6 additions & 0 deletions src/version.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef VERSION_HPP
#define VERSION_HPP

#define VERSION "GateControl v1.0.0"

#endif
6 changes: 6 additions & 0 deletions src/version.hpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef VERSION_HPP
#define VERSION_HPP

#define VERSION "@PROJECT_NAME@ v@PROJECT_VERSION@"

#endif

0 comments on commit f3d19df

Please sign in to comment.