main.cc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // main.cc
  3. //
  4. // Copyright (c) 2024 Yuji Hirose. All rights reserved.
  5. // MIT License
  6. //
  7. #include <chrono>
  8. #include <ctime>
  9. #include <format>
  10. #include <iomanip>
  11. #include <iostream>
  12. #include <sstream>
  13. #include <httplib.h>
  14. constexpr auto error_html = R"(<html>
  15. <head><title>{} {}</title></head>
  16. <body>
  17. <center><h1>404 Not Found</h1></center>
  18. <hr><center>cpp-httplib/{}</center>
  19. </body>
  20. </html>
  21. )";
  22. void sigint_handler(int s) { exit(1); }
  23. std::string time_local() {
  24. auto p = std::chrono::system_clock::now();
  25. auto t = std::chrono::system_clock::to_time_t(p);
  26. std::stringstream ss;
  27. ss << std::put_time(std::localtime(&t), "%d/%b/%Y:%H:%M:%S %z");
  28. return ss.str();
  29. }
  30. std::string log(auto &req, auto &res) {
  31. auto remote_user = "-"; // TODO:
  32. auto request = std::format("{} {} {}", req.method, req.path, req.version);
  33. auto body_bytes_sent = res.get_header_value("Content-Length");
  34. auto http_referer = "-"; // TODO:
  35. auto http_user_agent = req.get_header_value("User-Agent", "-");
  36. // NOTE: From NGINX defualt access log format
  37. // log_format combined '$remote_addr - $remote_user [$time_local] '
  38. // '"$request" $status $body_bytes_sent '
  39. // '"$http_referer" "$http_user_agent"';
  40. return std::format(R"({} - {} [{}] "{}" {} {} "{}" "{}")", req.remote_addr,
  41. remote_user, time_local(), request, res.status,
  42. body_bytes_sent, http_referer, http_user_agent);
  43. }
  44. int main(int argc, const char **argv) {
  45. signal(SIGINT, sigint_handler);
  46. auto base_dir = "./html";
  47. auto host = "0.0.0.0";
  48. auto port = 80;
  49. httplib::Server svr;
  50. svr.set_error_handler([](auto & /*req*/, auto &res) {
  51. auto body =
  52. std::format(error_html, res.status, httplib::status_message(res.status),
  53. CPPHTTPLIB_VERSION);
  54. res.set_content(body, "text/html");
  55. });
  56. svr.set_logger(
  57. [](auto &req, auto &res) { std::cout << log(req, res) << std::endl; });
  58. svr.set_mount_point("/", base_dir);
  59. std::cout << std::format("Serving HTTP on {0} port {1} ...", host, port)
  60. << std::endl;
  61. auto ret = svr.listen(host, port);
  62. return ret ? 0 : 1;
  63. }