APIServer2

Modern C++23 High-Performance Web API Server

About The Project

APIServer2 is a high-performance, multi-reactor EPOLL-based web server written in Modern C++23. Engineered from the ground up to handle massive concurrent loads with low latency, it serves as an ideal foundation for scalable and robust backend services.

Architecture Diagram

The system leverages the Linux Kernel's SO_REUSEPORT for efficient load balancing across multiple reactor threads.

Key Features

🚀 Modern C++23

Built using the latest C++ standards to ensure memory safety, efficiency, and cleaner code syntax. 100% written with AI assistance.

☸️ Kubernetes Ready

Designed for cloud-native environments. Deploy effortlessly with our dedicated MicroK8s Tutorial.

⚡ High Performance

Utilizes multi-reactor pattern and EPOLL for non-blocking I/O, capable of handling thousands of concurrent connections.

💰 Cost-Efficient Scaling

Maximize your infrastructure. Run a robust 3-Pod cluster on a single node with just 4 cores and 4GB RAM, delivering high availability at a fraction of the cloud cost.

3-Step Quick Tutorial

Create a blazing fast, compiled machine-code API in just three steps.

1 Define the Validator

Define a strict contract using modern C++23 rules and lambdas for validation.

const validator customer_validator { rule<std::string>{"id", requirement::required, [](std::string_view s) { return s.length() == 5 && std::ranges::all_of(s, [](unsigned char c){ return std::isalpha(c); }); }, "Customer ID must be exactly 5 alphabetic characters." } };

2 Create the API

Implement the logic. The framework handles input extraction using strong types, and sql::get executes the stored procedure directly with a potentially cached prepared statement, avoiding SQL injection attacks.

void get_customer(const http::request& req, http::response& res) { auto customer_id = **req.get_value("id"); const auto json_result = sql::get("DB1", "{CALL sp_customer_get(?)}", customer_id); res.set_body( json_result ? ok : not_found, json_result.value_or(R"({"error":"Customer not found"})") ); }

3 Publish It

Register the API endpoint in main(). The true flag at the end enables automatic secure authentication.

int main() { try { // ... server initialization ... util::log::info("Application starting..."); server s; // Register: Path, Method, Validator, Function Pointer, AuthEnabled s.register_api(webapi_path{"/customer"}, post, customer_validator, &get_customer, true); // ... server run ... s.start(); util::log::info("Application shutting down gracefully."); } catch (const server_error& e) { util::log::critical("A critical server error occurred: {}", e.what()); return 1; } catch (const std::exception& e) { util::log::critical("An unexpected error occurred: {}", e.what()); return 1; } catch (...) { util::log::critical("An unknown error occurred."); return 1; } return 0; }

Test It (Secure)

Invoke your new API using curl with a Bearer Token and pipe to jq for formatting.

# Assuming you have a valid JWT token export BASE_URL="http://localhost:8080" export TOKEN="your_jwt_token_here" $ curl "${BASE_URL}/customer" -s -d '{"id":"ANATR"}' \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $TOKEN" | jq { "customerid": "ANATR", "contactname": "Ana Trujillo", "companyname": "Ana Trujillo Emparedados y helados", "city": "México D.F.", "country": "Mexico", "phone": "(5) 555-4729", "orders": [ { "customerid": "ANATR", "orderid": 10308, "orderdate": "1994-10-19", "shipcountry": "Mexico", "shipper": "Federal Shipping", "total": 88.8 }, { "customerid": "ANATR", "orderid": 10625, "orderdate": "1995-09-08", "shipcountry": "Mexico", "shipper": "Speedy Express", "total": 479.75 }, { "customerid": "ANATR", "orderid": 10759, "orderdate": "1995-12-29", "shipcountry": "Mexico", "shipper": "Federal Shipping", "total": 320 }, { "customerid": "ANATR", "orderid": 10926, "orderdate": "1996-04-03", "shipcountry": "Mexico", "shipper": "Federal Shipping", "total": 514.4 } ] }

Safety & Code Quality

We prioritize stability and correctness. APIServer2 allows you to write C++ with confidence.

🏅 SonarCloud 'A' Grade Strict adherence to C++ Core Guidelines ensuring maintainable and bug-free code.
🛡️ Sanitizer Hardened & Leak Free Validated with AddressSanitizer (ASan), LeakSanitizer (LSan), and ThreadSanitizer (TSan) to guarantee memory safety, zero leaks, and race-free concurrency.

Resource Efficiency

Optimized for high-density cloud deployments, APIServer2 delivers maximum performance with a minimal footprint.

< 1%

RAM Usage
On a 4GB K8s Node under load

450KB

Binary Size
Compact executable, all APIs included

Instant

Startup Time
Blazing fast initialization

1 Core

CPU Requirement
Full performance with single core dedication

Installation & Deployment

⚠️ Prerequisite: The DemoDB with SQLServer 2019 is required to run APIServer2.

Local Cloud with Multipass (Windows Users)

For Windows users, we strongly recommend simulating a cloud environment using Multipass. This allows you to run Ubuntu 24.04 VMs seamlessly.

The recommended topology consists of two VMs: demodb.mshome.net for the database and mk8s.mshome.net for the Kubernetes cluster.