SNode.C
Loading...
Searching...
No Matches
Route.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "express/Route.h"
21
22#include "express/Controller.h"
23#include "express/dispatcher/ApplicationDispatcher.h"
24#include "express/dispatcher/MiddlewareDispatcher.h"
25#include "express/dispatcher/RouterDispatcher.h"
26
27#ifndef DOXYGEN_SHOULD_SKIP_THIS
28
29#include <memory>
30
31#endif /* DOXYGEN_SHOULD_SKIP_THIS */
32
33#define DEFINE_ROUTE_REQUESTMETHOD(METHOD, HTTP_METHOD)
34 Route& Route::METHOD(const std::function<void(const std::shared_ptr<Request>&, const std::shared_ptr<Response>&, Next&)>& lambda)
35 const {
36 return *(dispatcher->nextRoute = std::make_shared<Route>(
37 HTTP_METHOD, mountPoint.relativeMountPath, std::make_shared<dispatcher::MiddlewareDispatcher>(lambda)))
38 .get();
39 }
40 Route& Route::METHOD(const std::function<void(const std::shared_ptr<Request>&, const std::shared_ptr<Response>&)>& lambda) const {
41 return *(dispatcher->nextRoute = std::make_shared<Route>(
42 HTTP_METHOD, mountPoint.relativeMountPath, std::make_shared<dispatcher::ApplicationDispatcher>(lambda)))
43 .get();
44 }
45
46namespace express {
47
52
53 Route::Route(const std::string& method, const std::string& relativeMountPath, const std::shared_ptr<Dispatcher>& dispatcher)
56 }
57
58 bool Route::dispatch(Controller& controller) {
59 return dispatch(controller, "");
60 }
61
62 bool Route::dispatch(Controller& controller, const std::string& parentMountPath) {
63 controller.setCurrentRoute(this);
64
65 bool dispatched = dispatcher->dispatch(controller, parentMountPath, mountPoint);
66
67 if (!dispatched) { // TODO: only call if parent route matched
68 dispatched = controller.dispatchNext(parentMountPath);
69 }
70
71 return dispatched;
72 }
73
74 bool Route::dispatchNext(Controller& controller, const std::string& parentMountPath) {
75 return dispatcher->dispatchNext(controller, parentMountPath);
76 }
77
84 DEFINE_ROUTE_REQUESTMETHOD(connect, "CONNECT")
85 DEFINE_ROUTE_REQUESTMETHOD(options, "OPTIONS")
89
90} // namespace express
#define DEFINE_ROUTE_REQUESTMETHOD(METHOD, HTTP_METHOD)
Definition Route.cpp:33
bool dispatch(Controller &controller, const std::string &parentMountPath)
Definition Route.cpp:62
Route(const std::string &method, const std::string &relativeMountPath, const std::shared_ptr< Dispatcher > &dispatcher)
Definition Route.cpp:53
bool dispatch(Controller &controller)
Definition Route.cpp:58
bool dispatchNext(Controller &controller, const std::string &parentMountPath)
Definition Route.cpp:74