main.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <algorithm>
  2. #include <unistd.h>
  3. #include <iostream>
  4. #include <silicon/backends/mhd.hh>
  5. #include <silicon/api.hh>
  6. #include <silicon/middlewares/mysql_connection.hh>
  7. #include <silicon/middlewares/mysql_orm.hh>
  8. #include "symbols.hh"
  9. using namespace s;
  10. using namespace sl;
  11. typedef decltype(D(_id(_auto_increment, _primary_key) = int(),
  12. _randomNumber = int())) random_number;
  13. typedef decltype(D(_id(_auto_increment, _primary_key) = int(),
  14. _message = std::string())) fortune;
  15. typedef mysql_orm_factory<random_number> rn_orm_factory;
  16. typedef mysql_orm<random_number> rn_orm;
  17. typedef mysql_orm_factory<fortune> fortune_orm_factory;
  18. typedef mysql_orm<fortune> fortune_orm;
  19. std::string escape_html_entities(const std::string& data)
  20. {
  21. std::string buffer;
  22. buffer.reserve(data.size());
  23. for(size_t pos = 0; pos != data.size(); ++pos) {
  24. switch(data[pos]) {
  25. case '&': buffer.append("&amp;"); break;
  26. case '\"': buffer.append("&quot;"); break;
  27. case '\'': buffer.append("&apos;"); break;
  28. case '<': buffer.append("&lt;"); break;
  29. case '>': buffer.append("&gt;"); break;
  30. default: buffer.append(&data[pos], 1); break;
  31. }
  32. }
  33. return std::move(buffer);
  34. }
  35. int main(int argc, char* argv[])
  36. {
  37. if (argc != 3)
  38. {
  39. std::cerr << "Usage: " << argv[0] << " mysql_host port" << std::endl;
  40. return 1;
  41. }
  42. auto hello_api = make_api(
  43. _plaintext = [] () { return response(_content_type = "text/plain",
  44. _body = "Hello, World!"); },
  45. _json = [] () { return response(_content_type = "application/json",
  46. _body = D(_message = "Hello, World!")); },
  47. _db = [] (rn_orm& orm) {
  48. random_number r;
  49. orm.find_by_id(1245, r);
  50. return response(_content_type = "application/json",
  51. _body = r);
  52. },
  53. _queries = [] (rn_orm& orm, get_parameters& get_params) {
  54. int N = atoi(get_params["queries"].c_str());
  55. N = std::max(1, std::min(N, 500));
  56. std::vector<random_number> qs(N);
  57. for (int i = 0; i < N; i++)
  58. orm.find_by_id(1 + rand() % 9999, qs[i]);
  59. return response(_content_type = "application/json",
  60. _body = std::move(qs));
  61. },
  62. _updates = [] (rn_orm& orm, get_parameters& get_params) {
  63. int N = atoi(get_params["queries"].c_str());
  64. N = std::max(1, std::min(N, 500));
  65. std::vector<random_number> qs(N);
  66. for (int i = 0; i < N; i++)
  67. {
  68. orm.find_by_id(1 + rand() % 9999, qs[i]);
  69. qs[i].randomNumber = 1 + rand() % 9999;
  70. orm.update(qs[i]);
  71. }
  72. return response(_content_type = "application/json",
  73. _body = std::move(qs));
  74. },
  75. _fortunes = [] (fortune_orm& orm) {
  76. std::vector<fortune> table;
  77. orm.forall([&] (fortune& f) { table.push_back(f); });
  78. table.push_back(fortune(0, "Additional fortune added at request time."));
  79. std::sort(table.begin(), table.end(),
  80. [] (const fortune& a, const fortune& b) { return a.message < b.message; });
  81. std::stringstream ss;
  82. ss << "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>";
  83. for(auto& f : table)
  84. ss << "<tr><td>" << f.id << "</td><td>" << escape_html_entities(f.message) << "</td></tr>";
  85. ss << "</table></body></html>";
  86. return response(_content_type = "text/html",
  87. _body = ss.str());
  88. }
  89. ).bind_factories(
  90. mysql_connection_factory(argv[1], "benchmarkdbuser", "benchmarkdbpass", "hello_world"),
  91. fortune_orm_factory("Fortune"),
  92. rn_orm_factory("World")
  93. );
  94. try
  95. {
  96. // Start the server.
  97. sl::mhd_json_serve(hello_api, atoi(argv[2])
  98. #ifdef TFB_USE_EPOLL
  99. , _linux_epoll, _nthreads = 1000
  100. #else
  101. , _one_thread_per_connection
  102. #endif
  103. );
  104. }
  105. catch (std::exception& e)
  106. {
  107. std::cerr << e.what() << std::endl;
  108. }
  109. catch (sl::error::error& e)
  110. {
  111. std::cerr << e.what() << std::endl;
  112. }
  113. }