diff --git a/http_server.hpp b/http_server.hpp index 709dbee5..a6bff11d 100644 --- a/http_server.hpp +++ b/http_server.hpp @@ -148,6 +148,15 @@ namespace cinatra { return (std::make_tuple(t) || ... || std::make_tuple(args)); } + template + auto filter(const T& t) { + return std::make_tuple(t); + } + + auto filter(enable_cache) { + return std::tuple<>(); + } + template bool need_cache(T&& t) { if constexpr(std::is_same_v>) { @@ -162,14 +171,14 @@ namespace cinatra { template void set_http_handler(std::string_view name, Function&& f, AP&&... ap) { if constexpr(has_type, std::tuple...>>::value) {//for cache - bool b = false; - ((!b&&(b = need_cache(std::forward(ap))), false),...); + bool b = true; + ((b&&(b = need_cache(std::forward(ap))), false),...); if (b) { http_cache::add_skip(name); } auto tp = filter(std::forward(ap)...); auto lm = [this, name, f = std::move(f)](auto... ap) { - set_http_handler(name, std::move(f), std::move(ap)...); + http_router_.register_handler(name, std::move(f), std::move(ap)...); }; std::apply(lm, std::move(tp)); } @@ -263,19 +272,23 @@ namespace cinatra { }; template - auto operator||(T n, U l) { + auto operator||(const T& n, const U& l) { return std::tuple_cat(n, l); } template - auto operator||(std::tuple> n, const U& l) { + auto operator||(std::tuple> , const U& l) { return l; } template - auto operator||(const T& n, std::tuple> l) { + auto operator||(const T& n, std::tuple> ) { return n; } + auto operator||(std::tuple> , std::tuple> ) { + return std::tuple<>(); + } + using http_server = http_server_; } \ No newline at end of file diff --git a/main.cpp b/main.cpp index f7f2a2de..96384735 100644 --- a/main.cpp +++ b/main.cpp @@ -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("/", [](const request& req, response& res) { res.set_status_and_content(status_type::ok, "hello world"); - }, enable_cache{ false }); + }); server.set_http_handler("/login", [](const request& req, response& res) { auto session = res.start_session(); @@ -104,7 +105,7 @@ int main() { json["number"] = 100.005; json["name"] = "中文"; res.render_json(json); - }); + }, enable_cache{ false }); server.set_http_handler("/redirect",[](const request& req, response& res){ res.redirect("http://www.baidu.com"); // res.redirect("/json");