Skip to content
Merged
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ set(TEST_TARGET "server_tests_bin")
set(TARGET "server_impl_bin")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

find_package(GTest REQUIRED)
find_package(GTest CONFIG REQUIRED)
find_package(Threads REQUIRED)


Expand Down
38 changes: 38 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
FROM ubuntu:24.04
LABEL authors="james"


RUN apt-get update && apt-get install -y \
build-essential \
gcc \
g++ \
cmake \
git \
python3 \
python3-pip \
python3-venv \
lcov \
pkg-config \
&& rm -rf /var/lib/apt/lists/*

# Create a venv for Conan
RUN python3 -m venv /opt/venv
# Activate venv
ENV PATH="/opt/venv/bin:$PATH"

# Install Conan inside the venv
RUN pip install --upgrade pip \
&& pip install conan

# Configure Conan default profile
RUN conan profile detect --force

# Set working dir and copy project files
WORKDIR /app
COPY --exclude=build/ . .

# Build with Conan
RUN conan build . --build=missing


CMD ["/bin/bash"]
52 changes: 40 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ A modern C++ HTTP server implementation with routing, testing, and CI-driven qua
* [Features](#features)
* [Usage](#usage)
* [Project Structure](#project-structure)
* [Using Docker](#using-docker)
* [Using Nix](#using-nix)
* [Building the Source Code](#building-the-source-code)

* [Building for Release Mode](#building-for-release-mode)
* [Running the Server](#running-the-server)
* [Running the Tests](#running-the-tests)
Expand All @@ -33,6 +34,8 @@ A modern C++ HTTP server implementation with routing, testing, and CI-driven qua
* Build verification
* Automated test execution
* Coverage report generation
* **Docker** for reproducible builds and containerization
* **Nix flake** for reproducible development environments
* **Project tracking** using GitHub Projects (Kanban board)

---
Expand All @@ -45,20 +48,24 @@ A modern C++ HTTP server implementation with routing, testing, and CI-driven qua
HttpServer server{};

server.get_mapping(
"/test", [](const HttpServer::Request &, HttpServer::Response &res) {
"/test", [](HttpServer::Request &req, HttpServer::Response &res) {
res.body = "testing new api route";
});

server.post_mapping(
"/test2/{id}", [](const HttpServer::Request &, HttpServer::Response &res) {
std::stringstream ss;
try {
ss << req.path_params.get_path_param("id").value() << "\n";
res.body = ss.str();
} catch (const std::bad_optional_access &e) {
res.body = "could not get path parameter foo";
}
});
server.post_mapping(
"/test2/{id}/foo/{user}",
[](HttpServer::Request &req, HttpServer::Response &res) {
std::stringstream ss;
try {
ss << "{\"id\":" << "\"" << req.path_params.get_path_param("id").value() << "\","
<< "\"user\":" << "\"" << req.path_params.get_path_param("user").value() << "\""
<< "}";
res.body = ss.str();
} catch (const std::bad_optional_access &e) {
res.body = "could not get path parameter";
}
});


try {
server.listen(3490); // use any port you like
Expand All @@ -82,6 +89,27 @@ try {

---

## Using Docker

```bash
# build the docker image
docker build -t cpp_http_server .

# run a container in interactive mode
# discards after use
docker run --rm -it cpp_http_server
```

---

## Using Nix

The `flake.nix` can only be used if your system has the Nix package manager with flakes enabled.

```bash
nix develop
```

## Building the Source Code

This project uses **Conan** for dependency management and **CMake** for builds.
Expand Down
27 changes: 27 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
description = "A C/C++ development environment";

inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11";
};

outputs = {nixpkgs, ...}: let
system = "x86_64-linux";
pkgs = import nixpkgs { inherit system; };
in {
devShells.${system}.default = pkgs.mkShell {
packages = with pkgs; [
git

gcc
lcov
gdb

# If you want to use clang and llvm-cov instead of gcc, uncomment
# clang
# llvmPackages.llvm

gnumake
cmake
conan

python312
];
};
};
}
19 changes: 14 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ int main() {
HttpServer server{};

server.get_mapping(
"/test", [](const HttpServer::Request &, HttpServer::Response &res) {
"/test", [](HttpServer::Request &req, HttpServer::Response &res) {
res.body = "testing new api route";
});

server.get_mapping(
"/test2", [](const HttpServer::Request &, HttpServer::Response &res) {
res.body = "this is the other route";
server.post_mapping(
"/test2/{id}/foo/{user}",
[](HttpServer::Request &req, HttpServer::Response &res) {
std::stringstream ss;
try {
ss << "{\"id\":" << "\"" << req.path_params.get_path_param("id").value() << "\","
<< "\"user\":" << "\"" << req.path_params.get_path_param("user").value() << "\""
<< "}";
res.body = ss.str();
} catch (const std::bad_optional_access &e) {
res.body = "could not get path parameter";
}
});

try {
Expand All @@ -21,4 +30,4 @@ int main() {
std::cerr << err.what() << '\n';
return EXIT_FAILURE;
}
}
}
Loading