main.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <iostream>
  2. #include <httplib.h>
  3. #include <noolite.h>
  4. namespace App {
  5. constexpr auto DefaultPort = 8888;
  6. constexpr auto DefaultHost = "0.0.0.0";
  7. static httplib::Server *srv = nullptr;
  8. }
  9. void signalHandler(int signum) {
  10. std::cout << "\n\nInterrupt signal (" << signum << ") received.\n";
  11. if (App::srv) {
  12. App::srv->stop();
  13. delete App::srv;
  14. }
  15. exit(signum);
  16. }
  17. int main(int argc, char *argv[])
  18. {
  19. std::signal(SIGINT, signalHandler);
  20. App::srv = new httplib::Server();
  21. App::srv->Get("/", [](const httplib::Request &, httplib::Response &res) {
  22. noolitelib::Noolite adapter;
  23. if (adapter.sendCommand(0, noolitelib::Switch)) {
  24. res.set_content("{\"Result\":\"Successful\"}", "application/json");
  25. } else {
  26. res.set_content("{\"Result\":\"Failed\"}", "application/json");
  27. }
  28. });
  29. auto host = App::DefaultHost;
  30. auto port = App::DefaultPort;
  31. if (argc > 1) {
  32. host = argv[1];
  33. }
  34. if (argc > 2) {
  35. port = atoi(argv[2]);
  36. }
  37. App::srv->listen(host, port);
  38. return 0;
  39. }