24#include <esp_http_server.h>
31#include <ztl/fail.hpp>
52 template<
typename T,
typename F>
54 if constexpr (std::invocable<typename signature<F>::type,
Request const&>)
55 _sync_map[key].push_back([t, f](
auto&&... args) {
56 return std::invoke(f, *t, std::forward<
decltype(args)>(args)...);
58 else if constexpr (std::invocable<typename signature<F>::type,
Message&>)
59 _async_map[key].push_back([t, f](
auto&&... args) {
60 return std::invoke(f, *t, std::forward<
decltype(args)>(args)...);
68 static constexpr auto chunk_size{16384uz};
73 return std::unexpected<std::string>{
"501 Not Implemented"};
77 Request r{.uri = std::string(req->uri),
78 .body = std::string(req->content_len,
'\0')};
80 while (bytes_red < req->content_len) {
82 httpd_req_recv(req, data(r.body) + bytes_red, chunk_size)};
85 else return std::unexpected<std::string>{
"500 Internal Server Error"};
89 return it->second[0uz](r);
97 httpd_ws_frame_t frame{};
98 if (httpd_ws_recv_frame(req, &frame, 0uz))
return ESP_FAIL;
101 Message msg{.sock_fd = httpd_req_to_sockfd(req),
103 .payload = std::vector<uint8_t>(frame.len)};
105 frame.payload = data(msg.payload);
106 if (httpd_ws_recv_frame(req, &frame, frame.len))
return ESP_FAIL;
109 return it->second[0uz](msg);
115 return {.uri = req->uri,
116 .method =
static_cast<httpd_method_t
>(req->method)};
122 return lhs.method != rhs.method
123 ? lhs.method < rhs.method
126 std::min(strlen(lhs.uri), strlen(rhs.uri))) < 0;
130 std::map<key_type, sync_mapped_type, key_compare>
_sync_map;
131 std::map<key_type, async_mapped_type, key_compare>
_async_map;
Definition endpoints.hpp:41
std::vector< std::function< esp_err_t(Message &)> > async_mapped_type
Mapped type for WebSockets.
Definition endpoints.hpp:48
httpd_uri_t key_type
Definition endpoints.hpp:42
std::map< key_type, async_mapped_type, key_compare > _async_map
Definition endpoints.hpp:131
void subscribe(key_type const &key, std::shared_ptr< T > t, F &&f)
Definition endpoints.hpp:53
esp_err_t asyncResponse(httpd_req_t *req)
Definition endpoints.hpp:93
std::map< key_type, sync_mapped_type, key_compare > _sync_map
Definition endpoints.hpp:130
std::vector< std::function< Response(Request const &)> > sync_mapped_type
Mapped type for HTTP requests.
Definition endpoints.hpp:45
httpd_uri_t req2key(httpd_req_t *req) const
Definition endpoints.hpp:114
Response syncResponse(httpd_req_t *req)
Definition endpoints.hpp:67
Log macros without TAG parameter.
Definition config.hpp:299
std::expected< std::string, std::string > Response
Definition response.hpp:29
Definition endpoints.hpp:120
bool operator()(key_type const &lhs, key_type const &rhs) const
Definition endpoints.hpp:121
Definition message.hpp:29
Definition request.hpp:28