Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM mcr.microsoft.com/devcontainers/cpp:1-debian-11

ARG REINSTALL_CMAKE_VERSION_FROM_SOURCE="none"

# Optionally install the cmake for vcpkg
COPY ./reinstall-cmake.sh /tmp/

RUN if [ "${REINSTALL_CMAKE_VERSION_FROM_SOURCE}" != "none" ]; then \
chmod +x /tmp/reinstall-cmake.sh && /tmp/reinstall-cmake.sh ${REINSTALL_CMAKE_VERSION_FROM_SOURCE}; \
fi \
&& rm -f /tmp/reinstall-cmake.sh

# [Optional] Uncomment this section to install additional vcpkg ports.
# RUN su vscode -c "${VCPKG_ROOT}/vcpkg install <your-port-name-here>"

# [Optional] Uncomment this section to install additional packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-package-list-here>
26 changes: 26 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/cpp
{
"name": "C++",
"build": {
"dockerfile": "Dockerfile"
},
"features": {
"ghcr.io/devcontainers-contrib/features/postgres-asdf:1": {}
}

// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],

// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "gcc -v",

// Configure tool-specific properties.
// "customizations": {},

// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
59 changes: 59 additions & 0 deletions .devcontainer/reinstall-cmake.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env bash
#-------------------------------------------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See https://go.microsoft.com/fwlink/?linkid=2090316 for license information.
#-------------------------------------------------------------------------------------------------------------
#
set -e

CMAKE_VERSION=${1:-"none"}

if [ "${CMAKE_VERSION}" = "none" ]; then
echo "No CMake version specified, skipping CMake reinstallation"
exit 0
fi

# Cleanup temporary directory and associated files when exiting the script.
cleanup() {
EXIT_CODE=$?
set +e
if [[ -n "${TMP_DIR}" ]]; then
echo "Executing cleanup of tmp files"
rm -Rf "${TMP_DIR}"
fi
exit $EXIT_CODE
}
trap cleanup EXIT


echo "Installing CMake..."
apt-get -y purge --auto-remove cmake
mkdir -p /opt/cmake

architecture=$(dpkg --print-architecture)
case "${architecture}" in
arm64)
ARCH=aarch64 ;;
amd64)
ARCH=x86_64 ;;
*)
echo "Unsupported architecture ${architecture}."
exit 1
;;
esac

CMAKE_BINARY_NAME="cmake-${CMAKE_VERSION}-linux-${ARCH}.sh"
CMAKE_CHECKSUM_NAME="cmake-${CMAKE_VERSION}-SHA-256.txt"
TMP_DIR=$(mktemp -d -t cmake-XXXXXXXXXX)

echo "${TMP_DIR}"
cd "${TMP_DIR}"

curl -sSL "https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/${CMAKE_BINARY_NAME}" -O
curl -sSL "https://github.com/Kitware/CMake/releases/download/v${CMAKE_VERSION}/${CMAKE_CHECKSUM_NAME}" -O

sha256sum -c --ignore-missing "${CMAKE_CHECKSUM_NAME}"
sh "${TMP_DIR}/${CMAKE_BINARY_NAME}" --prefix=/opt/cmake --skip-license

ln -s /opt/cmake/bin/cmake /usr/local/bin/cmake
ln -s /opt/cmake/bin/ctest /usr/local/bin/ctest
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
http_test: http_test.cpp
clang++ -o http_test -std=c++17 http_test.cpp -I$$(pg_config --includedir) $$(pg_config --ldflags) -lpq -lpthread
clang++ -o http_test -std=c++17 http_test.cpp -I$$(pg_config --includedir) $$(pg_config --ldflags) -L$$(pg_config --libdir) -lpq -lpthread

fmt:
clang-format -i http_test.cpp
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ Environment variables are your friends:
* `SQL_QUERY` is the SQL query that will be executed when accessing the `/tx`
endpoint.

* `KEEP_ALIVE_MAX_COUNT` TCP keepalive configuration

* `KEEP_ALIVE_TIMEOUT` TCP keepalive configuration

* `READ_TIMEOUT` HTTP request handling read timeout

* `WRITE_TIMEOUT` HTTP request handling write timeout

* `IDLE_INTERVAL` HTTP request timeout idle interval

The endpoint `/.well-known/check` will always return "ok".

## Other considerations
Expand Down
29 changes: 29 additions & 0 deletions http_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ void ok(const httplib::Request &req, httplib::Response &res);
void logger(const httplib::Request &req, const httplib::Response &res);
std::string get_configuration(const std::string &env_name,
const std::string &default_value = "");
void set_server_option_from_env(const std::string& env_name, std::function<void(int)>setter);

/**
* The database URL to be used as a libpq connection string
Expand All @@ -45,11 +46,39 @@ int main() {
httplib::Server svr;
svr.Get("/tx", tx);
svr.Get("/.well-known/check", ok);

// Keepalive settings
set_server_option_from_env("KEEP_ALIVE_MAX_COUNT", [&svr](int value){ svr.set_keep_alive_max_count(value); });
set_server_option_from_env("KEEP_ALIVE_TIMEOUT", [&svr](int value){ svr.set_keep_alive_timeout(value); });

// Timeout settings
set_server_option_from_env("READ_TIMEOUT", [&svr](int value){ svr.set_read_timeout(value); });
set_server_option_from_env("WRITE_TIMEOUT", [&svr](int value){ svr.set_write_timeout(value); });
set_server_option_from_env("IDLE_INTERVAL", [&svr](int value){ svr.set_idle_interval(value); });

svr.listen("0.0.0.0", 8080);

return 0;
}

/**
* This is an helper function to set an HTTP server property from
* an environment variable.
* The property to be set is an integer.
*/
void set_server_option_from_env(const std::string& env_name, std::function<void(int)>setter) {
const std::string env_value = get_configuration(env_name);

if (!env_value.empty()) {
try {
std::cout << env_name << ": " << std::quoted(env_value) << std::endl;
setter(std::stoi(env_value));
} catch(std::exception &e) {
std::cout << "Error parsing " << env_name << " as an integer" << std::endl;
}
}
}

/**
* This is a context class that to be used inside a request handler. It will
* log the request handling time
Expand Down
Loading