Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed May 23, 2018
1 parent a497171 commit ac6a992
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
25 changes: 19 additions & 6 deletions http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ namespace cinatra {
return (std::make_tuple(t) || ... || std::make_tuple(args));
}

template<typename T>
auto filter(const T& t) {
return std::make_tuple(t);
}

auto filter(enable_cache<bool>) {
return std::tuple<>();
}

template<typename T>
bool need_cache(T&& t) {
if constexpr(std::is_same_v<T, enable_cache<bool>>) {
Expand All @@ -162,14 +171,14 @@ namespace cinatra {
template<http_method... Is, typename Function, typename... AP>
void set_http_handler(std::string_view name, Function&& f, AP&&... ap) {
if constexpr(has_type<enable_cache<bool>, std::tuple<std::decay_t<AP>...>>::value) {//for cache
bool b = false;
((!b&&(b = need_cache(std::forward<AP>(ap))), false),...);
bool b = true;
((b&&(b = need_cache(std::forward<AP>(ap))), false),...);
if (b) {
http_cache::add_skip(name);
}
auto tp = filter(std::forward<AP>(ap)...);
auto lm = [this, name, f = std::move(f)](auto... ap) {
set_http_handler<Is...>(name, std::move(f), std::move(ap)...);
http_router_.register_handler<Is...>(name, std::move(f), std::move(ap)...);
};
std::apply(lm, std::move(tp));
}
Expand Down Expand Up @@ -263,19 +272,23 @@ namespace cinatra {
};

template <typename T, typename U>
auto operator||(T n, U l) {
auto operator||(const T& n, const U& l) {
return std::tuple_cat(n, l);
}

template <typename U>
auto operator||(std::tuple<enable_cache<bool>> n, const U& l) {
auto operator||(std::tuple<enable_cache<bool>> , const U& l) {
return l;
}

template <typename T>
auto operator||(const T& n, std::tuple<enable_cache<bool>> l) {
auto operator||(const T& n, std::tuple<enable_cache<bool>> ) {
return n;
}

auto operator||(std::tuple<enable_cache<bool>> , std::tuple<enable_cache<bool>> ) {
return std::tuple<>();
}

using http_server = http_server_<io_service_pool>;
}
5 changes: 3 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ int main() {

server.set_base_path("base_path","/feather");
server.enable_http_cache(true);//set global cache

server.set_http_handler<GET, POST>("/", [](const request& req, response& res) {
res.set_status_and_content(status_type::ok, "hello world");
}, enable_cache{ false });
});

server.set_http_handler<GET, POST>("/login", [](const request& req, response& res) {
auto session = res.start_session();
Expand Down Expand Up @@ -104,7 +105,7 @@ int main() {
json["number"] = 100.005;
json["name"] = "中文";
res.render_json(json);
});
}, enable_cache{ false });

server.set_http_handler<GET,POST>("/redirect",[](const request& req, response& res){
res.redirect("http://www.baidu.com"); // res.redirect("/json");
Expand Down

0 comments on commit ac6a992

Please sign in to comment.