diff --git a/examples/HelloWorld.cpp b/examples/HelloWorld.cpp index 0ecf080a0..069268566 100644 --- a/examples/HelloWorld.cpp +++ b/examples/HelloWorld.cpp @@ -8,7 +8,7 @@ int main() { .key_file_name = "misc/key.pem", .cert_file_name = "misc/cert.pem", .passphrase = "1234" - }).get("/*", [](auto *res, auto */*req*/) { + }).oneRouteCatchAll(true).get("/*", [](auto *res, auto */*req*/) { res->end("Hello world!"); }).listen(3000, [](auto *listen_socket) { if (listen_socket) { diff --git a/src/App.h b/src/App.h index db916822a..6c9e82145 100644 --- a/src/App.h +++ b/src/App.h @@ -93,6 +93,13 @@ struct TemplatedApp { TopicTree *topicTree = nullptr; + /* Used if you don't really need the URL router. */ + BuilderPatternReturnType &&oneRouteCatchAll(bool enabled) { + httpContext->getSocketContextData()->router.firstRouteCatchAll = enabled; + + return std::move(static_cast(*this)); + } + /* Server name */ BuilderPatternReturnType &&addServerName(std::string hostname_pattern, SocketContextOptions options = {}) { diff --git a/src/HttpRouter.h b/src/HttpRouter.h index 39ebb2e7c..fea979985 100644 --- a/src/HttpRouter.h +++ b/src/HttpRouter.h @@ -37,6 +37,7 @@ template struct HttpRouter { static constexpr std::string_view ANY_METHOD_TOKEN = "*"; static const uint32_t HIGH_PRIORITY = 0xd0000000, MEDIUM_PRIORITY = 0xe0000000, LOW_PRIORITY = 0xf0000000; + bool firstRouteCatchAll = false; private: USERDATA userData; @@ -259,6 +260,11 @@ struct HttpRouter { setUrl(url); routeParameters.reset(); + /* When you only have 1 handler, you're most likely not using the router */ + if (firstRouteCatchAll) { + return handlers[0](this); + } + /* Begin by finding the method node */ for (auto &p : root.children) { if (p->name == method) {