Skip to content
Download

ControllerBuilder

mach::ControllerBuilder

Configures routes for a controller.

Primary header:

#include <mach/controllers/ControllerBuilder.hpp>

Convenience header:

#include <mach/controllers.hpp>
template <typename TController>
class ControllerBuilder {
public:
ControllerBuilder(const ControllerBuilder&) = delete;
ControllerBuilder& operator=(const ControllerBuilder&) = delete;
ControllerBuilder(ControllerBuilder&&) = delete;
ControllerBuilder& operator=(ControllerBuilder&&) = delete;
template <typename THandler>
ControllerBuilder<TController>& mapGet(std::string_view pattern, THandler&& handler);
template <typename THandler>
ControllerBuilder<TController>& mapGet(THandler&& handler);
template <typename THandler>
ControllerBuilder<TController>& mapPost(std::string_view pattern, THandler&& handler);
template <typename THandler>
ControllerBuilder<TController>& mapPost(THandler&& handler);
template <typename THandler>
ControllerBuilder<TController>& mapPut(std::string_view pattern, THandler&& handler);
template <typename THandler>
ControllerBuilder<TController>& mapPut(THandler&& handler);
template <typename THandler>
ControllerBuilder<TController>& mapPatch(std::string_view pattern, THandler&& handler);
template <typename THandler>
ControllerBuilder<TController>& mapPatch(THandler&& handler);
template <typename THandler>
ControllerBuilder<TController>& mapDelete(std::string_view pattern, THandler&& handler);
template <typename THandler>
ControllerBuilder<TController>& mapDelete(THandler&& handler);
template <typename THandler>
ControllerBuilder<TController>& mapHead(std::string_view pattern, THandler&& handler);
template <typename THandler>
ControllerBuilder<TController>& mapHead(THandler&& handler);
};
Name Description
TController The type of the HTTP controller being configured.
Name Description
mapGet Registers a GET route handler for the controller.
mapPost Registers a POST route handler for the controller.
mapPut Registers a PUT route handler for the controller.
mapPatch Registers a PATCH route handler for the controller.
mapDelete Registers a DELETE route handler for the controller.
mapHead Registers a HEAD route handler for the controller.

Not thread-safe. Controller configuration should be performed from one thread.

  • ControllerBuilder instances are provided by Mach during controller configuration.
  • ControllerBuilder is not intended to be instantiated directly.
  • ControllerBuilder instances are valid only for the duration of the controller’s configure method.
class UsersController : public mach::ControllerBase {
public:
static constexpr std::string_view route = "/users";
mach::Reply<> get() {
auto id = request().routeParam<int>("id");
return ok(id);
}
mach::Reply<> create(CreateUserRequest request) {
return created(request);
}
static void configure(mach::ControllerBuilder<UsersController>& builder) {
builder.mapGet("/{id:int}", &UsersController::get);
builder.mapPost("/", &UsersController::create);
}
};
Name Description
ControllerBase Base class for defining controllers.