Created
February 5, 2020 11:59
-
-
Save fopenp/83e4bcda41fd4822f4f2e2b178a0ba19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -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