Class HttpController
Synopsis
#include <lib/inc/drogon/HttpController.h>
template <typename T, bool AutoCreation = true>
class HttpController : public DrObject<T>, public HttpControllerBase
Description
The reflection base class template for HTTP controllers.
- Template Parameters
T
- the type of the implementation classAutoCreation
- The flag for automatically creating, user can set this flag to false for classes that have nondefault constructors.
Mentioned in
- Getting Started / A very simple example
- Controller Introduction / Controller
- Controller Introduction / Controller HttpController / Generation
- Controller Introduction / Controller HttpController / Usage
- Controller Introduction / Controller HttpController / Parameters mapping
- Wiki / Filter
- Drogon Ctl Command / Create sub command
- Drogon Ctl Command / Controller creation
Inheritance
Ancestors: HttpControllerBase, DrObject
Methods
registerMethod | ||
registerMethodViaRegex |
Source
Lines 58-144 in lib/inc/drogon/HttpController.h.
template <typename T, bool AutoCreation = true>
class HttpController : public DrObject<T>, public HttpControllerBase
{
public:
static const bool isAutoCreation = AutoCreation;
protected:
template <typename FUNCTION>
static void registerMethod(
FUNCTION &&function,
const std::string &pattern,
const std::vector<internal::HttpConstraint> &filtersAndMethods =
std::vector<internal::HttpConstraint>{},
bool classNameInPath = true,
const std::string &handlerName = "")
{
if (classNameInPath)
{
std::string path = "/";
path.append(HttpController<T, AutoCreation>::classTypeName());
LOG_TRACE << "classname:"
<< HttpController<T, AutoCreation>::classTypeName();
// transform(path.begin(), path.end(), path.begin(), [](unsigned
// char c){ return tolower(c); });
std::string::size_type pos;
while ((pos = path.find("::")) != std::string::npos)
{
path.replace(pos, 2, "/");
}
if (pattern.empty() || pattern[0] == '/')
app().registerHandler(path + pattern,
std::forward<FUNCTION>(function),
filtersAndMethods,
handlerName);
else
app().registerHandler(path + "/" + pattern,
std::forward<FUNCTION>(function),
filtersAndMethods,
handlerName);
}
else
{
std::string path = pattern;
if (path.empty() || path[0] != '/')
{
path = "/" + path;
}
app().registerHandler(path,
std::forward<FUNCTION>(function),
filtersAndMethods,
handlerName);
}
}
template <typename FUNCTION>
static void registerMethodViaRegex(
FUNCTION &&function,
const std::string ®Exp,
const std::vector<internal::HttpConstraint> &filtersAndMethods =
std::vector<internal::HttpConstraint>{},
const std::string &handlerName = "")
{
app().registerHandlerViaRegex(regExp,
std::forward<FUNCTION>(function),
filtersAndMethods,
handlerName);
}
private:
class methodRegistrator
{
public:
methodRegistrator()
{
if (AutoCreation)
T::initPathRouting();
}
};
// use static value to register controller method in framework before
// main();
static methodRegistrator registrator_;
virtual void *touch()
{
return ®istrator_;
}
};