24#include <esp_http_server.h>
32#include <ztl/fail.hpp>
33#include <ztl/type_traits.hpp>
54 template<
typename T,
typename F>
56 assert(key.uri && strlen(key.uri) && key.uri[strlen(key.uri) - 1uz] ==
'/');
57 if constexpr (std::invocable<typename ztl::signature<F>::type,
60 _sync_map[key].push_back([t, f](
auto&&... args) {
61 return std::invoke(f, *t, std::forward<
decltype(args)>(args)...);
63 else if constexpr (std::invocable<typename ztl::signature<F>::type,
66 _async_map[key].push_back([t, f](
auto&&... args) {
67 return std::invoke(f, *t, std::forward<
decltype(args)>(args)...);
75 static constexpr auto chunk_size{16384uz};
80 return std::unexpected<std::string>{
"501 Not Implemented"};
84 Request r{.uri = std::string(req->uri),
85 .body = std::string(req->content_len,
'\0')};
87 while (bytes_red < req->content_len) {
89 httpd_req_recv(req, data(r.body) + bytes_red, chunk_size)};
92 else return std::unexpected<std::string>{
"500 Internal Server Error"};
96 return it->second[0uz](r);
104 httpd_ws_frame_t frame{};
105 if (httpd_ws_recv_frame(req, &frame, 0uz))
return ESP_FAIL;
108 Message msg{.sock_fd = httpd_req_to_sockfd(req),
110 .payload = std::vector<uint8_t>(frame.len)};
112 frame.payload = data(msg.payload);
113 if (httpd_ws_recv_frame(req, &frame, frame.len))
return ESP_FAIL;
116 return it->second[0uz](msg);
122 return {.uri = req->uri,
123 .method =
static_cast<httpd_method_t
>(req->method)};
130 if (lhs.method != rhs.method)
return lhs.method < rhs.method;
132 else if (
auto const lhs_prefix_len{strrchr(lhs.uri,
'/') - lhs.uri + 1},
133 rhs_prefix_len{strrchr(rhs.uri,
'/') - rhs.uri + 1};
134 lhs_prefix_len == rhs_prefix_len)
135 return strncmp(lhs.uri, rhs.uri, lhs_prefix_len) < 0;
137 else return strcmp(lhs.uri, rhs.uri) < 0;
141 std::map<key_type, sync_mapped_type, key_compare>
_sync_map;
142 std::map<key_type, async_mapped_type, key_compare>
_async_map;
Definition endpoints.hpp:43
esp_err_t asyncResponse(httpd_req_t *req)
Definition endpoints.hpp:100
httpd_uri_t req2key(httpd_req_t *req) const
Definition endpoints.hpp:121
std::map< key_type, async_mapped_type, key_compare > _async_map
Definition endpoints.hpp:142
httpd_uri_t key_type
Definition endpoints.hpp:44
std::map< key_type, sync_mapped_type, key_compare > _sync_map
Definition endpoints.hpp:141
std::vector< std::function< Response(Request const &)> > sync_mapped_type
Mapped type for HTTP requests.
Definition endpoints.hpp:47
void subscribe(key_type const &key, std::shared_ptr< T > t, F &&f)
Definition endpoints.hpp:55
Response syncResponse(httpd_req_t *req)
Definition endpoints.hpp:74
std::vector< std::function< esp_err_t(Message &)> > async_mapped_type
Mapped type for WebSockets.
Definition endpoints.hpp:50
Definition config.hpp:442
std::expected< std::string, std::string > Response
Definition response.hpp:29
Definition endpoints.hpp:127
bool operator()(key_type const &lhs, key_type const &rhs) const
Definition endpoints.hpp:128
Definition message.hpp:29
Definition request.hpp:28