example_vs.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "crow.h"
  2. #include <sstream>
  3. class ExampleLogHandler : public crow::ILogHandler {
  4. public:
  5. void log(std::string message, crow::LogLevel level) override {
  6. // cerr << "ExampleLogHandler -> " << message;
  7. }
  8. };
  9. struct ExampleMiddleware
  10. {
  11. std::string message;
  12. ExampleMiddleware()
  13. {
  14. message = "foo";
  15. }
  16. void setMessage(std::string newMsg)
  17. {
  18. message = newMsg;
  19. }
  20. struct context
  21. {
  22. };
  23. void before_handle(crow::request& req, crow::response& res, context& ctx)
  24. {
  25. CROW_LOG_DEBUG << " - MESSAGE: " << message;
  26. }
  27. void after_handle(crow::request& req, crow::response& res, context& ctx)
  28. {
  29. // no-op
  30. }
  31. };
  32. int main()
  33. {
  34. crow::App<ExampleMiddleware> app;
  35. app.get_middleware<ExampleMiddleware>().setMessage("hello");
  36. app.route_dynamic("/")
  37. ([]{
  38. return "Hello World!";
  39. });
  40. app.route_dynamic("/about")
  41. ([](){
  42. return "About Crow example.";
  43. });
  44. // a request to /path should be forwarded to /path/
  45. app.route_dynamic("/path/")
  46. ([](){
  47. return "Trailing slash test case..";
  48. });
  49. // simple json response
  50. app.route_dynamic("/json")
  51. ([]{
  52. crow::json::wvalue x;
  53. x["message"] = "Hello, World!";
  54. return x;
  55. });
  56. app.route_dynamic("/hello/<int>")
  57. ([](int count){
  58. if (count > 100)
  59. return crow::response(400);
  60. std::ostringstream os;
  61. os << count << " bottles of beer!";
  62. return crow::response(os.str());
  63. });
  64. app.route_dynamic("/add/<int>/<int>")
  65. ([](const crow::request& req, crow::response& res, int a, int b){
  66. std::ostringstream os;
  67. os << a+b;
  68. res.write(os.str());
  69. res.end();
  70. });
  71. // Compile error with message "Handler type is mismatched with URL paramters"
  72. //CROW_ROUTE(app,"/another/<int>")
  73. //([](int a, int b){
  74. //return crow::response(500);
  75. //});
  76. // more json example
  77. app.route_dynamic("/add_json")
  78. .methods(crow::HTTPMethod::POST)
  79. ([](const crow::request& req){
  80. auto x = crow::json::load(req.body);
  81. if (!x)
  82. return crow::response(400);
  83. auto sum = x["a"].i()+x["b"].i();
  84. std::ostringstream os;
  85. os << sum;
  86. return crow::response{os.str()};
  87. });
  88. app.route_dynamic("/params")
  89. ([](const crow::request& req){
  90. std::ostringstream os;
  91. os << "Params: " << req.url_params << "\n\n";
  92. os << "The key 'foo' was " << (req.url_params.get("foo") == nullptr ? "not " : "") << "found.\n";
  93. if(req.url_params.get("pew") != nullptr) {
  94. double countD = boost::lexical_cast<double>(req.url_params.get("pew"));
  95. os << "The value of 'pew' is " << countD << '\n';
  96. }
  97. auto count = req.url_params.get_list("count");
  98. os << "The key 'count' contains " << count.size() << " value(s).\n";
  99. for(const auto& countVal : count) {
  100. os << " - " << countVal << '\n';
  101. }
  102. return crow::response{os.str()};
  103. });
  104. // ignore all log
  105. crow::logger::setLogLevel(crow::LogLevel::DEBUG);
  106. //crow::logger::setHandler(std::make_shared<ExampleLogHandler>());
  107. app.port(18080)
  108. .multithreaded()
  109. .run();
  110. }