http_controller.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #pragma once
  2. #include "http_define.h"
  3. #if USE_NET_HTTP_WEBSITE
  4. #include "http_request.h"
  5. #include "http_response.h"
  6. #include "http_reqpack.h"
  7. #include "http_interface.h"
  8. #define _DBL_MIN 2.2250738585072014e-308 // min positive value
  9. #define _DBL_MAX 1.7976931348623158e+308 // max value
  10. #define _INT_MIN (-2147483647 - 1)
  11. #define _INT_MAX 2147483647
  12. #define _UINT_MAX 0xffffffff
  13. #define _LONG_MIN (-2147483647L - 1)
  14. #define _LONG_MAX 2147483647L
  15. #define _ULONG_MAX 0xffffffff
  16. #ifndef _LLONG_MAX
  17. #define _LLONG_MAX 9223372036854775807
  18. #endif
  19. #define _LLONG_MIN (-9223372036854775807 - 1)
  20. #define _ULLONG_MAX 0xffffffffffffffff
  21. #define _INT8_MIN (-127 - 1)
  22. #define _INT16_MIN (-32767 - 1)
  23. #define _INT32_MIN (-2147483647 - 1)
  24. #define _INT64_MIN (-9223372036854775807 - 1)
  25. #define _INT8_MAX 127
  26. #define _INT16_MAX 32767
  27. #define _INT32_MAX 2147483647
  28. #define _INT64_MAX 9223372036854775807
  29. #define _UINT8_MAX 0xff
  30. #define _UINT16_MAX 0xffff
  31. #define _UINT32_MAX 0xffffffff
  32. #define _UINT64_MAX 0xffffffffffffffff
  33. #define _FLT_MIN 1.175494351e-38F // min normalized positive value
  34. #define _FLT_MAX 3.402823466e+38F // max value
  35. using namespace ylib::network::http;
  36. // 成功回复
  37. #define REPLY_SUCC rpjson()["code"] = 200;return RT_OK
  38. // 自定义回复
  39. #define REPLY(CODE,MSG) rpjson()["code"] = CODE;rpjson()["msg"]=MSG;return RT_OK
  40. // 失败回复
  41. #define REPLY_ERROR(MSG) rpjson()["code"] = -1;rpjson()["msg"]=MSG;return RT_OK
  42. /**********************************************************
  43. * Class:Http控制器接口
  44. *********************************************************/
  45. //#define check_qry_json(NAME) request()->parser()->json()[NAME].is_empty()
  46. #define qry_json_string(NAME) request()->parser()->json()[NAME].to<std::string>(true)
  47. #define qry_json_uint32(NAME) request()->parser()->json()[NAME].to<uint32>(true)
  48. #define qry_json_uint64(NAME) request()->parser()->json()[NAME].to<uint64>(true)
  49. #define qry_json_int32(NAME) request()->parser()->json()[NAME].to<int32>(true)
  50. #define qry_json_double(NAME) request()->parser()->json()[NAME].to<double>(true)
  51. #define qry_json_short(NAME) request()->parser()->json()[NAME].to<short>(true)
  52. #define qry_json_bool(NAME) request()->parser()->json()[NAME].to<bool>(true)
  53. namespace ylib
  54. {
  55. namespace network
  56. {
  57. namespace http
  58. {
  59. class router;
  60. class controller :public network::http::interface_
  61. {
  62. public:
  63. controller();
  64. ~controller();
  65. const std::shared_ptr<network::http::request>& request();
  66. const std::shared_ptr<network::http::response>& response();
  67. // 获取回复JSON
  68. inline ylib::json& rpjson() { return response()->sjson["data"]; }
  69. inline ylib::json& rpcode() { return response()->sjson["code"]; }
  70. //inline ylib::json& rp() { return response()->sjson["code"]; }
  71. inline void rp(int code, const std::string& msg = "", const ylib::json& data = ylib::json())
  72. {
  73. response()->sjson["code"] = code;
  74. response()->sjson["msg"] = msg;
  75. response()->sjson["data"] = data;
  76. };
  77. inline ylib::json rqjson() { return ylib::json::from(request()->body().to_string()); }
  78. void send(const ylib::json& data);
  79. friend class router;
  80. private:
  81. network::http::reqpack* m_reqpack = nullptr;
  82. };
  83. }
  84. }
  85. }
  86. #endif