FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

refactor: move code to router · kassane/expresscpp@3fd44f2 · GitHub

Commit 3fd44f2

Browse files
Marco Czech
committed
refactor: move code to router
1 parent 6170b9e commit 3fd44f2

11 files changed

Lines changed: 81 additions & 92 deletions

File tree

‎.gitlab-ci.yml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ deploy-latest-docker:
5757
- cd build
5858
- |
5959
cmake .. \
60+
-DEXPRESSCPP_USE_CONAN_DEPENDENCIES=ON \
6061
-DEXPRESSCPP_ENABLE_COVERAGE=ON \
6162
-DEXPRESSCPP_BUILD_EXAMPLES=ON \
6263
-DEXPRESSCPP_ENABLE_STATIC_FILE_PROVIDER=ON \
@@ -168,6 +169,7 @@ ADDRESS_SANITIZER:linux:gcc9:
168169
- cd build
169170
- |
170171
cmake .. \
172+
-DEXPRESSCPP_USE_CONAN_DEPENDENCIES=ON \
171173
-DEXPRESSCPP_ENABLE_COVERAGE=OFF \
172174
-DEXPRESSCPP_BUILD_EXAMPLES=OFF \
173175
-DEXPRESSCPP_BUILD_TESTS=ON \
@@ -188,6 +190,7 @@ LEAK_SANITIZER:linux:gcc9:
188190
- cd build
189191
- |
190192
cmake .. \
193+
-DEXPRESSCPP_USE_CONAN_DEPENDENCIES=ON \
191194
-DEXPRESSCPP_ENABLE_COVERAGE=OFF \
192195
-DEXPRESSCPP_BUILD_EXAMPLES=OFF \
193196
-DEXPRESSCPP_BUILD_TESTS=ON \
@@ -208,6 +211,7 @@ LEAK_SANITIZER:linux:gcc9:
208211
- cd build
209212
- |
210213
cmake .. \
214+
-DEXPRESSCPP_USE_CONAN_DEPENDENCIES=ON \
211215
-DEXPRESSCPP_ENABLE_COVERAGE=OFF \
212216
-DEXPRESSCPP_BUILD_EXAMPLES=OFF \
213217
-DEXPRESSCPP_BUILD_TESTS=ON \
@@ -228,6 +232,7 @@ UNDEFINED_SANITIZER:linux:gcc9:
228232
- cd build
229233
- |
230234
cmake .. \
235+
-DEXPRESSCPP_USE_CONAN_DEPENDENCIES=ON \
231236
-DEXPRESSCPP_ENABLE_COVERAGE=OFF \
232237
-DEXPRESSCPP_BUILD_EXAMPLES=OFF \
233238
-DEXPRESSCPP_BUILD_TESTS=ON \
@@ -248,6 +253,7 @@ THREAD_SANITIZER:linux:gcc9:
248253
- cd build
249254
- |
250255
cmake .. \
256+
-DEXPRESSCPP_USE_CONAN_DEPENDENCIES=ON \
251257
-DEXPRESSCPP_ENABLE_COVERAGE=OFF \
252258
-DEXPRESSCPP_BUILD_EXAMPLES=OFF \
253259
-DEXPRESSCPP_BUILD_TESTS=ON \

