File: SlowCtrl.cc

package info (click to toggle)
drogon 1.9.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,096 kB
  • sloc: cpp: 52,222; sh: 249; xml: 20; makefile: 11
file content (37 lines) | stat: -rw-r--r-- 1,127 bytes parent folder | download | duplicates (2)
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
#include "SlowCtrl.h"
#include "RedisCache.h"
#include "DateFuncs.h"
#include <drogon/drogon.h>
#define VDate "visitDate"

void SlowCtrl::hello(const HttpRequestPtr &req,
                     std::function<void(const HttpResponsePtr &)> &&callback,
                     std::string &&userid)
{
    auto resp = drogon::HttpResponse::newHttpResponse();
    resp->setBody("hello, " + userid);
    callback(resp);
}

drogon::AsyncTask SlowCtrl::observe(
    HttpRequestPtr req,
    std::function<void(const HttpResponsePtr &)> callback,
    std::string userid)
{
    auto key = userid + "." + VDate;
    auto redisPtr = drogon::app().getFastRedisClient();
    try
    {
        auto date = co_await getFromCache<trantor::Date>(key, redisPtr);
        auto resp = drogon::HttpResponse::newHttpResponse();
        std::string body{"your last visit time: "};
        body.append(date.toFormattedStringLocal(false));
        resp->setBody(std::move(body));
        callback(resp);
    }
    catch (const std::exception &err)
    {
        LOG_ERROR << err.what();
        callback(drogon::HttpResponse::newNotFoundResponse());
    }
}