a simple multithreaded webserver
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.8KB

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