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 class

AutoCreation - The flag for automatically creating, user can set this flag to false for classes that have nondefault constructors.

Mentioned in

Inheritance

Ancestors: HttpControllerBase, DrObject

Methods

registerMethod
registerMethodViaRegex

Source

Lines 58-142 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>::classTypeName());
            LOG_TRACE << "classname:" << HttpController<T>::classTypeName();

            // transform(path.begin(), path.end(), path.begin(), tolower);
            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 &regExp,
        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 &registrator_;
    }
};





Add Discussion as Guest

Log in to DocsForge