drogon
C++14/17-based HTTP application framework
Loading...
Searching...
No Matches
HttpSimpleController.h
1
14
15#pragma once
16
17#include <drogon/DrObject.h>
18#include <drogon/utils/HttpConstraint.h>
20#include <trantor/utils/Logger.h>
21#include <iostream>
22#include <string>
23#include <vector>
24#define PATH_LIST_BEGIN \
25 static void initPathRouting() \
26 {
27#define PATH_ADD(path, ...) registerSelf__(path, {__VA_ARGS__})
28#define PATH_LIST_END }
29
30namespace drogon
31{
37{
38 public:
47 const HttpRequestPtr &req,
48 std::function<void(const HttpResponsePtr &)> &&callback) = 0;
49
51 {
52 }
53};
54
62template <typename T, bool AutoCreation = true>
63class HttpSimpleController : public DrObject<T>, public HttpSimpleControllerBase
64{
65 public:
66 static const bool isAutoCreation = AutoCreation;
67
68 virtual ~HttpSimpleController()
69 {
70 }
71
72 protected:
73 HttpSimpleController()
74 {
75 }
76
77 static void registerSelf__(
78 const std::string &path,
79 const std::vector<internal::HttpConstraint> &constraints)
80 {
81 LOG_TRACE << "register simple controller("
82 << HttpSimpleController<T, AutoCreation>::classTypeName()
83 << ") on path:" << path;
85 path,
86 HttpSimpleController<T, AutoCreation>::classTypeName(),
87 constraints);
88 }
89
90 private:
91 class pathRegistrator
92 {
93 public:
94 pathRegistrator()
95 {
96 if (AutoCreation)
97 {
98 T::initPathRouting();
99 }
100 }
101 };
102
103 friend pathRegistrator;
104 static pathRegistrator registrator_;
105
106 virtual void *touch()
107 {
108 return &registrator_;
109 }
110};
111
112template <typename T, bool AutoCreation>
113typename HttpSimpleController<T, AutoCreation>::pathRegistrator
114 HttpSimpleController<T, AutoCreation>::registrator_;
115
116} // namespace drogon
The base class for all drogon reflection classes.
Definition DrObject.h:34
virtual HttpAppFramework & registerHttpSimpleController(const std::string &pathName, const std::string &ctrlName, const std::vector< internal::HttpConstraint > &constraints={})=0
Register a HttpSimpleController object into the framework.
The abstract base class for HTTP simple controllers.
Definition HttpSimpleController.h:37
virtual void asyncHandleHttpRequest(const HttpRequestPtr &req, std::function< void(const HttpResponsePtr &)> &&callback)=0
The function is called when a HTTP request is routed to the controller.
Drogon Test is a minimal effort test framework developed because the major C++ test frameworks doesn'...
Definition Attribute.h:23
HttpAppFramework & app()
A wrapper of the instance() method.
Definition HttpAppFramework.h:1656