add HttpView class

This commit is contained in:
an-tao 2018-05-29 15:15:23 +08:00
parent ffd652c4e0
commit a0f065e684
7 changed files with 148 additions and 4 deletions

View File

@ -14,6 +14,6 @@ void JsonTestController::asyncHandleHttpRequest(const HttpRequest& req,std::func
array.append(user);
}
json["rows"]=array;
auto resp=std::unique_ptr<HttpResponse>(HttpResponse::newHttpResponse(json));
auto resp=std::unique_ptr<HttpResponse>(HttpResponse::newHttpJsonResponse(json));
callback(*resp);
}

View File

@ -20,6 +20,7 @@
#pragma once
#include <drogon/HttpViewData.h>
#include <string>
#include <json/json.h>
@ -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);
};
}

12
lib/inc/drogon/HttpView.h Executable file
View File

@ -0,0 +1,12 @@
#pragma once
#include <drogon/DrObject.h>
namespace drogon {
class HttpViewBase;
template <typename T>
class HttpView:public DrObject<T>,public HttpViewBase
{
protected:
HttpView(){}
};
}

56
lib/inc/drogon/HttpViewData.h Executable file
View File

@ -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 <any>
typedef std::any Any;
#define Any_cast std::any_cast
#elif USE_BOOST
#include <boost/any.hpp>
typedef boost::any Any;
#define Any_cast boost::any_cast
#else
#error,must use c++17 or boost
#endif
#include <map>
#include <string>
typedef std::map<std::string,Any> ViewData;
namespace drogon
{
class HttpViewData
{
public:
template <typename T>
const T get(const std::string &key) const
{
auto it=viewData_.find(key);
if(it!=viewData_.end())
{
return Any_cast<T>(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_;
};
}

View File

@ -15,6 +15,9 @@
// that can be found in the License file.
#include "HttpResponseImpl.h"
#include "HttpViewBase.h"
#include <drogon/HttpViewData.h>
#include <trantor/utils/Logger.h>
#include <stdio.h>
@ -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)
{

19
lib/src/HttpViewBase.cc Executable file
View File

@ -0,0 +1,19 @@
#include "HttpViewBase.h"
#include <drogon/DrClassMap.h>
#include <trantor/utils/Logger.h>
#include <memory>
using namespace drogon;
HttpResponse* HttpViewBase::genHttpResponse(std::string viewName,const HttpViewData &data)
{
LOG_TRACE<<"http view name="<<viewName;
auto obj=std::shared_ptr<DrObjectBase>(drogon::DrClassMap::newObject(viewName));
if(obj)
{
auto view= std::dynamic_pointer_cast<HttpViewBase>(obj);
if(view)
{
return view->genHttpResponse(data);
}
}
return drogon::HttpResponse::notFoundResponse();
}

31
lib/src/HttpViewBase.h Executable file
View File

@ -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 <drogon/DrObject.h>
#include <drogon/HttpViewData.h>
#include <drogon/HttpResponse.h>
#include <map>
#include <string>
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;
};
}