Building Mach: A Modern C++20 Web Framework on Boost.Asio and Boost.Beast

Building Mach: A Modern C++20 Web Framework on Boost.Asio and Boost.Beast

I've been working on Mach, an open-source C++20 web framework designed to make building HTTP APIs in C++ more approachable without sacrificing the performance and control the language provides.

In September, I released Mach 1.0.0, its first stable version.

A major part of what makes Mach possible is Boost.Asio and Boost.Beast, which provide the foundation for its networking and HTTP infrastructure.

Why I Built Mach

Modern C++ has powerful tools for building high-performance network applications, but creating a complete web API often requires considerably more infrastructure than it would in higher-level frameworks.

Coming from experience with ASP.NET Core, I wanted to explore what a similar developer experience could look like in C++.

My goal was to provide familiar abstractions such as routing, dependency injection, middleware, controllers, and model binding, while keeping the framework native to C++20.

For example, a simple Mach endpoint looks like this:

app.mapGet("/hello", []{
    return mach::ok("Hello from Mach!");
});

The idea is to let developers focus on application logic rather than repeatedly implementing the same HTTP infrastructure.

Boost.Asio and Boost.Beast Under the Hood

Mach uses Boost.Asio for asynchronous networking and Boost.Beast for HTTP protocol handling.

The server is built around C++20 coroutines and an io_context-based execution model, with a separate io_context for each worker thread.

This architecture allows Mach to handle network I/O asynchronously while exposing a higher-level application programming interface.

Beast handles the underlying HTTP communication, while Mach builds additional layers on top of it, including routing, request processing, middleware execution, and response generation.

One of the main architectural challenges has been deciding which details to expose to application developers and which should remain internal to the framework.

Asio and Beast provide powerful low-level capabilities. Mach's role is to make those capabilities accessible through a more application-oriented interface.

From HTTP Handling to Web Applications

Beyond the networking layer, Mach 1.0 includes:

  • Minimal APIs and controller-based routing

  • Dependency injection with singleton, scoped, and transient lifetimes

  • Middleware and centralized exception handling

  • JSON serialization and model binding

  • Request validation

  • CORS and CSRF protection

  • Logging and configuration

  • A command-line interface for creating, building, and running projects

For instance, Mach can serialize custom C++ types into JSON using a simple declaration:

struct User {
    std::string name;
    int age;
};

MACH_DEFINE_JSON(User, name, age);

That type can then be returned directly from an endpoint:

app.mapGet("/users", [] {
    return mach::ok(
        std::vector<User>{
            {"Alice", 30},
            {"Bob", 25}
        }
    );
});

The framework handles the JSON serialization and HTTP response.

What's Next?

I'm currently working on Mach 1.1.0, which focuses on improving the request-handling experience, including stronger typed request binding, additional HTTP helpers, and configurable request limits.

There are also important capabilities still on the roadmap, particularly asynchronous application-level route handlers and better documented database integration.

Although Mach already uses asynchronous networking internally, asynchronous route handlers aren't yet part of its public API.

These are areas I want to develop carefully as the framework evolves.

Feedback and Contributions

Mach is still a young project, and I'm particularly interested in feedback from developers familiar with Boost.Asio, Boost.Beast, and modern C++ networking.

I'd welcome thoughts on its architecture, API design, and opportunities for improvement. Contributions are welcome as well.

You can explore the source code and documentation here:

GitHub: https://github.com/machframework/mach

Website: https://machframework.dev

Thanks to the Boost community for building and maintaining the libraries that make projects like Mach possible.

  • Powering Cognitive Communications Engine for NASA with Boost
    News #Serialization
    Boost libraries were employed to provide permissive‑licensed, reliable networking and serialization for a C++11 reinforcement‑learning cognitive engine that autonomously optimizes ISS radio communications. Boost.Asio was used to handle concurrent UDP/Ethernet I/O, and Boost.Serialization was used to enable state persistence across orbital passes, facilitating successful on‑orbit testing.
  • A postgres library for Boost
    Link #Asio
    A Boost-native PostgreSQL library, NativePG, is being developed to provide low‑level message serialization, type parsing, pipelined request composition, flexible response handlers, Asio‑based connections, auto‑batching, connection pooling, and safe SQL formatting, extending beyond libPQ.