Skip to content

Commit

Permalink
Add initial interface without router
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Oct 16, 2024
1 parent 600e7fe commit 1c6c78b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion examples/HelloWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions src/App.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ struct TemplatedApp {

TopicTree<TopicTreeMessage, TopicTreeBigMessage> *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<BuilderPatternReturnType &&>(*this));
}

/* Server name */
BuilderPatternReturnType &&addServerName(std::string hostname_pattern, SocketContextOptions options = {}) {

Expand Down
6 changes: 6 additions & 0 deletions src/HttpRouter.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ template <class USERDATA>
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;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1c6c78b

Please sign in to comment.