http_controller.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. network::http::request* request();
  66. network::http::response* response();
  67. //min和max为<=或>= ,若填-1则不使用该条件
  68. std::string qry_string(const std::string& name,bool exception = true);
  69. int32 qry_int32(const std::string& name, bool exception = true);
  70. uint32 qry_uint32(const std::string& name, bool exception = true);
  71. int64 qry_int64(const std::string& name, bool exception = true);
  72. uint64 qry_uint64(const std::string& name, bool exception = true);
  73. double qry_double(const std::string& name, bool exception = true);
  74. float qry_float(const std::string& name, bool exception = true);
  75. bool qry_empty(const std::string& name, bool exception = true);
  76. bool qry_bool(const std::string& name, bool exception = true);
  77. ylib::buffer qry_buffer(const std::string& name, bool exception);
  78. // 请求参数
  79. bool request_param(const std::string& name, std::string& value);
  80. // 获取回复JSON
  81. inline ylib::json& rpjson() { return response()->sjson["data"]; }
  82. inline ylib::json& rpcode() { return response()->sjson["code"]; }
  83. //inline ylib::json& rp() { return response()->sjson["code"]; }
  84. inline void rp(int code, const std::string& msg = "", const ylib::json& data = ylib::json())
  85. {
  86. response()->sjson["code"] = code;
  87. response()->sjson["msg"] = msg;
  88. response()->sjson["data"] = data;
  89. };
  90. void send(const ylib::json& data);
  91. friend class router;
  92. private:
  93. network::http::reqpack* m_reqpack = nullptr;
  94. };
  95. }
  96. }
  97. }
  98. #endif