Skip to content

lllleven/protoc-gen-go-gin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

protoc-gen-go-gin

According to protobuf to generate gin registration route codes, you can use their own log and response to replace the default value, in addition to generating registration route code, but also support the generation of http handler template code, call rpc server template code, http or rpc error code codes.


Installation

Installation of dependency tools

# install protoc in linux
mkdir -p protocDir \
  && curl -L -o protocDir/protoc.zip https://github.com/protocolbuffers/protobuf/releases/download/v3.20.1/protoc-3.20.1-linux-x86_64.zip \
  && unzip protocDir/protoc.zip -d protocDir\
  && mv protocDir/bin/protoc protocDir/include/ $GOROOT/bin/ \
  && rm -rf protocDir

# install protoc-gen-go, protoc-gen-go-grpc
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28.0
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2.0

Install protoc-gen-go-gin

go install github.com/zhufuyi/sponge/cmd/protoc-gen-go-gin@latest


Usage

protobuf documentation conventions

By default the rpc method is named method+resource, using camel naming, which is mapped when generating code, as shown below:

  • "GET", "FIND", "QUERY", "LIST", "SEARCH" --> GET
  • "POST", "CREATE" --> POST
  • "PUT", "UPDATE" --> PUT
  • "DELETE" --> DELETE

Specify the route using the google.api.http option

service Greeter {
  rpc Create(CreateRequest) returns (CreateReply) {
    option (google.api.http) = {
      post: "/api/v1/greeter"
      body: "*"
    };
  }

  rpc GetByID(GetByIDRequest) returns (GetByIDReply) {
    option (google.api.http) = {
      get: "/api/v1/greeter/{id}"
    };
  }
}

Generate code

(1) Generate only *_router.pb.go

protoc --proto_path=. --proto_path=./third_party \
  --go_out=. --go_opt=paths=source_relative \
  --go-grpc_out=. --go-grpc_opt=paths=source_relative \
  --go-gin_out=. --go-gin_opt=paths=source_relative  \
  api/v1/*.proto

(2) Generate codes with plugin handler

protoc --proto_path=. --proto_path=./third_party \
  --go_out=. --go_opt=paths=source_relative \
  --go-gin_out=. --go-gin_opt=paths=source_relative --go-gin_opt=plugin=handler \
  --go-gin_opt=moduleName=yourModuleName --go-gin_opt=serverName=yourServerName \
  api/v1/*.proto

A total of 4 files are generated: the registration route file _*router.pb.go, the injection route file *_handler.go (default save path in internal/routers), the logic code template file *_logic.go (default save path in internal/handler), the error code file *_http.go (default save path in internal/ecode).


(3) Generate codes with plugin service

protoc --proto_path=. --proto_path=./third_party \
  --go_out=. --go_opt=paths=source_relative \
  --go-grpc_out=. --go-grpc_opt=paths=source_relative \
  --go-gin_out=. --go-gin_opt=paths=source_relative --go-gin_opt=plugin=service \
  --go-gin_opt=moduleName=yourModuleName --go-gin_opt=serverName=yourServerName \
  api/v1/*.proto

A total of 4 files are generated: the registration route file *_router.pb.go, the injection route file *_service.go (default save path in internal/routers), and the logic code template file *_logic.go (default save path in internal/service), the error code file *_rpc.go (default save path in internal/ecode).

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published