Add .unhandled route

This commit is contained in:
Alex Hultman
2018-10-03 18:35:20 +02:00
parent 0d932e9649
commit d4ca158495
5 changed files with 22 additions and 4 deletions
+1 -1
View File
@@ -33,7 +33,7 @@ public:
}
TemplatedApp &unhandled(std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
//httpContext->onGet(pattern, handler);
httpContext->onUnhandled(handler);
return *this;
}
+8
View File
@@ -220,6 +220,14 @@ public:
});
}
void onUnhandled(std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
HttpContextData<SSL> *httpContextData = getSocketContextData();
httpContextData->router.unhandled([handler](typename HttpContextData<SSL>::UserData *user, auto *args) {
handler(user->httpResponse, user->httpRequest);
});
}
/* Listen to port using this HttpContext */
us_listen_socket *listen(const char *host, int port, int options) {
return static_dispatch(us_ssl_socket_context_listen, us_socket_context_listen)(getSocketContext(), host, port, options, sizeof(HttpResponseData<SSL>));
+6
View File
@@ -18,6 +18,7 @@ class HttpRouter {
private:
std::vector<std::function<void(USERDATA, std::vector<std::string_view> *)>> handlers;
std::vector<std::string_view> params;
std::function<void(USERDATA, std::vector<std::string_view> *)> unhandledHandler;
struct Node {
std::string name;
@@ -135,6 +136,10 @@ public:
params.reserve(100);
}
HttpRouter *unhandled(std::function<void(USERDATA, std::vector<std::string_view> *)> handler) {
unhandledHandler = handler;
}
HttpRouter *add(const char *method, const char *pattern, std::function<void(USERDATA, std::vector<std::string_view> *)> handler) {
// step over any initial slash
@@ -178,6 +183,7 @@ public:
handlers[index](userData, &params);
} else {
std::cout << "Did not find route for URL: " << std::string_view(url, url_length) << std::endl;
unhandledHandler(userData, &params);
}
params.clear();