Add simple "any" route to catch any method
This commit is contained in:
+4
-1
@@ -16,6 +16,9 @@ int main(int argc, char **argv) {
|
||||
.key_file_name = "/home/alexhultman/key.pem",
|
||||
.cert_file_name = "/home/alexhultman/cert.pem",
|
||||
.passphrase = "1234"
|
||||
}).any("/anything", [](auto *res, auto *req) {
|
||||
std::cout << "Any route with method: " << req->getMethod() << std::endl;
|
||||
res->end("Hello Any route!");
|
||||
}).get("/exit", [](auto *res, auto *req) {
|
||||
|
||||
if (!token) {
|
||||
@@ -28,7 +31,7 @@ int main(int argc, char **argv) {
|
||||
/* Use this route to signal stop listening */
|
||||
us_listen_socket_close(token);
|
||||
token = nullptr;
|
||||
}).ws<PerSocketData>("/*", {
|
||||
}).ws<PerSocketData>("/ws", {
|
||||
/* Settings */
|
||||
.compression = uWS::DEDICATED_COMPRESSOR,
|
||||
.maxPayloadLength = 16 * 1024 * 1024,
|
||||
|
||||
@@ -268,8 +268,9 @@ public:
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
TemplatedApp &&unhandled(std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
|
||||
httpContext->onUnhandled(handler);
|
||||
/* This one catches any method */
|
||||
TemplatedApp &&any(std::string pattern, std::function<void(HttpResponse<SSL> *, HttpRequest *)> handler) {
|
||||
httpContext->onHttp("*", pattern, handler);
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
|
||||
+10
-2
@@ -154,9 +154,17 @@ private:
|
||||
}
|
||||
|
||||
if (parent != &tree) {
|
||||
return matchUrlSegment(parent, 0);
|
||||
int handler = matchUrlSegment(parent, 0);
|
||||
|
||||
/* We failed to match */
|
||||
if (!handler && method.length() > 1) {
|
||||
return lookupNew("*", url);
|
||||
}
|
||||
|
||||
return handler;
|
||||
} else {
|
||||
return 0;
|
||||
/* Unhandled, check if we have any "all" route for this url */
|
||||
return method.length() == 1 ? 0 : lookupNew("*", url);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user