From a0f065e68452c52d16f6bd9b8940ee8925a752d9 Mon Sep 17 00:00:00 2001 From: an-tao <20741618@qq.com> Date: Tue, 29 May 2018 15:15:23 +0800 Subject: [PATCH] add HttpView class --- .../static_link_example/JsonTestController.cc | 2 +- lib/inc/drogon/HttpResponse.h | 7 ++- lib/inc/drogon/HttpView.h | 12 ++++ lib/inc/drogon/HttpViewData.h | 56 +++++++++++++++++++ lib/src/HttpResponseImpl.cc | 25 ++++++++- lib/src/HttpViewBase.cc | 19 +++++++ lib/src/HttpViewBase.h | 31 ++++++++++ 7 files changed, 148 insertions(+), 4 deletions(-) create mode 100755 lib/inc/drogon/HttpView.h create mode 100755 lib/inc/drogon/HttpViewData.h create mode 100755 lib/src/HttpViewBase.cc create mode 100755 lib/src/HttpViewBase.h diff --git a/examples/static_link_example/JsonTestController.cc b/examples/static_link_example/JsonTestController.cc index b310ff59..93e2a859 100755 --- a/examples/static_link_example/JsonTestController.cc +++ b/examples/static_link_example/JsonTestController.cc @@ -14,6 +14,6 @@ void JsonTestController::asyncHandleHttpRequest(const HttpRequest& req,std::func array.append(user); } json["rows"]=array; - auto resp=std::unique_ptr(HttpResponse::newHttpResponse(json)); + auto resp=std::unique_ptr(HttpResponse::newHttpJsonResponse(json)); callback(*resp); } \ No newline at end of file diff --git a/lib/inc/drogon/HttpResponse.h b/lib/inc/drogon/HttpResponse.h index 02d45263..0b63f4cc 100755 --- a/lib/inc/drogon/HttpResponse.h +++ b/lib/inc/drogon/HttpResponse.h @@ -20,6 +20,7 @@ #pragma once +#include #include #include @@ -100,8 +101,10 @@ namespace drogon virtual std::string getBody() const=0; static HttpResponse* newHttpResponse(); - static HttpResponse* newHttpResponse(const Json::Value &data); - + static HttpResponse* notFoundResponse(); + static HttpResponse* newHttpJsonResponse(const Json::Value &data); + static HttpResponse* newHttpViewResponse(const std::string &viewName,const HttpViewData& data); + static HttpResponse* locationRedirectResponse(std::string path); }; } diff --git a/lib/inc/drogon/HttpView.h b/lib/inc/drogon/HttpView.h new file mode 100755 index 00000000..c28613cb --- /dev/null +++ b/lib/inc/drogon/HttpView.h @@ -0,0 +1,12 @@ +#pragma once + +#include +namespace drogon { + class HttpViewBase; + template + class HttpView:public DrObject,public HttpViewBase + { + protected: + HttpView(){} + }; +} diff --git a/lib/inc/drogon/HttpViewData.h b/lib/inc/drogon/HttpViewData.h new file mode 100755 index 00000000..71d2d3b4 --- /dev/null +++ b/lib/inc/drogon/HttpViewData.h @@ -0,0 +1,56 @@ +// +// Copyright 2018, An Tao. All rights reserved. +// +// Use of this source code is governed by a MIT license +// that can be found in the License file. + +#pragma once +#ifdef USE_STD_ANY + +#include +typedef std::any Any; +#define Any_cast std::any_cast + +#elif USE_BOOST + +#include +typedef boost::any Any; +#define Any_cast boost::any_cast + +#else +#error,must use c++17 or boost +#endif + + +#include +#include + +typedef std::map ViewData; +namespace drogon +{ +class HttpViewData +{ +public: + template + const T get(const std::string &key) const + { + auto it=viewData_.find(key); + if(it!=viewData_.end()) + { + return Any_cast(it->second); + } + T tmp; + return tmp; + } + void insert(const std::string& key,Any &&obj) + { + viewData_[key]=std::move(obj); + } + void insert(const std::string& key,const Any &obj) + { + viewData_[key]=obj; + } +protected: + ViewData viewData_; +}; +} diff --git a/lib/src/HttpResponseImpl.cc b/lib/src/HttpResponseImpl.cc index 09a7f95a..90a51f4b 100755 --- a/lib/src/HttpResponseImpl.cc +++ b/lib/src/HttpResponseImpl.cc @@ -15,6 +15,9 @@ // that can be found in the License file. #include "HttpResponseImpl.h" +#include "HttpViewBase.h" + +#include #include #include @@ -30,7 +33,7 @@ HttpResponse* HttpResponse::newHttpResponse() return res; } -HttpResponse* HttpResponse::newHttpResponse(const Json::Value &data) +HttpResponse* HttpResponse::newHttpJsonResponse(const Json::Value &data) { auto res=new HttpResponseImpl; res->setStatusCode(HttpResponse::k200Ok); @@ -40,6 +43,26 @@ HttpResponse* HttpResponse::newHttpResponse(const Json::Value &data) builder["indentation"] = ""; res->setBody(writeString(builder, data)); return res; +} +HttpResponse* HttpResponse::notFoundResponse() +{ + auto res=new HttpResponseImpl; + res->setStatusCode(HttpResponse::k404NotFound); + res->setCloseConnection(true); + + return res; +} +HttpResponse* HttpResponse::locationRedirectResponse(std::string path) +{ + auto res=new HttpResponseImpl; + res->setStatusCode(HttpResponse::k302Found); + res->redirect(path.c_str()); + return res; +} + +newHttpViewResponse(const std::string &viewName,const HttpViewData& data) +{ + } const std::string HttpResponseImpl::web_content_type_to_string(uint8_t contenttype) { diff --git a/lib/src/HttpViewBase.cc b/lib/src/HttpViewBase.cc new file mode 100755 index 00000000..aa5e7a62 --- /dev/null +++ b/lib/src/HttpViewBase.cc @@ -0,0 +1,19 @@ +#include "HttpViewBase.h" +#include +#include +#include +using namespace drogon; +HttpResponse* HttpViewBase::genHttpResponse(std::string viewName,const HttpViewData &data) +{ + LOG_TRACE<<"http view name="<(drogon::DrClassMap::newObject(viewName)); + if(obj) + { + auto view= std::dynamic_pointer_cast(obj); + if(view) + { + return view->genHttpResponse(data); + } + } + return drogon::HttpResponse::notFoundResponse(); +} diff --git a/lib/src/HttpViewBase.h b/lib/src/HttpViewBase.h new file mode 100755 index 00000000..ed524c03 --- /dev/null +++ b/lib/src/HttpViewBase.h @@ -0,0 +1,31 @@ +// +// Copyright 2018, An Tao. All rights reserved. +// +// Use of this source code is governed by a MIT license +// that can be found in the License file. + +#pragma once + +#include +#include +#include +#include +#include + +namespace drogon +{ + class HttpViewBase:virtual public DrObjectBase + { + public: + + static HttpResponse* genHttpResponse(std::string viewName,const HttpViewData &data); + + virtual ~HttpViewBase(){}; + HttpViewBase(){}; + protected: + + virtual HttpResponse* genHttpResponse(const HttpViewData&)=0; + }; +} + +