Skip to content
Download

ControllerBase

mach::ControllerBase

Base class for defining controllers.

Primary header:

#include <mach/controllers/ControllerBase.hpp>

Convenience header:

#include <mach/controllers.hpp>
class ControllerBase {
public:
[[nodiscard]] mach::Request& request() noexcept;
[[nodiscard]] const mach::Request& request() const noexcept;
[[nodiscard]] mach::Response& response() noexcept;
[[nodiscard]] const mach::Response& response() const noexcept;
protected:
template <typename T>
[[nodiscard]] mach::Reply<T> ok(T value);
[[nodiscard]] mach::Reply<std::string> ok(const char* value);
[[nodiscard]] mach::Reply<> ok();
template <typename T>
[[nodiscard]] mach::Reply<T> created(T value);
[[nodiscard]] mach::Reply<std::string> created(const char* value);
[[nodiscard]] mach::Reply<> created();
[[nodiscard]] mach::Reply<> noContent();
template <typename T>
[[nodiscard]] mach::Reply<T> badRequest(T value);
[[nodiscard]] mach::Reply<std::string> badRequest(const char* value);
template <typename T = void>
[[nodiscard]] mach::Reply<T> badRequest();
template <typename T>
[[nodiscard]] mach::Reply<T> unauthorized(T value);
[[nodiscard]] mach::Reply<std::string> unauthorized(const char* value);
template <typename T = void>
[[nodiscard]] mach::Reply<T> unauthorized();
template <typename T>
[[nodiscard]] mach::Reply<T> forbidden(T value);
[[nodiscard]] mach::Reply<std::string> forbidden(const char* value);
template <typename T = void>
[[nodiscard]] mach::Reply<T> forbidden();
template <typename T>
[[nodiscard]] mach::Reply<T> notFound(T value);
[[nodiscard]] mach::Reply<std::string> notFound(const char* value);
template <typename T = void>
[[nodiscard]] mach::Reply<T> notFound();
template <typename T>
[[nodiscard]] mach::Reply<T> conflict(T value);
[[nodiscard]] mach::Reply<std::string> conflict(const char* value);
template <typename T = void>
[[nodiscard]] mach::Reply<T> conflict();
};
Name Description
request Returns the current HTTP request.
response Returns the current HTTP response.
Name Description
ok Creates a 200 OK response.
created Creates a 201 Created response.
noContent Creates a 204 No Content response.
badRequest Creates a 400 Bad Request response.
unauthorized Creates a 401 Unauthorized response.
forbidden Creates a 403 Forbidden response.
notFound Creates a 404 Not Found response.
conflict Creates a 409 Conflict response.

Not thread-safe. Controller instances must not be shared between threads.

  • ControllerBase is intended to be used only as a base class for HTTP controllers.
  • Protected helper functions are available for creating common HTTP responses.
class UsersController : public mach::ControllerBase {
public:
static constexpr std::string_view route = "/users/{id:int}";
mach::Reply<> get() {
auto id = request().routeParam<int>("id");
return ok(id);
}
};
Name Description
ControllerBuilder Configures routes for a controller.
Request Represents the current HTTP request.
Response Represents the HTTP response being built.