Skip to content

Instantly share code, notes, and snippets.

@fopenp
Created February 5, 2020 11:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fopenp/83e4bcda41fd4822f4f2e2b178a0ba19 to your computer and use it in GitHub Desktop.
Save fopenp/83e4bcda41fd4822f4f2e2b178a0ba19 to your computer and use it in GitHub Desktop.
// -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