A simple HTTP server with REST API, JSON support, and non-blocking sockets for Linux.
- ✅ Non-blocking sockets with epoll
- ✅ REST API supporting GET, POST, PUT, DELETE
- ✅ JSON parsing and generation
- ✅ Multithreaded request handling
- ✅ Simple in-memory data storage
- Linux (Ubuntu/Debian/CentOS/etc.)
- GCC 7+ or Clang 6+
- CMake 3.10+
- pthread library
Ubuntu/Debian:
sudo apt update
sudo apt install build-essential cmakeCentOS/RHEL:
sudo yum groupinstall "Development Tools"
sudo yum install cmake# Create build directory
mkdir build
cd build
# Configure the project
cmake ..
# Compile
make
# Or build in release mode
cmake -DCMAKE_BUILD_TYPE=Release ..
make# From the build directory
./bin/rest_server
# Or using make target
make runThe server will start on port 8080.
Get all data
curl -X GET http://localhost:8080/api/dataGet a specific item
curl -X GET http://localhost:8080/api/data/mykeyCreate new data (JSON in request body)
curl -X POST http://localhost:8080/api/data \
-H "Content-Type: application/json" \
-d '{"name": "John", "city": "Kiev"}'Update an item
curl -X PUT http://localhost:8080/api/data/name \
-H "Content-Type: application/json" \
-d '{"value": "Jane"}'Delete an item
curl -X DELETE http://localhost:8080/api/data/name# Create a user
curl -X POST http://localhost:8080/api/data \
-H "Content-Type: application/json" \
-d '{"username": "user123", "email": "user@example.com"}'
# Response: {"message":"Data saved successfully"}# Get all data
curl -X GET http://localhost:8080/api/data
# Response: {"username":"user123","email":"user@example.com"}
# Get a specific field
curl -X GET http://localhost:8080/api/data/username
# Response: {"username":"user123"}# Update email
curl -X PUT http://localhost:8080/api/data/email \
-H "Content-Type: application/json" \
-d '{"value": "newemail@example.com"}'
# Response: {"message":"Data updated successfully"}# Delete user
curl -X DELETE http://localhost:8080/api/data/username
# Response: {"message":"Data deleted successfully"}-
RestApiServer - main server class
- Socket and epoll management
- Handling incoming connections
- Request routing
-
HttpRequest - HTTP request parsing
- Extracting method, path, headers
- Parsing request body
-
HttpResponse - HTTP response formation
- Setting status codes
- Managing headers
- Serializing response
-
JsonParser - simple JSON parser
- Parsing JSON strings into map
- Generating JSON from map
The server uses:
- epoll for monitoring multiple file descriptors
- Non-blocking sockets to prevent blocking
- Edge-triggered mode for efficient event handling
- Data storage is protected by a mutex
- Each request is handled atomically
- Safe access to shared state
In the main.cpp file, change the line:
if (!server.start(8080)) { // Replace 8080 with the desired portAdd handling in the handle*Request methods:
if (path == "/api/newendpoint") {
// Your logic
}The current parser is simple. For production, consider:
- nlohmann/json
- RapidJSON
- jsoncpp
For production, add:
- Structured logging
- Log rotation
- Different log levels
- Compile with optimization flags (
-O2,-O3) - Use static linking if needed
- Profile with
perforvalgrind
# Using Apache Bench
ab -n 1000 -c 10 http://localhost:8080/api/data
# Using wrk
wrk -t4 -c100 -d30s http://localhost:8080/api/datacmake -DCMAKE_BUILD_TYPE=Debug ..
makegdb ./bin/rest_server
(gdb) runvalgrind --leak-check=full ./bin/rest_server- Simple JSON parser (string fields only)
- In-memory storage (data not persisted)
- No SSL/TLS support
- Basic error handling
- HTTPS support
- Database integration
- More advanced JSON parser
- Middleware system
- Configuration files
- Metrics and monitoring
- Rate limiting
- Authentication and authorization
MIT License