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

refactor: simplify code and cleanup · expresscpp/expresscpp@0d81342 · GitHub

Commit 0d81342

Browse files
Marco Czech
committed
refactor: simplify code and cleanup
1 parent b32c53d commit 0d81342

8 files changed

Lines changed: 60 additions & 62 deletions

File tree

‎include/expresscpp/console.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma once
22

3-
#if defined(__cpp_lib_source_location) || defined(__cpp_lib_experimental_source_location)
3+
#if defined(__cpp_lib_source_location) || defined(__cpp_lib_experimental_source_location)
44

55
#include <experimental/source_location>
66
#define EXPRESSCPP_CURRENT_SOURCE_LOCATION \

‎include/expresscpp/layer.hpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ namespace expresscpp {
1414
class Layer {
1515
public:
1616
Layer(const std::string_view path);
17-
Layer(const std::string_view path, PathToRegExpOptions options, std::string_view parent_path,
18-
handler_wn_t handler);
17+
Layer(const std::string_view path, PathToRegExpOptions options, std::string_view parent_path, handler_wn_t handler);
1918

2019
void SetParentPath(const std::string_view parent_path);
2120

‎include/expresscpp/nextrouter.hpp‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,20 @@
77

88
namespace expresscpp {
99

10+
class Router;
11+
class Request;
12+
class Response;
13+
1014
class NextRouter {
1115
public:
16+
NextRouter() = default;
17+
NextRouter(Router* router, std::shared_ptr<Request> req, std::shared_ptr<Response> res);
1218
void operator()(std::shared_ptr<std::string> error = nullptr);
1319

14-
void setCallback(std::function<void(std::shared_ptr<std::string>)> cb);
15-
1620
private:
17-
std::function<void(std::shared_ptr<std::string>)> cb_;
18-
bool callback_registered_{false};
21+
std::shared_ptr<Request> req_;
22+
std::shared_ptr<Response> res_;
23+
Router* router_{nullptr};
1924
};
2025

2126
typedef NextRouter next_t;

‎include/expresscpp/router.hpp‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class Router {
7272

7373
std::vector<std::shared_ptr<Layer>> stack() const;
7474

75+
void Next(std::shared_ptr<Request> req, std::shared_ptr<Response> res, std::shared_ptr<std::string> err = nullptr);
76+
7577
private:
7678
void Init();
7779

‎include/expresscpp/types.hpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ namespace expresscpp {
1111

1212
typedef std::function<void(request_t req, response_t res)> handler_t;
1313
typedef std::function<void(request_t req, response_t res, next_t next)> handler_wn_t;
14-
typedef std::function<void(std::error_code ec, request_t req, response_t res, next_t next)>
15-
express_handler_wecn_t;
14+
typedef std::function<void(std::error_code ec, request_t req, response_t res, next_t next)> express_handler_wecn_t;
1615
typedef std::function<void(const std::error_code)> ready_fn_cb_error_code_t;
1716

1817
typedef std::function<void()> ready_fn_cb_void_t;

‎src/nextrouter.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "expresscpp/nextrouter.hpp"
22

33
#include "expresscpp/console.hpp"
4+
#include "expresscpp/router.hpp"
45

56
namespace expresscpp {
67

@@ -9,16 +10,15 @@ void NextRouter::operator()(std::shared_ptr<std::string> error) {
910
if (error != nullptr) {
1011
Console::Error(*error);
1112
}
12-
if(!callback_registered_){
13+
if (!router_) {
1314
Console::Trace("no callback for next handler registered");
1415
return;
1516
}
16-
cb_(error);
17+
router_->Next(req_, res_, error);
1718
}
1819

19-
void NextRouter::setCallback(std::function<void(std::shared_ptr<std::string>)> cb) {
20-
cb_ = cb;
21-
callback_registered_ = true;
20+
NextRouter::NextRouter(Router* router, std::shared_ptr<Request> req, std::shared_ptr<Response> res)
21+
: req_{std::move(req)}, res_{std::move(res)}, router_{router} {
2222
}
2323

2424
} // namespace expresscpp

‎src/router.cpp‎

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#include "expresscpp/router.hpp"
22

3+
#include <functional>
34
#include <memory>
45

5-
#include "boost/uuid/uuid_generators.hpp"
66
#include "boost/algorithm/string.hpp"
7+
#include "boost/uuid/uuid_generators.hpp"
78
#include "boost/uuid/uuid_io.hpp"
89
#include "expresscpp/console.hpp"
910
#include "expresscpp/impl/matcher.hpp"
@@ -86,64 +87,56 @@ void Router::Use(std::string_view path, std::shared_ptr<Router> router) {
8687
stack_.emplace_back(layer);
8788
}
8889

90+
void Router::Next(std::shared_ptr<Request> req, std::shared_ptr<Response> res, std::shared_ptr<std::string> err) {
91+
if (err != nullptr) {
92+
Console::Error(fmt::format("next error: {}", *err));
93+
return;
94+
}
95+
while (req->match != true && req->idx < stack_.size()) {
96+
req->current_layer = stack_[req->idx++];
97+
req->match = matchLayer(req->current_layer, req->getPath());
98+
99+
req->current_route = req->current_layer->getRoute();
100+
101+
// no match
102+
if (req->match == false) {
103+
Console::Debug(fmt::format("no match for path \"{}\", layer \"{}\"", req->getPath(), req->current_layer->path_));
104+
continue;
105+
}
106+
107+
if (!req->current_route) {
108+
// process non-route handlers normally
109+
break;
110+
}
111+
112+
const auto method = req->getMethod();
113+
const auto has_method = req->current_route->handles_method(method);
114+
115+
// don't even bother matching route
116+
if (!has_method && method != HttpMethod::Head) {
117+
req->match = false;
118+
continue;
119+
}
120+
req->SetParams(req->current_layer->params_);
121+
req->SetQueryParams(req->current_layer->query_params_);
122+
req->SetQueryString(req->current_layer->query_string_);
123+
}
124+
}
125+
89126
void Router::HandleRequest(std::shared_ptr<Request> req, std::shared_ptr<Response> res) {
90127
assert(req != nullptr);
91128
assert(res != nullptr);
92129

93130
Console::Debug(fmt::format("dispatching request path: \"{}\", method \"{}\"", req->getPath(),
94131
getHttpMethodName(req->getMethod())));
95132

96-
auto protohost = ""s;
97-
auto removed = ""s;
98-
99133
// find next matching layer
100134
auto layerError = ""s;
101-
auto stack = stack_;
102135
req->idx = 0;
103-
104-
auto next = [req, res, stack](std::shared_ptr<std::string> err = nullptr) {
105-
if (err != nullptr) {
106-
Console::Error(fmt::format("next error: {}", *err));
107-
return;
108-
}
109-
while (req->match != true && req->idx < stack.size()) {
110-
req->current_layer = stack[req->idx++];
111-
req->match = matchLayer(req->current_layer, req->getPath());
112-
113-
req->current_route = req->current_layer->getRoute();
114-
115-
// no match
116-
if (req->match == false) {
117-
Console::Debug(
118-
fmt::format("no match for path \"{}\", layer \"{}\"", req->getPath(), req->current_layer->path_));
119-
continue;
120-
}
121-
122-
if (!req->current_route) {
123-
// process non-route handlers normally
124-
break;
125-
}
126-
127-
const auto method = req->getMethod();
128-
const auto has_method = req->current_route->handles_method(method);
129-
130-
// don't even bother matching route
131-
if (!has_method && method != HttpMethod::Head) {
132-
req->match = false;
133-
continue;
134-
}
135-
req->SetParams(req->current_layer->params_);
136-
req->SetQueryParams(req->current_layer->query_params_);
137-
req->SetQueryString(req->current_layer->query_string_);
138-
}
139-
};
140-
141-
next();
142-
NextRouter next_handler;
136+
Next(req, res);
137+
NextRouter next_handler(this, req, res);
143138
while (req->match == true) {
144139
req->match = false;
145-
146-
next_handler.setCallback(next);
147140
req->current_layer->HandleRequest(req, res, next_handler);
148141
// will be reset to true in the next() function if there is another matching layer
149142
}

‎test/static_file_middleware.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ TEST(StaticFileMiddleware, ServeIndexHtml) {
4747
assert(std::filesystem::exists(path_to_index_html));
4848
}
4949

50-
// create other doc files such as json
50+
// create other doc files such as json
5151
{
5252
std::filesystem::path path_to_doc = doc_root + "/doc.json";
5353
std::ofstream index_doc_file(path_to_doc);

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL