Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// -lstdc++ -std=c++17 -lpthread
// https://forums.mageia.org/en/viewtopic.php?f=5&t=13294
#include <iostream>
#include <iterator>
#include <list>
#include <sstream>
// https://github.com/yhirose/cpp-httplib
#include "httplib.h"
/* max 100 thousand lines */
#define MAX_LINES 100000
#define HOST "127.0.0.1"
#define PORT 12345
using namespace std;
using namespace httplib;
int main()
{
list<string> lst;
Server srv;
mutex mut;
srv.Get("/", [&lst, &mut](const Request& req, Response& res) {
// copy strings into another area
mut.lock();
list<string> lstCopy(lst);
mut.unlock();
stringstream ss;
auto row = lstCopy.begin();
while(row != lstCopy.end())
{
ss << *row << endl;
row = next(row, 1);
}
// Send strings to client
res.set_content(ss.str(), "text/plain");
});
thread tsrv([&srv](){ srv.listen(HOST, PORT); });
while(true)
{
string line;
getline(cin, line);
mut.lock();
lst.push_back(line);
while(lst.size() > MAX_LINES) {
lst.pop_front();
}
mut.unlock();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.