‎.vscode/settings.json‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@
7676
"unordered_set": "cpp",
7777
"hash_map": "cpp",
7878
"hash_set": "cpp",
79-
"any": "cpp"
79+
"any": "cpp",
80+
"csetjmp": "cpp",
81+
"rope": "cpp",
82+
"slist": "cpp",
83+
"scoped_allocator": "cpp"
8084
},
8185
"editor.tokenColorCustomizations": {
8286
"textMateRules": [
@@ -103,6 +107,6 @@
103107
"cmake.configureSettings": {
104108
"EXPRESSCPP_BUILD_TESTS": true,
105109
"EXPRESSCPP_BUILD_EXAMPLES": true,
106-
"EXPRESSCPP_ENABLE_STATIC_FILE_PROVIDER": true
110+
"EXPRESSCPP_USE_CONAN_DEPENDENCIES": true
107111
}
108112
}

‎CMakeLists.txt‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
cmake_minimum_required(VERSION 3.8)
22

3-
project(expresscpp
4-
LANGUAGES CXX
5-
C)
3+
project(expresscpp LANGUAGES CXX)
64

75
execute_process(COMMAND git
86
describe
@@ -19,7 +17,7 @@ endif()
1917
###
2018
option(EXPRESSCPP_USE_CONAN_DEPENDENCIES
2119
"Get dependencies from conan"
22-
ON)
20+
OFF)
2321
option(EXPRESSCPP_BUILD_EXAMPLES
2422
"Build the example executables"
2523
OFF)

‎build.sh‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ mkdir _build
77
cd _build
88
cmake .. -GNinja \
99
-DEXPRESSCPP_BUILD_EXAMPLES=ON \
10-
-DEXPRESSCPP_BUILD_TESTS=ON
10+
-DEXPRESSCPP_BUILD_TESTS=ON \
11+
-DEXPRESSCPP_USE_CONAN_DEPENDENCIES=ON
1112

1213
cmake --build . -j
1314

‎example/simple_rest_router_server.cpp‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,28 +73,28 @@ int main() {
7373
auto api_v1_router = expresscpp->GetRouter("api v1 router");
7474
expresscpp->Use("/api/v1", api_v1_router);
7575

76-
api_v1_router->Get("/", [](auto /*req*/, auto res) { res->Send("work in progress"); });
76+
api_v1_router->Get("/", [](auto /*req*/, auto res, auto next) { res->Send("work in progress"); });
7777

78-
api_v1_router->Post("/", [](auto /*req*/, auto res) { res->Send("work in progress"); });
78+
api_v1_router->Post("/", [](auto /*req*/, auto res, auto next) { res->Send("work in progress"); });
7979

8080
auto user_v1_router = expresscpp->GetRouter("api v1 user router");
8181
api_v1_router->Use("/user", user_v1_router);
8282

83-
user_v1_router->Get("/me", [](auto /*req*/, auto res) { res->Send("work in progress"); });
83+
user_v1_router->Get("/me", [](auto /*req*/, auto res, auto next) { res->Send("work in progress"); });
8484

8585
// Things
8686
auto things_router = expresscpp->GetRouter("thing router");
8787
api_router->Use("/things", things_router);
8888

89-
things_router->Get("/", [](auto /*req*/, auto res) {
89+
things_router->Get("/", [](auto /*req*/, auto res, auto next) {
9090
std::cout << "things called" << std::endl;
9191
res->Send("getting all things");
9292
});
93-
things_router->Post("/", [](auto /*req*/, auto res) {
93+
things_router->Post("/", [](auto /*req*/, auto res, auto next) {
9494
std::cout << "things called" << std::endl;
9595
res->Send("adding thing");
9696
});
97-
things_router->Get("/:thing_id/status", [](auto /*req*/, auto res) {
97+
things_router->Get("/:thing_id/status", [](auto /*req*/, auto res, auto next) {
9898
std::cout << "things called" << std::endl;
9999
res->Send("getting status of things");
100100
});

‎include/expresscpp/expresscpp.hpp‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,9 @@ class ExpressCpp {
8585
std::function<void(const std::error_code)> callback);
8686

8787
private:
88-
void RegisterPath(const std::string_view path, const HttpMethod method, express_handler_wn_t handler);
8988
void Init();
9089

91-
void lazyrouter();
92-
93-
std::shared_ptr<Router> _router;
90+
std::unique_ptr<Router> router_;
9491

9592
#ifdef EXPRESSCPP_ENABLE_STATIC_FILE_PROVIDER
9693
std::vector<StaticFileProviderPtr> static_file_providers_;

‎include/expresscpp/router.hpp‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,17 @@ class Router {
3535
*/
3636
void Use(std::string_view path, express_handler_wn_t handler);
3737

38-
void Get(std::string_view path, express_handler_t handler);
39-
void Post(std::string_view path, express_handler_t handler);
40-
void Delete(std::string_view path, express_handler_t handler);
38+
void Get(std::string_view path, express_handler_wn_t handler);
39+
void Post(std::string_view path, express_handler_wn_t handler);
40+
void Delete(std::string_view path, express_handler_wn_t handler);
41+
void Patch(std::string_view path, express_handler_wn_t handler);
4142

42-
void RegisterPath(std::string_view path, HttpMethod method, express_handler_t handler);
43+
void RegisterPath(std::string_view registered_path, const HttpMethod method, express_handler_wn_t handler);
4344

4445
void HandleRequest(std::shared_ptr<Request> req, std::shared_ptr<Response> res);
4546

47+
void SetParentPath(std::string_view parent_path);
48+
4649
//! @brief returns a router which then can use used to serve some paths
4750
auto GetRouter();
4851
auto GetRouter(std::string_view name);
@@ -99,6 +102,7 @@ class Router {
99102

100103
std::string_view name_{"unknown"};
101104
std::vector<std::shared_ptr<Layer>> stack_;
105+
std::string_view parent_path_;
102106
};
103107

104108
typedef std::shared_ptr<Router> RouterPtr;

‎src/expresscpp.cpp‎

Lines changed: 14 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,22 @@ ExpressCpp::~ExpressCpp() {
3939

4040
void ExpressCpp::Get(std::string_view registered_path, express_handler_wn_t handler) {
4141
assert(handler != nullptr);
42-
RegisterPath(registered_path, HttpMethod::Get, handler);
42+
router_->Get(registered_path, handler);
4343
}
4444

4545
void ExpressCpp::Post(std::string_view registered_path, express_handler_wn_t handler) {
4646
assert(handler != nullptr);
47-
RegisterPath(registered_path, HttpMethod::Post, handler);
47+
router_->Post(registered_path, handler);
4848
}
4949

5050
void ExpressCpp::Delete(std::string_view registered_path, express_handler_wn_t handler) {
5151
assert(handler != nullptr);
52-
RegisterPath(registered_path, HttpMethod::Delete, handler);
52+
router_->Delete(registered_path, handler);
5353
}
5454

5555
void ExpressCpp::Patch(std::string_view registered_path, express_handler_wn_t handler) {
5656
assert(handler != nullptr);
57-
RegisterPath(registered_path, HttpMethod::Patch, handler);
57+
router_->Patch(registered_path, handler);
5858
}
5959

6060
void ExpressCpp::Error(express_handler_wecn_t handler) {
@@ -65,8 +65,7 @@ void ExpressCpp::Error(express_handler_wecn_t handler) {
6565
}
6666

6767
std::shared_ptr<Route> ExpressCpp::CreateRoute(const std::string_view registered_path) {
68-
lazyrouter();
69-
return _router->CreateRoute(registered_path);
68+
return router_->CreateRoute(registered_path);
7069
}
7170

7271
void ExpressCpp::Use(express_handler_t handler) {
@@ -77,8 +76,7 @@ void ExpressCpp::Use(express_handler_t handler) {
7776

7877
void ExpressCpp::Use(express_handler_wn_t handler) {
7978
Console::Debug("using handler for all paths");
80-
lazyrouter();
81-
_router->Use("/", handler);
79+
router_->Use("/", handler);
8280
}
8381

8482
void ExpressCpp::Use(std::string_view registered_path, express_handler_t handler) {
@@ -87,14 +85,12 @@ void ExpressCpp::Use(std::string_view registered_path, express_handler_t handler
8785
}
8886

8987
void ExpressCpp::Use(std::string_view registered_path, express_handler_wn_t handler) {
90-
RegisterPath(registered_path, HttpMethod::All, handler);
88+
router_->RegisterPath(registered_path, HttpMethod::All, handler);
9189
}
9290

9391
void ExpressCpp::Use(std::string_view registered_path, RouterPtr router) {
9492
Console::Debug(fmt::format(R"(adding router "{}" to path "{}")", router->GetName(), registered_path));
95-
// RegisterPath(
96-
// registered_path, HttpMethod::All, [&](auto req, auto res) { router->HandleRequest(req, res); }, true);
97-
throw std::runtime_error("not implemented yet");
93+
router_->Use(registered_path, router);
9894
}
9995

10096
ExpressCpp& ExpressCpp::Listen(const uint16_t port, ready_fn_cb_error_code_t callback) {
@@ -132,8 +128,8 @@ ExpressCpp& ExpressCpp::Listen(const uint16_t port, ready_fn_cb_error_code_t cal
132128

133129
#ifdef EXPRESSCPP_ENABLE_STATIC_FILE_PROVIDER
134130
void ExpressCpp::Use(StaticFileProviderPtr static_file_provider) {
135-
RegisterPath("/", HttpMethod::Get,
136-
[&](auto req, auto res, auto /*next*/) { static_file_provider->HandleRequests(req, res); });
131+
router_->RegisterPath("/", HttpMethod::Get,
132+
[&](auto req, auto res, auto /*next*/) { static_file_provider->HandleRequests(req, res); });
137133
}
138134

139135
void ExpressCpp::Use(std::string_view path, StaticFileProviderPtr static_file_provider) {
@@ -201,15 +197,15 @@ void ExpressCpp::HandleRequest(express_request_t req, express_response_t res, st
201197
};
202198
}
203199

204-
if (_router == nullptr) {
200+
if (router_ == nullptr) {
205201
Console::Error("no routes defined on app");
206202
callback();
207203
return;
208204
}
209205

210206
Console::Debug(fmt::format(R"(handling request: "{}" "{}")", getHttpMethodName(req->getMethod()), req->getPath()));
211207

212-
_router->HandleRequest(req, res);
208+
router_->HandleRequest(req, res);
213209
} catch (...) {
214210
auto eptr = std::current_exception();
215211
if (error_handler_registered_) {
@@ -238,7 +234,7 @@ void ExpressCpp::HandleRequest(std::error_code ec, express_request_t req, expres
238234
std::vector<RoutingStack> ExpressCpp::Stack() const {
239235
std::vector<RoutingStack> routing_stack;
240236

241-
for (const auto& l : _router->stack()) {
237+
for (const auto& l : router_->stack()) {
242238
// if route is nullptr -> this means it is a middleware
243239
if (l->getRoute() != nullptr) {
244240
for (const auto& ll : l->getRoute()->stack_) {
@@ -256,31 +252,11 @@ std::vector<RoutingStack> ExpressCpp::Stack() const {
256252
return routing_stack;
257253
}
258254

259-
void ExpressCpp::RegisterPath(const std::string_view registered_path, const HttpMethod method,
260-
express_handler_wn_t handler) {
261-
lazyrouter();
262-
263-
auto route = _router->CreateRoute(registered_path);
264-
Console::Debug(fmt::format("registering path \"{}\"", registered_path));
265-
266-
PathToRegExpOptions op;
267-
auto layer = std::make_shared<Layer>("/", op, handler);
268-
layer->setMethod(method);
269-
route->methods_.insert(method);
270-
route->stack_.push_back(layer);
271-
}
272-
273255
void ExpressCpp::Init() {
274256
Console::Debug("ExpressCpp created");
275-
257+
router_ = std::make_unique<Router>("base router");
276258
finished = false;
277259
InstallSignalHandler();
278260
}
279261

280-
void ExpressCpp::lazyrouter() {
281-
if (_router == nullptr) {
282-
_router = std::make_shared<Router>("base router");
283-
}
284-
}
285-
286262
} // namespace expresscpp

‎src/request.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Request::Request(std::string_view path, HttpMethod method) : path_(path), method
2222
void Request::Init() {
2323
timestamp_ = std::chrono::system_clock::now();
2424
uuid_ = boost::uuids::random_generator()();
25+
originalUrl_ = path_;
2526
}
2627

2728
std::string Request::getBody() const {

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL