example.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (c) 2013 No Face Press, LLC
  3. * License http://opensource.org/licenses/mit-license.php MIT License
  4. */
  5. // Simple example program on how to use Embedded C++ interface.
  6. #include "CivetServer.h"
  7. #define DOCUMENT_ROOT "."
  8. #define PORT "8888"
  9. #define EXAMPLE_URI "/example"
  10. #define EXIT_URI "/exit"
  11. bool exitNow = false;
  12. class ExampleHandler: public CivetHandler {
  13. public:
  14. bool handleGet(CivetServer *server, struct mg_connection *conn) {
  15. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n");
  16. mg_printf(conn, "<html><body>");
  17. mg_printf(conn, "<h2>This is example text!!!</h2>");
  18. mg_printf(conn, "<p>To exit <a href=\"%s\">click here</a></p>",
  19. EXIT_URI);
  20. mg_printf(conn, "</body></html>\n");
  21. return true;
  22. }
  23. };
  24. class ExitHandler: public CivetHandler {
  25. public:
  26. bool handleGet(CivetServer *server, struct mg_connection *conn) {
  27. mg_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\n");
  28. mg_printf(conn, "Bye!\n");
  29. exitNow = true;
  30. return true;
  31. }
  32. };
  33. int main(int argc, char *argv[]) {
  34. const char * options[] = { "document_root", DOCUMENT_ROOT,
  35. "listening_ports", PORT, 0 };
  36. CivetServer server(options);
  37. server.addHandler(EXAMPLE_URI, new ExampleHandler());
  38. server.addHandler(EXIT_URI, new ExitHandler());
  39. printf("Browse files at http://localhost:%s/\n", PORT);
  40. printf("Run example at http://localhost:%s%s\n", PORT, EXIT_URI);
  41. printf("Exit at http://localhost:%s%s\n", PORT, EXIT_URI);
  42. while (!exitNow) {
  43. sleep(1);
  44. }
  45. printf("Bye!\n");
  46. return 0;
  47. }