This project is a simple multithreaded HTTP server implemented in C++. It supports basic RESTful operations (GET, POST, PUT, PATCH, DELETE) on a JSON-based database, using a thread pool for concurrent request handling.
- Handles HTTP GET, POST, PUT, PATCH, and DELETE requests
- Serves static HTML and JSON responses
- Stores and manages data in a
database.jsonfile - Supports full and partial updates to database entries
- Logs all requests to
requests.json - Uses a thread pool for concurrent client handling
- Minimal dependencies (uses nlohmann/json for JSON parsing)
Returns a simple HTML welcome page.
Returns the contents of database.json as JSON. If the file does not exist or is empty, returns an error message.
Adds a new object to the database. The request body must be a JSON object (without an id field; IDs are auto-generated).
Example request body:
{
"name": "John Doe",
"email": "john@example.com"
}Replaces an existing object in the database by id. The request body must be a JSON object containing the id field and all fields to be set.
Example request body:
{
"id": 1,
"name": "Jane Doe",
"email": "jane@example.com"
}Partially updates an existing object in the database by id. The request body must be a JSON object containing the id field and only the fields to update.
Example request body:
{
"id": 1,
"email": "newemail@example.com"
}Deletes an object from the database by id. The request body must be a JSON object containing the id field.
Example request body:
{
"id": 1
}- g++ (with C++17 support)
- Linux environment
- nlohmann/json single-header file (
json.hppin project root)
You can build the server using the provided VS Code task or manually:
g++ -g -std=c++17 -Wall -Wextra -pedantic-errors -Weffc++ -Wno-unused-parameter -fsanitize=undefined,address *.cpp -o server -pthread./serverThe server listens on port 8080 by default.
server.cpp— Main server implementationthreadpool.h— Simple thread pool for concurrent request handlingjson.hpp— nlohmann/json single-header librarydatabase.json— JSON database (auto-created/updated)requests.json— Log of all HTTP requests
- The server is for educational/demo purposes and is not production-ready.
- Error handling is basic; malformed requests may not be fully supported.
- Only one route (
/database) is supported for POST, PUT, PATCH, and DELETE.
Feel free to extend the server with more features or endpoints!