mirror of
https://gitee.com/sogou/srpc.git
synced 2024-12-21 16:14:54 +08:00
Merge pull request #291 from holmes1412/master
srpc tools: support command [api] and update codes.
This commit is contained in:
commit
08affc52b7
@ -1,6 +1,5 @@
|
||||
ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
|
||||
BUILD_DIR := build.cmake
|
||||
OUTPUT := srpc-ctl
|
||||
|
||||
.PHONY: all clean
|
||||
|
||||
@ -12,5 +11,8 @@ base:
|
||||
cd $(BUILD_DIR) && cmake -D CMAKE_BUILD_TYPE=Debug $(ROOT_DIR)
|
||||
|
||||
clean:
|
||||
ifeq ($(BUILD_DIR), $(wildcard $(BUILD_DIR)))
|
||||
make -C $(BUILD_DIR) clean
|
||||
rm -rf $(BUILD_DIR)
|
||||
endif
|
||||
|
||||
|
@ -33,6 +33,7 @@ Available Commands:
|
||||
http - create project with both client and server
|
||||
redis - create project with both client and server
|
||||
rpc - create project with both client and server
|
||||
api - create protobuf or thrift IDL api
|
||||
proxy - create proxy for some client and server protocol
|
||||
file - create project with asynchronous file service
|
||||
compute - create project with asynchronous computing service
|
||||
@ -46,14 +47,14 @@ Available Commands:
|
||||
./srpc http project_name1
|
||||
```
|
||||
|
||||
然后就可以看到屏幕上显示,项目建立在了新目录`./project_name1/`中,并且带有编译和执行的提示命令。
|
||||
然后就可以看到屏幕上显示,项目建立在了新目录`project_name1/`中,并且带有编译和执行的提示命令。
|
||||
|
||||
```
|
||||
Success:
|
||||
make project path " ./project_name1/ " done.
|
||||
make project path project_name1/ done.
|
||||
|
||||
Commands:
|
||||
cd ./project_name1/
|
||||
cd project_name1/
|
||||
make -j
|
||||
|
||||
Execute:
|
||||
@ -102,6 +103,9 @@ Missing: PROJECT_NAME
|
||||
Usage:
|
||||
./srpc http <PROJECT_NAME> [FLAGS]
|
||||
|
||||
Example:
|
||||
./srpc http my_http_project
|
||||
|
||||
Available Flags:
|
||||
-o : project output path (default: CURRENT_PATH)
|
||||
-d : path of dependencies (default: COMPILE_PATH)
|
||||
@ -121,6 +125,9 @@ Missing: PROJECT_NAME
|
||||
Usage:
|
||||
./srpc rpc <PROJECT_NAME> [FLAGS]
|
||||
|
||||
Example:
|
||||
./srpc rpc my_rpc_project
|
||||
|
||||
Available Flags:
|
||||
-r : rpc type [ SRPC | SRPCHttp | BRPC | Thrift | ThriftHttp | TRPC | TRPCHttp ] (default: SRPC)
|
||||
-o : project output path (default: CURRENT_PATH)
|
||||
@ -155,10 +162,10 @@ finish parsing proto file: [/root/srpc/tools/rpc_example/test.proto]
|
||||
Info: srpc generator done.
|
||||
|
||||
Success:
|
||||
make project path " /root/srpc/tools/rpc_example/ " done.
|
||||
make project path rpc_example/ done.
|
||||
|
||||
Commands:
|
||||
cd /root/srpc/tools/rpc_example/
|
||||
cd rpc_example/
|
||||
make -j
|
||||
|
||||
Execute:
|
||||
@ -166,7 +173,44 @@ Execute:
|
||||
./client
|
||||
```
|
||||
|
||||
## 6. REDIS
|
||||
## 6. API
|
||||
|
||||
API命令用于简单地创建一个IDL文件,这样方便我们改完文件之后再进行rpc项目调查创建。命令如下:
|
||||
|
||||
```
|
||||
Missing: FILE_NAME
|
||||
|
||||
Usage:
|
||||
./srpc api <FILE_NAME> [FLAGS]
|
||||
|
||||
Example:
|
||||
./srpc api my_api
|
||||
|
||||
Available Flags:
|
||||
-o : file output path (default: CURRENT_PATH)
|
||||
-i : idl type [ protobuf | thrift ] (default: protobuf)
|
||||
```
|
||||
|
||||
我们通过以下命令,可以创建一个默认为proto格式的文件:
|
||||
|
||||
```
|
||||
./srpc api test
|
||||
```
|
||||
|
||||
```
|
||||
Success:
|
||||
Create api file test.proto at path /root/srpc/tools done.
|
||||
|
||||
Suggestions:
|
||||
Modify the api file as you needed.
|
||||
And make rpc project base on this file with the following command:
|
||||
|
||||
./srpc rpc my_rpc_project -f test.proto -p /root/srpc/tools
|
||||
```
|
||||
|
||||
之后就可以通过以上建议的命令创建一个rpc项目了,rpc项目以test.proto文件创建生成代码,使用方法如`RPC`部分介绍。
|
||||
|
||||
## 7. REDIS
|
||||
|
||||
创建REDIS协议的client和server,命令如下:
|
||||
|
||||
@ -182,6 +226,9 @@ Missing: PROJECT_NAME
|
||||
Usage:
|
||||
./srpc redis <PROJECT_NAME> [FLAGS]
|
||||
|
||||
Example:
|
||||
./srpc redis my_redis_project
|
||||
|
||||
Available Flags:
|
||||
-o : project output path (default: CURRENT_PATH)
|
||||
-d : path of dependencies (default: COMPILE_PATH)
|
||||
@ -206,19 +253,19 @@ response: OK
|
||||
如果client有填写用户名和密码的需求,可以填到`client.conf`中。我们打开这个配置文件看看:
|
||||
|
||||
```
|
||||
1 {
|
||||
2 "client":
|
||||
3 {
|
||||
4 "remote_host": "127.0.0.1",
|
||||
5 "remote_port": 6379,
|
||||
6 "retry_max": 2,
|
||||
7 "user_name": "root",
|
||||
8 "password": ""
|
||||
9 }
|
||||
1 {
|
||||
2 "client":
|
||||
3 {
|
||||
4 "remote_host": "127.0.0.1",
|
||||
5 "remote_port": 6379,
|
||||
6 "retry_max": 2,
|
||||
7 "user_name": "root",
|
||||
8 "password": ""
|
||||
9 }
|
||||
10 }
|
||||
```
|
||||
|
||||
## 7. PROXY
|
||||
## 8. PROXY
|
||||
|
||||
这个命令用于构建一个转发服务器,并且还有与其协议相关的server和client。
|
||||
|
||||
@ -234,6 +281,9 @@ Missing: PROJECT_NAME
|
||||
Usage:
|
||||
./srpc proxy <PROJECT_NAME> [FLAGS]
|
||||
|
||||
Example:
|
||||
./srpc redis my_proxy_project
|
||||
|
||||
Available Flags:
|
||||
-c : client type for proxy [ Http | Redis | SRPC | SRPCHttp | BRPC | Thrift | ThriftHttp | TRPC | TRPCHttp ] (default: Http)
|
||||
-s : server type for proxy [ Http | Redis | SRPC | SRPCHttp | BRPC | Thrift | ThriftHttp | TRPC | TRPCHttp ] (default: Http)
|
||||
@ -248,10 +298,10 @@ Available Flags:
|
||||
```
|
||||
```
|
||||
Success:
|
||||
make project path " ./srpc_trpc_proxy_example/ " done.
|
||||
make project path srpc_trpc_proxy_example/ " done.
|
||||
|
||||
Commands:
|
||||
cd ./srpc_trpc_proxy_example/
|
||||
cd srpc_trpc_proxy_example/
|
||||
make -j
|
||||
|
||||
Execute:
|
||||
@ -312,7 +362,7 @@ sync resp. message: "Hi back"
|
||||
async resp. message: "Hi back"
|
||||
```
|
||||
|
||||
## 8. FILE
|
||||
## 9. FILE
|
||||
|
||||
这是一个简单的文件服务器,文件读取都是异步的,不会因为读文件阻塞当前服务器的处理线程:
|
||||
|
||||
@ -322,7 +372,7 @@ async resp. message: "Hi back"
|
||||
|
||||
```
|
||||
Success:
|
||||
make project path " ./file_project/ " done.
|
||||
make project path file_project/ done.
|
||||
|
||||
Commands:
|
||||
cd ./file_project/
|
||||
@ -408,7 +458,7 @@ http file service start, port 8080
|
||||
file service get request: /a/b/
|
||||
```
|
||||
|
||||
## 9. COMPUTE
|
||||
## 10. COMPUTE
|
||||
|
||||
接下来是一个简单的计算服务器,同理,计算也不会阻塞当前服务器的处理线程:
|
||||
|
||||
@ -420,7 +470,7 @@ file service get request: /a/b/
|
||||
|
||||
```
|
||||
Success:
|
||||
make project path " compute_test/ " done.
|
||||
make project path compute_test/ done.
|
||||
|
||||
Commands:
|
||||
cd compute_test/
|
||||
|
@ -102,6 +102,9 @@ Missing: PROJECT_NAME
|
||||
Usage:
|
||||
./srpc http <PROJECT_NAME> [FLAGS]
|
||||
|
||||
Example:
|
||||
./srpc redis my_http_project
|
||||
|
||||
Available Flags:
|
||||
-o : project output path (default: CURRENT_PATH)
|
||||
-d : path of dependencies (default: COMPILE_PATH)
|
||||
@ -123,6 +126,9 @@ Missing: PROJECT_NAME
|
||||
Usage:
|
||||
./srpc rpc <PROJECT_NAME> [FLAGS]
|
||||
|
||||
Example:
|
||||
./srpc redis my_rpc_project
|
||||
|
||||
Available Flags:
|
||||
-r : rpc type [ SRPC | SRPCHttp | BRPC | Thrift | ThriftHttp | TRPC | TRPCHttp ] (default: SRPC)
|
||||
-o : project output path (default: CURRENT_PATH)
|
||||
@ -168,7 +174,44 @@ Execute:
|
||||
./client
|
||||
```
|
||||
|
||||
## 6. REDIS COMMAND
|
||||
## 6. API COMMAND
|
||||
|
||||
This command is to create an IDL file, which is convenient for us to create rpc project after changing the file. The command is as follows:
|
||||
|
||||
```
|
||||
Missing: FILE_NAME
|
||||
|
||||
Usage:
|
||||
./srpc api <FILE_NAME> [FLAGS]
|
||||
|
||||
Example:
|
||||
./srpc api my_api
|
||||
|
||||
Available Flags:
|
||||
-o : file output path (default: CURRENT_PATH)
|
||||
-i : idl type [ protobuf | thrift ] (default: protobuf)
|
||||
```
|
||||
|
||||
Create a file in proto format by default:
|
||||
|
||||
```
|
||||
./srpc api test
|
||||
```
|
||||
|
||||
```
|
||||
Success:
|
||||
Create api file test.proto at path /root/srpc/tools done.
|
||||
|
||||
Suggestions:
|
||||
Modify the api file as you needed.
|
||||
And make rpc project base on this file with the following command:
|
||||
|
||||
./srpc rpc my_rpc_project -f test.proto -p /root/srpc/tools
|
||||
```
|
||||
|
||||
Then we can create an rpc project through the above suggested commands. The rpc project uses the test.proto file to create the generated code.
|
||||
|
||||
## 7. REDIS COMMAND
|
||||
|
||||
commands for REDIS:
|
||||
|
||||
@ -184,6 +227,9 @@ Missing: PROJECT_NAME
|
||||
Usage:
|
||||
./srpc redis <PROJECT_NAME> [FLAGS]
|
||||
|
||||
Example:
|
||||
./srpc redis my_redis_project
|
||||
|
||||
Available Flags:
|
||||
-o : project output path (default: CURRENT_PATH)
|
||||
-d : path of dependencies (default: COMPILE_PATH)
|
||||
@ -208,19 +254,19 @@ response: OK
|
||||
If there is user name and password for redis server, client may fill them into client.conf:
|
||||
|
||||
```
|
||||
1 {
|
||||
2 "client":
|
||||
3 {
|
||||
4 "remote_host": "127.0.0.1",
|
||||
5 "remote_port": 6379,
|
||||
6 "retry_max": 2,
|
||||
7 "user_name": "root",
|
||||
8 "password": ""
|
||||
9 }
|
||||
1 {
|
||||
2 "client":
|
||||
3 {
|
||||
4 "remote_host": "127.0.0.1",
|
||||
5 "remote_port": 6379,
|
||||
6 "retry_max": 2,
|
||||
7 "user_name": "root",
|
||||
8 "password": ""
|
||||
9 }
|
||||
10 }
|
||||
```
|
||||
|
||||
## 7. PROXY COMMAND
|
||||
## 8. PROXY COMMAND
|
||||
|
||||
commands for PROXY:
|
||||
|
||||
@ -236,6 +282,9 @@ Missing: PROJECT_NAME
|
||||
Usage:
|
||||
./srpc proxy <PROJECT_NAME> [FLAGS]
|
||||
|
||||
Example:
|
||||
./srpc redis my_proxy_project
|
||||
|
||||
Available Flags:
|
||||
-c : client type for proxy [ Http | Redis | SRPC | SRPCHttp | BRPC | Thrift | ThriftHttp | TRPC | TRPCHttp ] (default: Http)
|
||||
-s : server type for proxy [ Http | Redis | SRPC | SRPCHttp | BRPC | Thrift | ThriftHttp | TRPC | TRPCHttp ] (default: Http)
|
||||
@ -315,7 +364,7 @@ sync resp. message: "Hi back"
|
||||
async resp. message: "Hi back"
|
||||
```
|
||||
|
||||
## 8. FILE COMMAND
|
||||
## 9. FILE COMMAND
|
||||
|
||||
This is a simple file server. File reading is asynchronous, and the threads for process( ) will not be blocked by file IOs.
|
||||
|
||||
@ -415,7 +464,7 @@ http file service start, port 8080
|
||||
file service get request: /a/b/
|
||||
```
|
||||
|
||||
## 9. COMPUTE COMMAND
|
||||
## 10. COMPUTE COMMAND
|
||||
|
||||
Next is a simple calculation server, similarly, the calculation will not block the server processing thread.
|
||||
|
||||
|
@ -322,12 +322,14 @@ static void basic_print_usage(const char *name, const char *command)
|
||||
printf(COLOR_PINK"Usage:\n"
|
||||
COLOR_INFO" %s " COLOR_COMMAND "%s "
|
||||
COLOR_INFO"<PROJECT_NAME>" COLOR_FLAG " [FLAGS]\n\n"
|
||||
COLOR_PINK"Example:\n"
|
||||
COLOR_PURPLE" %s %s my_%s_project\n\n"
|
||||
COLOR_PINK"Available Flags:\n"
|
||||
COLOR_FLAG" -o : "
|
||||
COLOR_WHITE"project output path (default: CURRENT_PATH)\n"
|
||||
COLOR_FLAG" -d : "
|
||||
COLOR_WHITE"path of dependencies (default: COMPILE_PATH)\n"
|
||||
COLOR_OFF, name, command);
|
||||
COLOR_OFF, name, command, name, command, command);
|
||||
}
|
||||
|
||||
HttpController::HttpController()
|
||||
|
@ -111,6 +111,7 @@ enum
|
||||
COMMAND_MYSQL,
|
||||
COMMAND_KAFKA,
|
||||
COMMAND_RPC,
|
||||
COMMAND_API,
|
||||
COMMAND_PROXY,
|
||||
COMMAND_FILE,
|
||||
COMMAND_COMPUTE
|
||||
|
@ -30,7 +30,7 @@ if(${PROTOC} STREQUAL "PROTOC-NOTFOUND")
|
||||
endif ()
|
||||
protobuf_generate_cpp(PROTO_SRCS PROTO_HDRS ${IDL_FILE}))";
|
||||
|
||||
static int mkdir_p(const char *name, mode_t mode)
|
||||
int mkdir_p(const char *name, mode_t mode)
|
||||
{
|
||||
int ret = mkdir(name, mode);
|
||||
if (ret == 0 || errno != ENOENT)
|
||||
@ -226,7 +226,11 @@ bool CommandController::parse_args(int argc, const char **argv)
|
||||
{
|
||||
if (argc < 3)
|
||||
{
|
||||
printf(COLOR_RED "Missing: PROJECT_NAME\n\n" COLOR_OFF);
|
||||
if (argc == 2 && strncmp(argv[1], "api", strlen(argv[1])) == 0)
|
||||
printf(COLOR_RED "Missing: FILE_NAME\n\n" COLOR_OFF);
|
||||
else
|
||||
printf(COLOR_RED "Missing: PROJECT_NAME\n\n" COLOR_OFF);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -254,15 +258,18 @@ bool CommandController::parse_args(int argc, const char **argv)
|
||||
bool CommandController::check_args()
|
||||
{
|
||||
if (*(this->config.project_name) == '-')
|
||||
{
|
||||
printf(COLOR_RED "Error: Invalid PROJECT_NAME\n\n" COLOR_OFF);
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t path_len = strlen(this->config.output_path);
|
||||
|
||||
if (strlen(this->config.project_name) >= MAXPATHLEN - path_len - 2)
|
||||
{
|
||||
printf(COLOR_RED "Error:\n project name" COLOR_BLUE " \" %s \" "
|
||||
COLOR_RED "or path" COLOR_BLUE " \" %s \" "
|
||||
COLOR_RED " is too long. Total limit : %d.\n\n" COLOR_OFF,
|
||||
printf(COLOR_RED"Error:\n project name" COLOR_BLUE " %s "
|
||||
COLOR_RED"or path" COLOR_BLUE " %s "
|
||||
COLOR_RED" is too long. Total limit : %d.\n\n" COLOR_OFF,
|
||||
this->config.project_name, this->config.output_path, MAXPATHLEN);
|
||||
return false;
|
||||
}
|
||||
@ -289,7 +296,7 @@ bool CommandController::dependencies_and_dir()
|
||||
if (dir != NULL)
|
||||
{
|
||||
printf(COLOR_RED "Error:\n project path "
|
||||
COLOR_BLUE "\" %s \"" COLOR_RED " EXISTS.\n\n" COLOR_OFF,
|
||||
COLOR_BLUE " %s " COLOR_RED " EXISTS.\n\n" COLOR_OFF,
|
||||
config->output_path);
|
||||
|
||||
closedir(dir);
|
||||
@ -300,7 +307,7 @@ bool CommandController::dependencies_and_dir()
|
||||
if (dir == NULL)
|
||||
{
|
||||
printf(COLOR_RED "Error:\n template path "
|
||||
COLOR_BLUE "\" %s \" " COLOR_RED "does NOT exist.\n" COLOR_OFF,
|
||||
COLOR_BLUE " %s " COLOR_RED "does NOT exist.\n" COLOR_OFF,
|
||||
config->template_path);
|
||||
return false;
|
||||
}
|
||||
@ -432,27 +439,27 @@ bool CommandController::copy_single_file(const std::string& in_file,
|
||||
}
|
||||
else
|
||||
{
|
||||
printf(COLOR_RED "Error:\n read " COLOR_WHITE
|
||||
"\" %s \" " COLOR_RED "failed\n\n" COLOR_OFF,
|
||||
printf(COLOR_RED"Error:\n read " COLOR_WHITE
|
||||
" %s " COLOR_RED "failed\n\n" COLOR_OFF,
|
||||
in_file.c_str());
|
||||
}
|
||||
|
||||
free(buf);
|
||||
}
|
||||
else
|
||||
printf(COLOR_RED "Error:\n system error.\n\n" COLOR_OFF);
|
||||
printf(COLOR_RED"Error:\n system error.\n\n" COLOR_OFF);
|
||||
|
||||
fclose(write_fp);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf(COLOR_RED "Error:\n write" COLOR_WHITE " \" %s \" "
|
||||
COLOR_RED "failed\n\n" COLOR_OFF, out_file.c_str());
|
||||
printf(COLOR_RED"Error:\n write" COLOR_WHITE " %s "
|
||||
COLOR_RED"failed\n\n" COLOR_OFF, out_file.c_str());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf(COLOR_RED "Error:\n open" COLOR_WHITE " \" %s \""
|
||||
printf(COLOR_RED"Error:\n open" COLOR_WHITE " %s "
|
||||
COLOR_RED " failed.\n\n" COLOR_OFF, in_file.c_str());
|
||||
}
|
||||
return ret;
|
||||
@ -483,8 +490,8 @@ bool CommandController::copy_files()
|
||||
|
||||
void CommandController::print_success_info() const
|
||||
{
|
||||
printf(COLOR_GREEN"Success!\n make project path "
|
||||
COLOR_BLUE"\" %s \"" COLOR_GREEN " done.\n\n",
|
||||
printf(COLOR_GREEN"Success!\n make project path"
|
||||
COLOR_BLUE" %s " COLOR_GREEN " done.\n\n",
|
||||
this->config.output_path);
|
||||
printf(COLOR_PINK"Commands:\n " COLOR_BLUE "cd %s\n make -j\n\n",
|
||||
this->config.output_path);
|
||||
@ -560,7 +567,6 @@ bool rpc_cmake_transform(const std::string& format, FILE *out,
|
||||
const struct srpc_config *config)
|
||||
{
|
||||
std::string idl_file_name;
|
||||
|
||||
std::string srpc_path = config->depend_path;
|
||||
std::string workflow_path = config->depend_path;
|
||||
workflow_path += "workflow";
|
||||
|
@ -37,7 +37,7 @@ class CommandController
|
||||
{
|
||||
public:
|
||||
bool parse_args(int argc, const char **argv);
|
||||
bool dependencies_and_dir();
|
||||
virtual bool dependencies_and_dir();
|
||||
virtual bool copy_files();
|
||||
virtual void print_success_info() const;
|
||||
virtual void print_usage(const char *name) const = 0;
|
||||
@ -116,6 +116,24 @@ public:
|
||||
~RPCController() { }
|
||||
};
|
||||
|
||||
class APIController : public CommandController
|
||||
{
|
||||
public:
|
||||
bool dependencies_and_dir() override;
|
||||
void print_usage(const char *name) const override;
|
||||
void print_success_info() const override;
|
||||
|
||||
protected:
|
||||
bool check_args() override;
|
||||
|
||||
private:
|
||||
bool get_opt(int argc, const char **argv) override;
|
||||
|
||||
public:
|
||||
APIController();
|
||||
~APIController() { }
|
||||
};
|
||||
|
||||
class ProxyController : public CommandController
|
||||
{
|
||||
public:
|
||||
@ -162,6 +180,10 @@ public:
|
||||
~ComputeController() { }
|
||||
};
|
||||
|
||||
////////// common functions //////////
|
||||
|
||||
int mkdir_p(const char *name, mode_t mode);
|
||||
|
||||
////////// common transform functions //////////
|
||||
|
||||
bool common_cmake_transform(const std::string& format, FILE *out,
|
||||
|
@ -31,6 +31,8 @@ static void usage(const char *name)
|
||||
COLOR_WHITE" - create project with both client and server\n"
|
||||
COLOR_COMMAND" rpc"
|
||||
COLOR_WHITE" - create project with both client and server\n"
|
||||
COLOR_COMMAND" api"
|
||||
COLOR_WHITE" - create protobuf or thrift IDL api\n"
|
||||
COLOR_COMMAND" proxy"
|
||||
COLOR_WHITE" - create proxy for some client and server protocol\n"
|
||||
COLOR_COMMAND" file"
|
||||
@ -62,6 +64,10 @@ int main(int argc, const char *argv[])
|
||||
{
|
||||
ctl = new RPCController;
|
||||
}
|
||||
else if (strcasecmp(argv[1], "api") == 0)
|
||||
{
|
||||
ctl = new APIController;
|
||||
}
|
||||
else if (strcasecmp(argv[1], "proxy") == 0)
|
||||
{
|
||||
ctl = new ProxyController;
|
||||
|
@ -223,6 +223,8 @@ void ProxyController::print_usage(const char *name) const
|
||||
printf(COLOR_PINK"Usage:\n"
|
||||
COLOR_INFO" %s " COLOR_BLUE "proxy "
|
||||
COLOR_INFO "<PROJECT_NAME>" COLOR_FLAG " [FLAGS]\n\n"
|
||||
COLOR_PINK"Example:\n"
|
||||
COLOR_PURPLE" %s proxy my_proxy_project\n\n"
|
||||
COLOR_PINK"Available Flags:\n"
|
||||
COLOR_FLAG" -c :"
|
||||
COLOR_WHITE" client type for proxy [ Http | Redis | SRPC | SRPCHttp"
|
||||
@ -234,13 +236,13 @@ void ProxyController::print_usage(const char *name) const
|
||||
COLOR_WHITE" project output path (default: CURRENT_PATH)\n"
|
||||
COLOR_FLAG" -d :"
|
||||
COLOR_WHITE" path of dependencies (default: COMPILE_PATH)\n"
|
||||
COLOR_OFF, name);
|
||||
COLOR_OFF, name, name);
|
||||
}
|
||||
|
||||
void ProxyController::print_success_info() const
|
||||
{
|
||||
printf(COLOR_GREEN"Success:\n make project path "
|
||||
COLOR_BLUE"\" %s \"" COLOR_GREEN " done.\n\n" COLOR_OFF,
|
||||
COLOR_BLUE" %s " COLOR_GREEN " done.\n\n" COLOR_OFF,
|
||||
this->config.output_path);
|
||||
printf(COLOR_PINK"Commands:\n"
|
||||
COLOR_BLUE " cd %s\n make -j\n\n" COLOR_OFF,
|
||||
|
@ -61,6 +61,8 @@ void RPCController::print_usage(const char *name) const
|
||||
printf(COLOR_PINK"Usage:\n"
|
||||
COLOR_INFO" %s " COLOR_COMMAND "rpc "
|
||||
COLOR_INFO"<PROJECT_NAME> " COLOR_FLAG "[FLAGS]\n\n"
|
||||
COLOR_PINK"Example:\n"
|
||||
COLOR_PURPLE" %s rpc my_rpc_project\n\n"
|
||||
COLOR_PINK"Available Flags:\n"
|
||||
COLOR_FLAG" -r "
|
||||
COLOR_WHITE": rpc type [ SRPC | SRPCHttp | BRPC | Thrift | "
|
||||
@ -85,12 +87,13 @@ void RPCController::print_usage(const char *name) const
|
||||
COLOR_FLAG" -p "
|
||||
COLOR_WHITE": specify the path for idl_file to depend "
|
||||
"(default: templates/rpc/)\n"
|
||||
COLOR_OFF, name);
|
||||
COLOR_OFF, name, name);
|
||||
}
|
||||
|
||||
bool RPCController::copy_files()
|
||||
{
|
||||
struct srpc_config *config = &this->config;
|
||||
int ret = true;
|
||||
|
||||
if (config->specified_idl_file == NULL) // fill the default rpc files
|
||||
{
|
||||
@ -119,9 +122,16 @@ bool RPCController::copy_files()
|
||||
return false;
|
||||
|
||||
ControlGenerator gen(config);
|
||||
printf(COLOR_PURPLE"Info: srpc-ctl generator begin.\n" COLOR_OFF);
|
||||
gen.generate(params);
|
||||
printf(COLOR_PURPLE"Info: srpc-ctl generator done.\n\n" COLOR_OFF);
|
||||
printf(COLOR_PURPLE"Info: srpc generator begin.\n" COLOR_OFF);
|
||||
ret = gen.generate(params);
|
||||
|
||||
if (ret == false)
|
||||
{
|
||||
printf(COLOR_RED"Error: srpc generator error.\n\n" COLOR_OFF);
|
||||
return false;
|
||||
}
|
||||
|
||||
printf(COLOR_PURPLE"Info: srpc generator done.\n\n" COLOR_OFF);
|
||||
}
|
||||
|
||||
return CommandController::copy_files();
|
||||
@ -134,7 +144,7 @@ bool RPCController::get_opt(int argc, const char **argv)
|
||||
optind = 3;
|
||||
|
||||
while ((c = getopt(argc, (char * const *)argv,
|
||||
"o:r:i:x:c:s:t:d:f:p:")) != -1)
|
||||
"o:r:i:x:c:s:d:f:p:")) != -1)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
@ -157,10 +167,6 @@ bool RPCController::get_opt(int argc, const char **argv)
|
||||
case 's':
|
||||
config->service_name = optarg;
|
||||
break;
|
||||
case 't':
|
||||
if (sscanf(optarg, "%s", config->template_path) != 1)
|
||||
return false; // TODO:
|
||||
break;
|
||||
case 'd':
|
||||
config->specified_depend_path = true;
|
||||
memset(config->depend_path, 0, MAXPATHLEN);
|
||||
@ -239,3 +245,148 @@ bool RPCController::check_args()
|
||||
return true;
|
||||
}
|
||||
|
||||
APIController::APIController()
|
||||
{
|
||||
this->config.type = COMMAND_API;
|
||||
this->config.idl_type = IDL_TYPE_PROTOBUF;
|
||||
}
|
||||
|
||||
void APIController::print_usage(const char *name) const
|
||||
{
|
||||
printf(COLOR_PINK"Usage:\n"
|
||||
COLOR_INFO" %s " COLOR_COMMAND "api "
|
||||
COLOR_INFO"<FILE_NAME> " COLOR_FLAG "[FLAGS]\n\n"
|
||||
COLOR_PINK"Example:\n"
|
||||
COLOR_PURPLE" %s api my_api\n\n"
|
||||
COLOR_PINK"Available Flags:\n"
|
||||
COLOR_FLAG" -o "
|
||||
COLOR_WHITE": file output path (default: CURRENT_PATH)\n"
|
||||
COLOR_FLAG" -i "
|
||||
COLOR_WHITE": idl type [ protobuf | thrift ] (default: protobuf)\n"
|
||||
COLOR_OFF, name, name);
|
||||
}
|
||||
|
||||
bool APIController::get_opt(int argc, const char **argv)
|
||||
{
|
||||
char c;
|
||||
optind = 3;
|
||||
|
||||
getcwd(this->config.output_path, MAXPATHLEN);
|
||||
|
||||
while ((c = getopt(argc, (char * const *)argv, "o:i:")) != -1)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case 'o':
|
||||
memset(this->config.output_path, 0, MAXPATHLEN);
|
||||
if (sscanf(optarg, "%s", this->config.output_path) != 1)
|
||||
return false;
|
||||
break;
|
||||
case 'i':
|
||||
this->config.set_idl_type(optarg);
|
||||
break;
|
||||
default:
|
||||
printf(COLOR_RED "Error:\n Unknown args : "
|
||||
COLOR_BLUE "%s\n\n" COLOR_OFF, argv[optind - 1]);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool APIController::check_args()
|
||||
{
|
||||
if (*(this->config.project_name) == '-')
|
||||
{
|
||||
printf(COLOR_RED "Error: Invalid FILE_NAME\n\n" COLOR_OFF);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool APIController::dependencies_and_dir()
|
||||
{
|
||||
std::string idl_file_name;
|
||||
std::string out_file_name = this->config.project_name;
|
||||
|
||||
if (this->config.idl_type == IDL_TYPE_PROTOBUF)
|
||||
{
|
||||
out_file_name += ".proto";
|
||||
idl_file_name = "rpc/rpc.proto";
|
||||
}
|
||||
else if (this->config.idl_type == IDL_TYPE_THRIFT)
|
||||
{
|
||||
out_file_name += ".thrift";
|
||||
idl_file_name = "rpc/rpc.thrift";
|
||||
}
|
||||
|
||||
std::string abs_file_name = this->config.output_path;
|
||||
if (abs_file_name.at(abs_file_name.length() - 1) != '/')
|
||||
abs_file_name += "/";
|
||||
abs_file_name += out_file_name;
|
||||
|
||||
DIR *dir;
|
||||
dir = opendir(this->config.output_path);
|
||||
if (dir == NULL)
|
||||
{
|
||||
if (mkdir_p(this->config.output_path, 0755) != 0)
|
||||
{
|
||||
perror("Error:\n failed to make output_path ");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
closedir(dir);
|
||||
|
||||
struct stat st;
|
||||
if (stat(abs_file_name.c_str(), &st) >= 0)
|
||||
{
|
||||
printf(COLOR_RED"Error:\n file"
|
||||
COLOR_BLUE" %s " COLOR_RED "EXISTED in path"
|
||||
COLOR_BLUE" %s " COLOR_RED ".\n" COLOR_OFF,
|
||||
out_file_name.c_str(), this->config.output_path);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
dir = opendir(this->config.template_path);
|
||||
if (dir == NULL)
|
||||
{
|
||||
printf(COLOR_RED"Error:\n template path "
|
||||
COLOR_BLUE" %s " COLOR_RED "does NOT exist.\n" COLOR_OFF,
|
||||
this->config.template_path);
|
||||
return false;
|
||||
}
|
||||
|
||||
struct file_info info;
|
||||
info = { idl_file_name , out_file_name, rpc_idl_transform };
|
||||
this->default_files.push_back(info);
|
||||
|
||||
closedir(dir);
|
||||
return true;
|
||||
}
|
||||
|
||||
void APIController::print_success_info() const
|
||||
{
|
||||
std::string file_name = this->config.project_name;
|
||||
|
||||
if (this->config.idl_type == IDL_TYPE_PROTOBUF)
|
||||
file_name += ".proto";
|
||||
else if (this->config.idl_type == IDL_TYPE_THRIFT)
|
||||
file_name += ".thrift";
|
||||
|
||||
printf(COLOR_GREEN"Success:\n Create api file"
|
||||
COLOR_BLUE" %s " COLOR_GREEN "at path"
|
||||
COLOR_BLUE" %s " COLOR_GREEN "done.\n\n" COLOR_OFF,
|
||||
file_name.c_str(), this->config.output_path);
|
||||
printf(COLOR_PINK"Suggestions:\n"
|
||||
COLOR_WHITE" Modify the api file as you needed.\n"
|
||||
" And make rpc project base on this file with the following command:\n\n"
|
||||
COLOR_GREEN" ./srpc rpc my_rpc_project -f %s -p %s\n\n" COLOR_OFF,
|
||||
file_name.c_str(), this->config.output_path);
|
||||
}
|
||||
|
||||
|
@ -11,4 +11,8 @@ base:
|
||||
cd $(BUILD_DIR) && cmake $(ROOT_DIR)
|
||||
|
||||
clean:
|
||||
ifeq ($(BUILD_DIR), $(wildcard $(BUILD_DIR)))
|
||||
make -C $(BUILD_DIR) clean
|
||||
rm -rf $(BUILD_DIR)
|
||||
endif
|
||||
|
||||
|
@ -35,7 +35,7 @@ if (WIN32)
|
||||
find_package(lz4)
|
||||
if(lz4_FOUND)
|
||||
set(LZ4_LIBRARY ${lz4_LIBRARIES})
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
find_package(Snappy CONFIG REQUIRED)
|
||||
else ()
|
||||
|
Loading…
Reference in New Issue
Block a user