ControllerBase
mach::ControllerBase
Description
Section titled “Description”Base class for defining controllers.
Headers
Section titled “Headers”Primary header:
#include <mach/controllers/ControllerBase.hpp>Convenience header:
#include <mach/controllers.hpp>Syntax
Section titled “Syntax”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();};Member Functions
Section titled “Member Functions”| Name | Description |
|---|---|
request |
Returns the current HTTP request. |
response |
Returns the current HTTP response. |
Protected Member Functions
Section titled “Protected Member Functions”| 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. |
Thread Safety
Section titled “Thread Safety”Not thread-safe. Controller instances must not be shared between threads.
Remarks
Section titled “Remarks”ControllerBaseis intended to be used only as a base class for HTTP controllers.- Protected helper functions are available for creating common HTTP responses.
Example
Section titled “Example”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); }};See Also
Section titled “See Also”| Name | Description |
|---|---|
ControllerBuilder |
Configures routes for a controller. |
Request |
Represents the current HTTP request. |
Response |
Represents the HTTP response being built. |