2020-06-08 13:01:42 +02:00
|
|
|
#include <iostream>
|
2020-06-08 22:00:49 +02:00
|
|
|
#include <thread>
|
2020-06-19 22:11:04 +02:00
|
|
|
#include <unistd.h>
|
2020-06-08 22:00:49 +02:00
|
|
|
|
2020-06-08 13:39:53 +02:00
|
|
|
#include "webserver.hh"
|
2020-06-08 13:01:42 +02:00
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2020-06-17 16:35:26 +02:00
|
|
|
unsigned short port;
|
2020-06-17 17:40:36 +02:00
|
|
|
std::unique_ptr<pEp::Webserver> web(
|
|
|
|
pEp::Webserver::probing_port_range(pEp::net::ip::address::from_string("127.0.0.1"),
|
|
|
|
8080, 8089, port, "htdocs")
|
|
|
|
);
|
2020-06-17 16:35:26 +02:00
|
|
|
if (!web) {
|
|
|
|
std::cerr << "cannot start webserver, no ports free\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << "serving http://127.0.0.1:" << port << "/test.html\n";
|
|
|
|
|
|
|
|
std::cout << "adding handler for http://127.0.0.1:" << port << "/handler/sample\n";
|
|
|
|
web->add_url_handler("/handler/(?<name>\\w+)",
|
2020-06-12 11:32:47 +02:00
|
|
|
[](boost::cmatch m, const pEp::Webserver::request& req)->pEp::Webserver::response {
|
|
|
|
auto res = pEp::Webserver::response{pEp::http::status::ok, req.version()};
|
2020-06-08 23:12:05 +02:00
|
|
|
|
|
|
|
// https://www.boost.org/doc/libs/1_73_0/libs/beast/doc/html/beast/ref/boost__beast__http__response.html
|
|
|
|
|
2020-06-12 16:17:13 +02:00
|
|
|
res.set(pEp::http::field::content_type, "text/json; charset=utf-8");
|
|
|
|
res.keep_alive(req.keep_alive());
|
2020-06-08 23:12:05 +02:00
|
|
|
|
2020-06-12 16:17:13 +02:00
|
|
|
res.body() =
|
2020-06-08 23:12:05 +02:00
|
|
|
"{"
|
|
|
|
"\"Herausgeber\": \"Xema\","
|
|
|
|
"\"Nummer\": \"1234-5678-9012-3456\","
|
|
|
|
"\"Deckung\": 2e+6,"
|
|
|
|
"\"Waehrung\": \"EURO\","
|
|
|
|
"\"Inhaber\":"
|
|
|
|
"{"
|
|
|
|
"\"Name\": \"Mustermann\","
|
|
|
|
"\"Vorname\": \"Max\","
|
|
|
|
"\"maennlich\": true,"
|
|
|
|
"\"Hobbys\": [\"Reiten\", \"Golfen\", \"Lesen\"],"
|
|
|
|
"\"Alter\": 42,"
|
|
|
|
"\"Kinder\": [],"
|
|
|
|
"\"Partner\": null"
|
|
|
|
"}"
|
|
|
|
"}";
|
|
|
|
|
2020-06-12 16:17:13 +02:00
|
|
|
res.prepare_payload();
|
2020-06-08 23:12:05 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-06-17 16:35:26 +02:00
|
|
|
web->run();
|
2020-06-19 22:11:04 +02:00
|
|
|
std::cin.get();
|
|
|
|
web->shutdown();
|
2020-06-08 23:12:05 +02:00
|
|
|
|
2020-06-19 22:18:25 +02:00
|
|
|
exit(0); // avoid SIGABRT
|
|
|
|
|
2020-06-08 13:01:42 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2020-06-08 19:37:27 +02:00
|
|
|
|