lithium.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "lithium_http_backend.hh"
  2. #if TFB_MYSQL
  3. #include "lithium_mysql.hh"
  4. #elif TFB_PGSQL
  5. #include "lithium_pgsql.hh"
  6. #endif
  7. #include "symbols.hh"
  8. using namespace li;
  9. std::string escape_html_entities(const std::string& data)
  10. {
  11. std::string buffer;
  12. buffer.reserve(data.size());
  13. for(size_t pos = 0; pos != data.size(); ++pos) {
  14. switch(data[pos]) {
  15. case '&': buffer.append("&"); break;
  16. case '\"': buffer.append("""); break;
  17. case '\'': buffer.append("'"); break;
  18. case '<': buffer.append("&lt;"); break;
  19. case '>': buffer.append("&gt;"); break;
  20. default: buffer.append(&data[pos], 1); break;
  21. }
  22. }
  23. return std::move(buffer);
  24. }
  25. int main(int argc, char* argv[]) {
  26. if (argc != 4)
  27. {
  28. std::cerr << "Usage: " << argv[0] << " sql_host port nprocs" << std::endl;
  29. return 1;
  30. }
  31. int port = atoi(argv[2]);
  32. int nprocs = atoi(argv[3]);
  33. #if TFB_MYSQL
  34. auto sql_db =
  35. mysql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
  36. s::password = "benchmarkdbpass", s::port = 3306, s::charset = "utf8");
  37. int mysql_max_connection = sql_db.connect()("SELECT @@GLOBAL.max_connections;").read<int>() * 0.75;
  38. li::max_mysql_connections_per_thread = (mysql_max_connection / nprocs);
  39. #elif TFB_PGSQL
  40. auto sql_db =
  41. pgsql_database(s::host = argv[1], s::database = "hello_world", s::user = "benchmarkdbuser",
  42. s::password = "benchmarkdbpass", s::port = 5432, s::charset = "utf8");
  43. int pgsql_max_connection = atoi(sql_db.connect()("SHOW max_connections;").read<std::string>().c_str()) * 0.75;
  44. li::max_pgsql_connections_per_thread = (pgsql_max_connection / nprocs);
  45. #endif
  46. auto fortunes = sql_orm_schema(sql_db, "Fortune").fields(
  47. s::id(s::auto_increment, s::primary_key) = int(),
  48. s::message = std::string());
  49. auto random_numbers = sql_orm_schema(sql_db, "World").fields(
  50. s::id(s::auto_increment, s::primary_key) = int(),
  51. s::randomNumber = int());
  52. http_api my_api;
  53. my_api.get("/plaintext") = [&](http_request& request, http_response& response) {
  54. response.set_header("Content-Type", "text/plain");
  55. response.write("Hello, World!");
  56. };
  57. my_api.get("/json") = [&](http_request& request, http_response& response) {
  58. response.write_json(s::message = "Hello, World!");
  59. };
  60. my_api.get("/db") = [&](http_request& request, http_response& response) {
  61. response.write_json(random_numbers.connect(request.yield).find_one(s::id = 1234).value());
  62. };
  63. my_api.get("/queries") = [&](http_request& request, http_response& response) {
  64. std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
  65. int N = atoi(N_str.c_str());
  66. N = std::max(1, std::min(N, 500));
  67. auto c = random_numbers.connect(request.yield);
  68. std::vector<decltype(random_numbers.all_fields())> numbers(N);
  69. for (int i = 0; i < N; i++)
  70. numbers[i] = c.find_one(s::id = 1 + rand() % 9999).value();
  71. response.write_json(numbers);
  72. };
  73. my_api.get("/updates") = [&](http_request& request, http_response& response) {
  74. std::string N_str = request.get_parameters(s::N = std::optional<std::string>()).N.value_or("1");
  75. int N = atoi(N_str.c_str());
  76. N = std::max(1, std::min(N, 500));
  77. auto c = random_numbers.connect(request.yield);
  78. std::vector<decltype(random_numbers.all_fields())> numbers(N);
  79. for (int i = 0; i < N; i++)
  80. {
  81. numbers[i] = c.find_one(s::id = 1 + rand() % 9999).value();
  82. numbers[i].randomNumber = 1 + rand() % 9999;
  83. c.update(numbers[i]);
  84. }
  85. response.write_json(numbers);
  86. };
  87. my_api.get("/fortunes") = [&](http_request& request, http_response& response) {
  88. typedef decltype(fortunes.all_fields()) fortune;
  89. std::vector<fortune> table;
  90. auto c = fortunes.connect(request.yield);
  91. c.forall([&] (auto f) { table.emplace_back(f); });
  92. table.emplace_back(0, "Additional fortune added at request time.");
  93. std::sort(table.begin(), table.end(),
  94. [] (const fortune& a, const fortune& b) { return a.message < b.message; });
  95. std::stringstream ss;
  96. ss << "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>";
  97. for(auto& f : table)
  98. ss << "<tr><td>" << f.id << "</td><td>" << escape_html_entities(f.message) << "</td></tr>";
  99. ss << "</table></body></html>";
  100. response.set_header("Content-Type", "text/html; charset=utf-8");
  101. response.write(ss.str());
  102. };
  103. http_serve(my_api, port, s::nthreads = nprocs);
  104. return 0;
  105. }