Yet Another HTTP API Thing - A trivial HTTP server for simple REST APIs and other HTTP interfaces in C++ projects.
This project contains a very simple HTTP server-as-a-library to make it easy to embed an HTTP/REST API in a C++ project. In recent years, I have found myself re-inventing this wheel a little too frequently when writing microservices or other C++ projects that require an HTTP interface.
- Can be used to implement a simple embedded HTTP server
- Designed to be used as an embedded API server using the HTTP protocol
- Supports HTTP and HTTPS
- Native support for Server-Sent Events (SSE)
Compile-time switch to enable metrics using the OpenMetrics format.
If enabled, the server provides a /metrics endpoint and updates relevant
metrics, such as the number of HTTP requests. You can use the metrics functionality
directly in your application to add your own metrics.
This is demonstrated in the chatserver example.
The metrics feature is primarily designed for C++ applications to export their own metrics.
Supported metric types:
- Counter
- Gauge
- Info
- Histogram
- Summary
- Stateset
- Untyped
Metrics can be scraped by Prometheus and most other metrics collectors.
You can derive your own metrics class from yahat::Metrics::DataType and
add it to the metrics by calling Metrics::AddUntyped<YourClass>().
- Linux (Tested on Ubuntu)
- MacOS
- Windows
- C++20.
- Boost 1.82 or later
- zlib
- openssl
- gtest (if testing is enabled with CMake option
YAHAT_WITH_TESTS)
yahat-cpp is designed to be easily consumed by other CMake projects using modern CMake practices. You can integrate it using any of the following methods:
First, install yahat-cpp:
git clone https://github.com/jgaa/yahat-cpp.git
cd yahat-cpp
mkdir build && cd build
cmake ..
make -j4
sudo make installThen in your CMakeLists.txt:
find_package(yahat-cpp REQUIRED)
target_link_libraries(your_target yahat-cpp::yahat-cpp)include(FetchContent)
FetchContent_Declare(yahat-cpp
GIT_REPOSITORY https://github.com/jgaa/yahat-cpp.git
GIT_TAG main # or a specific tag/commit
)
# Optional: disable tests and examples
set(YAHAT_WITH_TESTS OFF)
set(YAHAT_WITH_EXAMPLES OFF)
FetchContent_MakeAvailable(yahat-cpp)
target_link_libraries(your_target yahat-cpp::yahat-cpp)If you have yahat-cpp as a subdirectory in your project:
# Optional: disable tests and examples
set(YAHAT_WITH_TESTS OFF)
set(YAHAT_WITH_EXAMPLES OFF)
add_subdirectory(path/to/yahat-cpp)
target_link_libraries(your_target yahat-cpp::yahat-cpp)Your project will also need to find the dependencies that yahat-cpp requires:
find_package(Boost REQUIRED COMPONENTS system program_options coroutine context chrono json url)
find_package(OpenSSL REQUIRED)
find_package(ZLIB REQUIRED)
find_package(Threads REQUIRED)#include "yahat/HttpServer.h"
#include <iostream>
int main() {
yahat::HttpConfig config;
config.http_endpoint = "0.0.0.0";
config.http_port = "8080";
yahat::HttpServer server(config);
// Add your request handlers here
server.start().wait();
return 0;
}This is a simple chat server that uses HTTP Server-Sent Events (SSE) to send chat events to the participants. I wrote this example because I wanted a simple chat server running locally on my network to quickly and securely pass information between local computers and phones/tablets.
By default (on log-level info), the server does not log or store any messages. When a message arrives, it is sent to all participants and then discarded.
The chat app running in the web browser uses JavaScript. However, it is kept to an
absolute minimum, and it does not use any JavaScript libraries or external resources.
It use only the app's own index.html file.
If can run docker containers on your machine, there is an official docker image available for yahatchat.
You can run it locally on port 8000 with this command. Since we are binding to 0.0.0.0, it will be abailable for all the machines on your local network. If you just want to test it locally, use 127.0.0.1 instead.
docker run --rm -d --name yahatchat -p 0.0.0.0:8000:8080 ghcr.io/jgaa/yahatchatBeta