2014-08-05 18:54:38 +00:00
|
|
|
#include "crow.h"
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2014-08-06 16:18:33 +00:00
|
|
|
#include <chrono>
|
2014-08-05 18:54:38 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
vector<string> msgs;
|
2014-08-06 16:18:33 +00:00
|
|
|
vector<pair<crow::response*, decltype(chrono::steady_clock::now())>> ress;
|
2014-08-05 18:54:38 +00:00
|
|
|
|
|
|
|
void broadcast(const string& msg)
|
|
|
|
{
|
|
|
|
msgs.push_back(msg);
|
|
|
|
crow::json::wvalue x;
|
|
|
|
x["msgs"][0] = msgs.back();
|
|
|
|
x["last"] = msgs.size();
|
2021-01-05 14:49:10 +00:00
|
|
|
string body = x.dump();
|
2021-11-25 11:45:38 +00:00
|
|
|
for (auto p : ress)
|
2014-08-05 18:54:38 +00:00
|
|
|
{
|
2014-08-06 16:18:33 +00:00
|
|
|
auto* res = p.first;
|
|
|
|
CROW_LOG_DEBUG << res << " replied: " << body;
|
2014-08-05 18:54:38 +00:00
|
|
|
res->end(body);
|
|
|
|
}
|
|
|
|
ress.clear();
|
|
|
|
}
|
2015-02-22 14:23:05 +00:00
|
|
|
// To see how it works go on {ip}:40080 but I just got it working with external build (not directly in IDE, I guess a problem with dependency)
|
2014-08-05 18:54:38 +00:00
|
|
|
int main()
|
|
|
|
{
|
2014-09-06 16:24:45 +00:00
|
|
|
crow::SimpleApp app;
|
2014-08-05 18:54:38 +00:00
|
|
|
crow::mustache::set_base(".");
|
|
|
|
|
|
|
|
CROW_ROUTE(app, "/")
|
2021-11-27 12:28:50 +00:00
|
|
|
([] {
|
2014-08-05 18:54:38 +00:00
|
|
|
crow::mustache::context ctx;
|
2021-11-25 11:45:38 +00:00
|
|
|
return crow::mustache::load("example_chat.html").render(); });
|
2014-08-05 18:54:38 +00:00
|
|
|
|
|
|
|
CROW_ROUTE(app, "/logs")
|
2021-11-27 12:28:50 +00:00
|
|
|
([] {
|
2014-08-05 18:54:38 +00:00
|
|
|
CROW_LOG_INFO << "logs requested";
|
|
|
|
crow::json::wvalue x;
|
2014-08-06 16:18:33 +00:00
|
|
|
int start = max(0, (int)msgs.size()-100);
|
|
|
|
for(int i = start; i < (int)msgs.size(); i++)
|
|
|
|
x["msgs"][i-start] = msgs[i];
|
2014-08-05 18:54:38 +00:00
|
|
|
x["last"] = msgs.size();
|
|
|
|
CROW_LOG_INFO << "logs completed";
|
2021-11-25 11:45:38 +00:00
|
|
|
return x; });
|
2014-08-05 18:54:38 +00:00
|
|
|
|
|
|
|
CROW_ROUTE(app, "/logs/<int>")
|
2021-11-27 12:28:50 +00:00
|
|
|
([](const crow::request& /*req*/, crow::response& res, int after) {
|
2014-08-05 18:54:38 +00:00
|
|
|
CROW_LOG_INFO << "logs with last " << after;
|
|
|
|
if (after < (int)msgs.size())
|
|
|
|
{
|
|
|
|
crow::json::wvalue x;
|
|
|
|
for(int i = after; i < (int)msgs.size(); i ++)
|
|
|
|
x["msgs"][i-after] = msgs[i];
|
|
|
|
x["last"] = msgs.size();
|
|
|
|
|
2021-01-05 14:49:10 +00:00
|
|
|
res.write(x.dump());
|
2014-08-05 18:54:38 +00:00
|
|
|
res.end();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-08-06 16:18:33 +00:00
|
|
|
vector<pair<crow::response*, decltype(chrono::steady_clock::now())>> filtered;
|
|
|
|
for(auto p : ress)
|
|
|
|
{
|
|
|
|
if (p.first->is_alive() && chrono::steady_clock::now() - p.second < chrono::seconds(30))
|
|
|
|
filtered.push_back(p);
|
|
|
|
else
|
|
|
|
p.first->end();
|
|
|
|
}
|
|
|
|
ress.swap(filtered);
|
|
|
|
ress.push_back({&res, chrono::steady_clock::now()});
|
|
|
|
CROW_LOG_DEBUG << &res << " stored " << ress.size();
|
2021-11-25 11:45:38 +00:00
|
|
|
} });
|
2014-08-05 18:54:38 +00:00
|
|
|
|
|
|
|
CROW_ROUTE(app, "/send")
|
2021-11-27 12:28:50 +00:00
|
|
|
.methods("GET"_method, "POST"_method)([](const crow::request& req) {
|
2014-08-05 18:54:38 +00:00
|
|
|
CROW_LOG_INFO << "msg from client: " << req.body;
|
|
|
|
broadcast(req.body);
|
2021-11-25 11:45:38 +00:00
|
|
|
return ""; });
|
2014-08-05 18:54:38 +00:00
|
|
|
|
2014-08-06 16:18:33 +00:00
|
|
|
app.port(40080)
|
2021-11-25 11:45:38 +00:00
|
|
|
//.multithreaded()
|
|
|
|
.run();
|
2014-08-05 18:54:38 +00:00
|
|
|
}
|