diff --git a/http_server.hpp b/http_server.hpp index a0d20f5c..709dbee5 100644 --- a/http_server.hpp +++ b/http_server.hpp @@ -15,21 +15,13 @@ #include "cookie.hpp" namespace cinatra { + //cache - template - auto operator||(T n, U l) { - return std::tuple_cat(n, l); - } - - template - auto operator||(std::tuple n, const U& l) { - return l; - } - - template - auto operator||(const T& n, std::tuple l) { - return n; - } + template + struct enable_cache { + enable_cache(T t) :value(t) {} + T value; + }; template class http_server_ : private noncopyable { @@ -43,7 +35,7 @@ namespace cinatra { init_conn_callback(); } - void enable_cache(bool b) { + void enable_http_cache(bool b) { http_cache::enable_cache(b); } @@ -158,8 +150,8 @@ namespace cinatra { template bool need_cache(T&& t) { - if constexpr(std::is_same_v) { - return t; + if constexpr(std::is_same_v>) { + return t.value; } else { return false; @@ -169,7 +161,7 @@ namespace cinatra { //set http handlers template void set_http_handler(std::string_view name, Function&& f, AP&&... ap) { - if constexpr(has_type...>>::value) {//for cache + if constexpr(has_type, std::tuple...>>::value) {//for cache bool b = false; ((!b&&(b = need_cache(std::forward(ap))), false),...); if (b) { @@ -186,11 +178,6 @@ namespace cinatra { } } - template - void set_static_res_handler(Function&& f, AP&&... ap) { - http_router_.register_handler(STAIC_RES, std::forward(f), std::forward(ap)...); - } - void set_base_path(const std::string& key,const std::string& path) { base_path_[0] = std::move(key); @@ -275,5 +262,20 @@ namespace cinatra { http_handler http_handler_ = nullptr; }; + template + auto operator||(T n, U l) { + return std::tuple_cat(n, l); + } + + template + auto operator||(std::tuple> n, const U& l) { + return l; + } + + template + auto operator||(const T& n, std::tuple> l) { + return n; + } + using http_server = http_server_; } \ No newline at end of file diff --git a/main.cpp b/main.cpp index e027dd9a..f7f2a2de 100644 --- a/main.cpp +++ b/main.cpp @@ -50,10 +50,10 @@ int main() { } server.set_base_path("base_path","/feather"); - server.enable_cache(true);//set global cache + 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();