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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
/*
* SPDX-FileCopyrightText: 2016 Mathieu Stefani
*
* SPDX-License-Identifier: Apache-2.0
*/
/*
Mathieu Stefani, 07 février 2016
Example of a REST endpoint with routing
*/
#include <algorithm>
#include <pistache/endpoint.h>
#include <pistache/http.h>
#include <pistache/router.h>
using namespace Pistache;
void printCookies(const Http::Request& req)
{
auto cookies = req.cookies();
std::cout << "Cookies: [" << std::endl;
const std::string indent(4, ' ');
for (const auto& c : cookies)
{
std::cout << indent << c.name << " = " << c.value << std::endl;
}
std::cout << "]" << std::endl;
}
namespace Generic
{
void handleReady(const Rest::Request&, Http::ResponseWriter response)
{
response.send(Http::Code::Ok, "1");
}
}
class StatsEndpoint
{
public:
explicit StatsEndpoint(Address addr)
: httpEndpoint(std::make_shared<Http::Endpoint>(addr))
{ }
void init(size_t thr = 2)
{
auto opts = Http::Endpoint::options()
.threads(static_cast<int>(thr));
httpEndpoint->init(opts);
setupRoutes();
}
void start()
{
httpEndpoint->setHandler(router.handler());
httpEndpoint->serve();
}
private:
void setupRoutes()
{
using namespace Rest;
Routes::Post(router, "/record/:name/:value?", Routes::bind(&StatsEndpoint::doRecordMetric, this));
Routes::Get(router, "/value/:name", Routes::bind(&StatsEndpoint::doGetMetric, this));
Routes::Get(router, "/ready", Routes::bind(&Generic::handleReady));
Routes::Get(router, "/auth", Routes::bind(&StatsEndpoint::doAuth, this));
}
void doRecordMetric(const Rest::Request& request, Http::ResponseWriter response)
{
auto name = request.param(":name").as<std::string>();
Guard guard(metricsLock);
auto it = std::find_if(metrics.begin(), metrics.end(), [&](const Metric& metric) {
return metric.name() == name;
});
int val = 1;
if (request.hasParam(":value"))
{
auto value = request.param(":value");
val = value.as<int>();
}
if (it == std::end(metrics))
{
metrics.emplace_back(std::move(name), val);
response.send(Http::Code::Created, std::to_string(val));
}
else
{
auto& metric = *it;
metric.incr(val);
response.send(Http::Code::Ok, std::to_string(metric.value()));
}
}
void doGetMetric(const Rest::Request& request, Http::ResponseWriter response)
{
auto name = request.param(":name").as<std::string>();
Guard guard(metricsLock);
auto it = std::find_if(metrics.begin(), metrics.end(), [&](const Metric& metric) {
return metric.name() == name;
});
if (it == std::end(metrics))
{
response.send(Http::Code::Not_Found, "Metric does not exist");
}
else
{
const auto& metric = *it;
response.send(Http::Code::Ok, std::to_string(metric.value()));
}
}
void doAuth(const Rest::Request& request, Http::ResponseWriter response)
{
printCookies(request);
response.cookies()
.add(Http::Cookie("lang", "en-US"));
response.send(Http::Code::Ok);
}
class Metric
{
public:
explicit Metric(std::string name, int initialValue = 1)
: name_(std::move(name))
, value_(initialValue)
{ }
int incr(int n = 1)
{
int old = value_;
value_ += n;
return old;
}
int value() const
{
return value_;
}
const std::string& name() const
{
return name_;
}
private:
std::string name_;
int value_;
};
using Lock = std::mutex;
using Guard = std::lock_guard<Lock>;
Lock metricsLock;
std::vector<Metric> metrics;
std::shared_ptr<Http::Endpoint> httpEndpoint;
Rest::Router router;
};
int main(int argc, char* argv[])
{
Port port(9080);
int thr = 2;
if (argc >= 2)
{
port = static_cast<uint16_t>(std::stol(argv[1]));
if (argc == 3)
thr = std::stoi(argv[2]);
}
Address addr(Ipv4::any(), port);
std::cout << "Cores = " << hardware_concurrency() << std::endl;
std::cout << "Using " << thr << " threads" << std::endl;
StatsEndpoint stats(addr);
stats.init(thr);
stats.start();
}
|