Skip to content
Download

Your First Endpoint

A new Mach project already includes a simple endpoint in src/main.cpp.

#include <mach/mach.hpp>
int main() {
mach::AppBuilder builder;
auto app = builder.build();
app.mapGet("/", [] {
return mach::ok("Hello, world!");
});
return app.run();
}
mach::AppBuilder builder;
auto app = builder.build();

mach::AppBuilder is used to configure and create your application.

Calling build() creates the application instance that you’ll use to define endpoints and run the server.

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

mapGet maps an HTTP GET request to an endpoint.

The first argument, "/", is the route. In this case, the endpoint responds to requests made to the root of the application.

The lambda is the endpoint handler. When the route is requested, it returns:

mach::ok("Hello, world!")

mach::ok creates a successful HTTP response containing "Hello, world!".

return app.run();

run() starts the Mach server and keeps the application running until it is stopped.


In the next step, you’ll run the application and send a request to your first endpoint.