srpc_config.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. Copyright (c) 2022 Sogou, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __SRPC_CONFIG__
  14. #define __SRPC_CONFIG__
  15. #include <stdint.h>
  16. #include <generator/generator.h>
  17. #ifndef _WIN32
  18. #include <unistd.h>
  19. #include <sys/param.h>
  20. #else
  21. #define MAXPATHLEN 4096
  22. #include <direct.h>
  23. #endif
  24. #define COLOR_OFF "\033[0m"
  25. #define COLOR_WHITE "\033[37;1m"
  26. #define COLOR_RED "\033[31;1m"
  27. #define COLOR_GREEN "\033[32;1m"
  28. #define COLOR_YELLOW "\033[33;1m"
  29. #define COLOR_PURPLE "\033[34;1m"
  30. #define COLOR_PINK "\033[35;1m"
  31. #define COLOR_BLUE "\033[36;1m"
  32. #define COLOR_LPURPLE "\033[94;1m"
  33. #define COLOR_COMMAND COLOR_BLUE
  34. #define COLOR_INFO COLOR_YELLOW
  35. #define COLOR_FLAG COLOR_PURPLE
  36. struct srpc_config
  37. {
  38. uint8_t type;
  39. const char *project_name;
  40. const char *service_name;
  41. char template_path[MAXPATHLEN];
  42. char depend_path[MAXPATHLEN];
  43. char output_path[MAXPATHLEN];
  44. // rpc
  45. uint8_t rpc_type;
  46. uint8_t idl_type;
  47. uint8_t data_type;
  48. uint8_t compress_type;
  49. bool specified_depend_path;
  50. const char *specified_idl_file;
  51. const char *specified_idl_path;
  52. // proxy
  53. uint8_t proxy_server_type;
  54. uint8_t proxy_client_type;
  55. srpc_config();
  56. bool prepare_args();
  57. bool prepare_project_path();
  58. bool prepare_dependencies() const;
  59. bool prepare_specified_idl_file();
  60. const char *rpc_type_string() const;
  61. const char *rpc_compress_string() const;
  62. const char *rpc_data_string() const;
  63. void set_rpc_type(const char *optarg);
  64. void set_idl_type(const char *optarg);
  65. void set_data_type(const char *optarg);
  66. void set_compress_type(const char *optarg);
  67. const char *proxy_server_type_string() const;
  68. const char *proxy_client_type_string() const;
  69. };
  70. class ControlGenerator : public Generator
  71. {
  72. public:
  73. bool generate_client_cpp_file(const idl_info& cur_info,
  74. const std::string& idl_file_name);
  75. bool generate_server_cpp_file(const idl_info& cur_info,
  76. const std::string& idl_file_name);
  77. ControlGenerator(const struct srpc_config *config);
  78. private:
  79. class ControlPrinter : public Printer
  80. {
  81. public:
  82. ControlPrinter(bool is_thrift) : Printer(is_thrift) { }
  83. void print_clt_include();
  84. void print_server_load_config();
  85. void print_client_load_config();
  86. void print_client_params();
  87. void print_client_done_method_ctl(const std::vector<std::string>& pkg,
  88. const std::string& service,
  89. const std::string& method,
  90. const std::string& response);
  91. void print_client_main_service_ctl(const std::string& type,
  92. const std::vector<std::string>& pkg,
  93. const std::string& service,
  94. const std::string& suffix);
  95. void print_server_construct(const char *rpc_type);
  96. void print_server_main_end_ctl(const char *project_name,
  97. const char *rpc_type);
  98. };
  99. ControlPrinter ctl_printer;
  100. const struct srpc_config *config;
  101. };
  102. enum
  103. {
  104. COMMAND_HTTP,
  105. COMMAND_REDIS,
  106. COMMAND_MYSQL,
  107. COMMAND_KAFKA,
  108. COMMAND_RPC,
  109. COMMAND_API,
  110. COMMAND_PROXY,
  111. COMMAND_FILE,
  112. COMMAND_COMPUTE
  113. };
  114. enum
  115. {
  116. PROTOCOL_TYPE_HTTP = COMMAND_HTTP,
  117. PROTOCOL_TYPE_REDIS = COMMAND_REDIS,
  118. PROTOCOL_TYPE_MYSQL = COMMAND_MYSQL,
  119. PROTOCOL_TYPE_KAFKA = COMMAND_KAFKA,
  120. PROTOCOL_TYPE_SRPC = 22,
  121. PROTOCOL_TYPE_SRPC_HTTP,
  122. PROTOCOL_TYPE_BRPC,
  123. PROTOCOL_TYPE_THRIFT,
  124. PROTOCOL_TYPE_THRIFT_HTTP,
  125. PROTOCOL_TYPE_TRPC,
  126. PROTOCOL_TYPE_TRPC_HTTP,
  127. PROTOCOL_TYPE_MAX
  128. };
  129. enum
  130. {
  131. IDL_TYPE_DEFAULT,
  132. IDL_TYPE_PROTOBUF,
  133. IDL_TYPE_THRIFT,
  134. IDL_TYPE_MAX
  135. };
  136. enum
  137. {
  138. DATA_TYPE_DEFAULT,
  139. DATA_TYPE_PROTOBUF,
  140. DATA_TYPE_THRIFT,
  141. DATA_TYPE_JSON,
  142. DATA_TYPE_MAX
  143. };
  144. enum
  145. {
  146. COMPRESS_TYPE_NONE,
  147. COMPRESS_TYPE_SNAPPY,
  148. COMPRESS_TYPE_GZIP,
  149. COMPRESS_TYPE_ZLIB,
  150. COMPRESS_TYPE_LZ4,
  151. COMPRESS_TYPE_MAX
  152. };
  153. enum
  154. {
  155. PROXY_BASIC_TYPE = 1,
  156. PROXY_PROTOBUF_TYPE = 2,
  157. PROXY_THRIFT_TYPE = 3
  158. };
  159. void usage(int argc, const char *argv[]);
  160. void usage_http(int argc, const char *argv[]);
  161. void usage_db(int argc, const char *argv[], const struct srpc_config *config);
  162. void usage_kafka(int argc, const char *argv[]);
  163. void usage_rpc(int argc, const char *argv[], const struct srpc_config *config);
  164. const char *get_type_string(uint8_t type);
  165. int check_proxy_type(uint8_t type);
  166. #endif