Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos committed May 22, 2018
1 parent 635f9ae commit a497171
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
48 changes: 25 additions & 23 deletions http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,13 @@
#include "cookie.hpp"

namespace cinatra {

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

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

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

template<class service_pool_policy = io_service_pool>
class http_server_ : private noncopyable {
Expand All @@ -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);
}

Expand Down Expand Up @@ -158,8 +150,8 @@ namespace cinatra {

template<typename T>
bool need_cache(T&& t) {
if constexpr(std::is_same_v<T, bool>) {
return t;
if constexpr(std::is_same_v<T, enable_cache<bool>>) {
return t.value;
}
else {
return false;
Expand All @@ -169,7 +161,7 @@ namespace cinatra {
//set http handlers
template<http_method... Is, typename Function, typename... AP>
void set_http_handler(std::string_view name, Function&& f, AP&&... ap) {
if constexpr(has_type<bool, std::tuple<std::decay_t<AP>...>>::value) {//for cache
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),...);
if (b) {
Expand All @@ -186,11 +178,6 @@ namespace cinatra {
}
}

template<http_method... Is, typename Function, typename... AP>
void set_static_res_handler(Function&& f, AP&&... ap) {
http_router_.register_handler<Is...>(STAIC_RES, std::forward<Function>(f), std::forward<AP>(ap)...);
}

void set_base_path(const std::string& key,const std::string& path)
{
base_path_[0] = std::move(key);
Expand Down Expand Up @@ -275,5 +262,20 @@ namespace cinatra {
http_handler http_handler_ = nullptr;
};

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

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

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

using http_server = http_server_<io_service_pool>;
}
4 changes: 2 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<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

0 comments on commit a497171

Please sign in to comment.