ControllerBuilder
mach::ControllerBuilder
Description
Section titled “Description”Configures routes for a controller.
Headers
Section titled “Headers”Primary header:
#include <mach/controllers/ControllerBuilder.hpp>Convenience header:
#include <mach/controllers.hpp>Syntax
Section titled “Syntax”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);};Template Parameters
Section titled “Template Parameters”| Name | Description |
|---|---|
TController |
The type of the HTTP controller being configured. |
Member Functions
Section titled “Member Functions”| 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. |
Thread Safety
Section titled “Thread Safety”Not thread-safe. Controller configuration should be performed from one thread.
Remarks
Section titled “Remarks”ControllerBuilderinstances are provided by Mach during controller configuration.ControllerBuilderis not intended to be instantiated directly.ControllerBuilderinstances are valid only for the duration of the controller’sconfiguremethod.
Example
Section titled “Example”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); }};See Also
Section titled “See Also”| Name | Description |
|---|---|
ControllerBase |
Base class for defining controllers. |