Skip to content

Commit

Permalink
modify (#517)
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Feb 1, 2024
1 parent 782386f commit e8f41be
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 25 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ int main() {
//将信息从中间件传输到处理程序
struct get_data : public base_aspect {
bool before(coro_http_request& req, coro_http_response& res) {
req.set_aspect_data("hello", std::string("hello world"));
req.set_aspect_data("hello world");
return true;
}
}
Expand All @@ -262,8 +262,8 @@ int main() {
}, std::vector{std::make_shared<check>(), std::make_shared<log_t>()});

server.set_http_handler<GET,POST>("/aspect/data", [](coro_http_request& req, coro_http_response& res) {
std::string hello = req.get_aspect_data<std::string>("hello");
res.set_status_and_content(status_type::ok, std::move(hello));
auto& val = req.get_aspect_data();
res.set_status_and_content(status_type::ok, std::move(val[0]));
}, std::vector{std::make_shared<get_data>()});

server.sync_start();
Expand Down
7 changes: 3 additions & 4 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ struct log_t : public base_aspect {

struct get_data : public base_aspect {
bool before(coro_http_request &req, coro_http_response &res) {
req.set_aspect_data("hello", std::string("hello world"));
req.set_aspect_data("hello world");
return true;
}
};
Expand All @@ -268,9 +268,8 @@ async_simple::coro::Lazy<void> use_aspects() {
server.set_http_handler<GET>(
"/get",
[](coro_http_request &req, coro_http_response &resp) {
std::optional<std::string> val =
req.get_aspect_data<std::string>("hello");
assert(*val == "hello world");
auto &val = req.get_aspect_data();
assert(val[0] == "hello world");
resp.set_status_and_content(status_type::ok, "ok");
},
std::vector<std::shared_ptr<base_aspect>>{std::make_shared<log_t>(),
Expand Down
20 changes: 4 additions & 16 deletions include/cinatra/coro_http_request.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,23 +208,11 @@ class coro_http_request {
return true;
}

void set_aspect_data(const std::string &&key, const std::any &data) {
aspect_data_[key] = data;
void set_aspect_data(std::string data) {
aspect_data_.push_back(std::move(data));
}

template <typename T>
std::optional<T> get_aspect_data(const std::string &&key) {
auto it = aspect_data_.find(key);
if (it == aspect_data_.end()) {
return std::optional<T>{};
}

try {
return std::any_cast<T>(it->second); // throws
} catch (const std::bad_any_cast &e) {
return std::optional<T>{};
}
}
std::vector<std::string> &get_aspect_data() { return aspect_data_; }

std::unordered_map<std::string_view, std::string_view> get_cookies(
std::string_view cookie_str) const {
Expand Down Expand Up @@ -268,7 +256,7 @@ class coro_http_request {
std::string_view body_;
coro_http_connection *conn_;
bool is_websocket_;
std::unordered_map<std::string, std::any> aspect_data_;
std::vector<std::string> aspect_data_;
std::string cached_session_id_;
};
} // namespace cinatra
5 changes: 3 additions & 2 deletions lang/english/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ int main() {
//将信息从中间件传输到处理程序
struct get_data : public base_aspect {
bool before(coro_http_request& req, coro_http_response& res) {
req.set_aspect_data("hello", std::string("hello world"));
req.set_aspect_data("hello world");
return true;
}
}
Expand All @@ -247,7 +247,8 @@ int main() {
}, std::vector{std::make_shared<check>(), std::make_shared<log_t>()});

server.set_http_handler<GET,POST>("/aspect/data", [](coro_http_request& req, coro_http_response& res) {
std::string hello = req.get_aspect_data<std::string>("hello");
auto &val = req.get_aspect_data();
std::string& hello = val[0];
res.set_status_and_content(status_type::ok, std::move(hello));
}, std::vector{std::make_shared<get_data>()});

Expand Down

0 comments on commit e8f41be

Please sign in to comment.