From c89cafa820ec02f041c3b0e52877bc321f6a1ba9 Mon Sep 17 00:00:00 2001 From: ipknHama Date: Sun, 7 Sep 2014 01:24:45 +0900 Subject: add Middlewares template to Crow main class, context implementation --- examples/example.cpp | 2 +- examples/example_chat.cpp | 2 +- include/crow.h | 19 +++++++++++- include/http_request.h | 2 ++ include/middleware.h | 50 ++++++++++++++++++++++++++++++ include/middleware_context.h | 32 +++++++++++++++++++ include/utility.h | 73 ++++++++++++++++++++++++++++++++++++++++++++ tests/unittest.cpp | 14 ++++----- 8 files changed, 184 insertions(+), 10 deletions(-) create mode 100644 include/middleware.h create mode 100644 include/middleware_context.h diff --git a/examples/example.cpp b/examples/example.cpp index b8e58f8..b360adb 100644 --- a/examples/example.cpp +++ b/examples/example.cpp @@ -12,7 +12,7 @@ class ExampleLogHandler : public crow::ILogHandler { int main() { - crow::Crow app; + crow::SimpleApp app; CROW_ROUTE(app, "/") .name("hello") diff --git a/examples/example_chat.cpp b/examples/example_chat.cpp index b12926a..31f16c5 100644 --- a/examples/example_chat.cpp +++ b/examples/example_chat.cpp @@ -28,7 +28,7 @@ void broadcast(const string& msg) int main() { - crow::App app; + crow::SimpleApp app; crow::mustache::set_base("."); CROW_ROUTE(app, "/") diff --git a/include/crow.h b/include/crow.h index 55572bf..a018f31 100644 --- a/include/crow.h +++ b/include/crow.h @@ -13,6 +13,8 @@ #include "http_server.h" #include "utility.h" #include "routing.h" +#include "middleware_impl.h" +#include "http_request.h" // TEST #include @@ -21,6 +23,7 @@ namespace crow { + template class Crow { public: @@ -78,12 +81,26 @@ namespace crow router_.debug_print(); } + // middleware + using context_t = detail::context; + template + T& get_middleware_context(request& req) + { + static_assert(black_magic::contains::value, "App doesn't have the specified middleware type."); + auto& ctx = *reinterpret_cast(req.middleware_context); + return ctx.get(); + } + private: uint16_t port_ = 80; uint16_t concurrency_ = 1; + std::tuple middlewares_; + Router router_; }; - using App = Crow; + template + using App = Crow; + using SimpleApp = Crow<>; }; diff --git a/include/http_request.h b/include/http_request.h index 83b6059..77b3ecb 100644 --- a/include/http_request.h +++ b/include/http_request.h @@ -10,5 +10,7 @@ namespace crow std::string url; std::unordered_map headers; std::string body; + + void* middleware_context; }; } diff --git a/include/middleware.h b/include/middleware.h new file mode 100644 index 0000000..270e026 --- /dev/null +++ b/include/middleware.h @@ -0,0 +1,50 @@ +#pragma once +#include "http_request.h" +#include "http_response.h" + +namespace crow +{ + class CookieParser + { + struct context + { + std::unordered_map jar; + }; + + template + void before_handle(request& req, response& res, context& ctx, AllContext& all_ctx) + { + // ctx == all_ctx.bind() + // ctx.jar[] = ; + } + + template + void after_handle(request& req, response& res, context& ctx, AllContext& all_ctx) + { + } + } + + /* + App app; + A B C + A::context + int aa; + + ctx1 : public A::context + ctx2 : public ctx1, public B::context + ctx3 : public ctx2, public C::context + + C depends on A + + C::handle + context.aaa + + App::context : private CookieParser::contetx, ... + { + jar + + } + + SimpleApp + */ +} diff --git a/include/middleware_context.h b/include/middleware_context.h new file mode 100644 index 0000000..6dbf923 --- /dev/null +++ b/include/middleware_context.h @@ -0,0 +1,32 @@ +#pragma once + +#include "utility.h" + +namespace crow +{ + namespace detail + { + template + struct partial_context + : public black_magic::pop_back::template rebind + , public black_magic::last_element_type::type::context + { + }; + + template <> + struct partial_context<> + { + }; + + template + struct context : private partial_context + //struct context : private Middlewares::context... // simple but less type-safe + { + template + typename T::context& get() + { + return static_cast(*this); + } + }; + } +} diff --git a/include/utility.h b/include/utility.h index a46d577..35ea848 100644 --- a/include/utility.h +++ b/include/utility.h @@ -2,6 +2,8 @@ #include #include +#include +#include namespace crow { @@ -211,5 +213,76 @@ template using type = S<>; }; + template + struct last_element_type + { + using type = typename std::tuple_element>::type; + }; + + + template <> + struct last_element_type<> + { + }; + + + // from http://stackoverflow.com/questions/13072359/c11-compile-time-array-with-logarithmic-evaluation-depth + template using Invoke = typename T::type; + + template struct seq{ using type = seq; }; + + template struct concat; + + template + struct concat, seq> + : seq{}; + + template + using Concat = Invoke>; + + template struct gen_seq; + template using GenSeq = Invoke>; + + template + struct gen_seq : Concat, GenSeq>{}; + + template<> struct gen_seq<0> : seq<>{}; + template<> struct gen_seq<1> : seq<0>{}; + + template + struct pop_back_helper; + + template + struct pop_back_helper, Tuple> + { + template