19template <
typename ClassType,
typename ReturnType,
typename... Args>
23 static constexpr size_t arg_count =
sizeof...(Args);
27template <
typename ClassType,
typename ReturnType,
typename... Args>
31 static constexpr size_t arg_count =
sizeof...(Args);
35template <
typename ReturnType,
typename... Args>
39 static constexpr size_t arg_count =
sizeof...(Args);
43template <
typename Handler>
46 using Ret =
typename traits::result_type;
48 if constexpr (std::is_same_v<Ret, void> || traits::arg_count == 2) {
51 return [h = std::forward<Handler>(h)](
http::HttpRequest& req, std::shared_ptr<http::ResponseWriter> writer)
mutable {
56 return [h = std::forward<Handler>(h)](
http::HttpRequest& req, std::shared_ptr<http::ResponseWriter> writer)
mutable {
59 if constexpr (traits::arg_count == 0) {
61 }
else if constexpr (traits::arg_count == 1) {
64 static_assert(traits::arg_count <= 1,
"Magic handlers can only take 0 or 1 arguments (HttpRequest&)");
68 if constexpr (std::is_same_v<Ret, std::string>) {
70 res.
headers[
"Content-Type"] =
"text/plain";
71 }
else if constexpr (std::is_same_v<Ret, nlohmann::json>) {
73 res.
headers[
"Content-Type"] =
"application/json";
78 res.
headers[
"Content-Type"] =
"application/json";
80 writer->send(std::move(res));
81 }
catch (
const std::exception& e) {
84 res.
headers[
"Content-Type"] =
"text/plain";
85 writer->send(std::move(res));
Represents an HTTP response to be sent to a client.
Definition HttpResponse.hpp:50
HttpResponse & send(const std::string &b) &
Sets the response body as plain text.
Definition HttpResponse.hpp:116
HttpResponse & status(HttpStatus code) &
Sets the HTTP status code of the response.
Definition HttpResponse.hpp:109
std::unordered_map< std::string, std::string, utils::CaseInsensitiveHash, utils::CaseInsensitiveEqual > headers
Definition HttpResponse.hpp:54
a class to store JSON values
Definition json.hpp:19401
string_t dump(const int indent=-1, const char indent_char=' ', const bool ensure_ascii=false, const error_handler_t error_handler=error_handler_t::strict) const
serialization
Definition json.hpp:20574
Definition HandlerWrapper.hpp:12
auto wrap_handler(Handler &&h) -> std::function< void(http::HttpRequest &, std::shared_ptr< http::ResponseWriter >)>
Definition HandlerWrapper.hpp:44
Represents an incoming HTTP request.
Definition HttpRequest.hpp:22
std::tuple< Args... > args_tuple
Definition HandlerWrapper.hpp:22
ReturnType result_type
Definition HandlerWrapper.hpp:21
ReturnType result_type
Definition HandlerWrapper.hpp:29
std::tuple< Args... > args_tuple
Definition HandlerWrapper.hpp:30
std::tuple< Args... > args_tuple
Definition HandlerWrapper.hpp:38
ReturnType result_type
Definition HandlerWrapper.hpp:37
Definition HandlerWrapper.hpp:16