File: HelloController.cc

package info (click to toggle)
drogon 1.9.11%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,820 kB
  • sloc: cpp: 57,270; sh: 297; xml: 20; makefile: 11
file content (40 lines) | stat: -rw-r--r-- 1,427 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <drogon/HttpController.h>

using namespace drogon;

// HttpControllers are automatically added to Drogon upon Drogon initializing.
class SayHello : public HttpController<SayHello>
{
  public:
    METHOD_LIST_BEGIN
    // Drogon automatically appends the namespace and name of the controller to
    // the handlers of the controller. In this example, although we are adding
    // a handler to /. But because it is a part of the SayHello controller. It
    // ends up in path /SayHello/ (IMPORTANT! It is /SayHello/ not /SayHello
    // as they are different paths).
    METHOD_ADD(SayHello::genericHello, "/", Get);
    // Same for /hello. It ends up at /SayHello/hello
    METHOD_ADD(SayHello::personalizedHello, "/hello", Get);
    METHOD_LIST_END

  protected:
    void genericHello(const HttpRequestPtr &,
                      std::function<void(const HttpResponsePtr &)> &&callback)
    {
        auto resp = HttpResponse::newHttpResponse();
        resp->setBody(
            "Hello, this is a generic hello message from the SayHello "
            "controller");
        callback(resp);
    }

    void personalizedHello(
        const HttpRequestPtr &,
        std::function<void(const HttpResponsePtr &)> &&callback)
    {
        auto resp = HttpResponse::newHttpResponse();
        resp->setBody(
            "Hi there, this is another hello from the SayHello Controller");
        callback(resp);
    }
};