Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cute-angelia/protoc-gen-micro
https://github.com/cute-angelia/protoc-gen-micro
Last synced: 6 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/cute-angelia/protoc-gen-micro
- Owner: cute-angelia
- Created: 2020-10-27T08:46:06.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2020-10-27T09:02:53.000Z (about 4 years ago)
- Last Synced: 2024-06-20T15:46:47.112Z (7 months ago)
- Language: Go
- Size: 65.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# protoc-gen-micro
This is protobuf code generation for micro. We use protoc-gen-micro to reduce boilerplate code.
## Install
```
go get github.com/micro/protoc-gen-micro
```Also required:
- [protoc](https://github.com/google/protobuf)
- [protoc-gen-go](https://github.com/golang/protobuf)## Usage
Define your service as `greeter.proto`
```
syntax = "proto3";service Greeter {
rpc Hello(Request) returns (Response) {}
}message Request {
string name = 1;
}message Response {
string msg = 1;
}
```Generate the code
```
protoc --proto_path=$GOPATH/src:. --micro_out=. --go_out=. greeter.proto
```Your output result should be:
```
./
greeter.proto # original protobuf file
greeter.pb.go # auto-generated by protoc-gen-go
greeter.micro.go # auto-generated by protoc-gen-micro
```The micro generated code includes clients and handlers which reduce boiler plate code
### Server
Register the handler with your micro server
```go
type Greeter struct{}func (g *Greeter) Hello(ctx context.Context, req *proto.Request, rsp *proto.Response) error {
rsp.Msg = "Hello " + req.Name
return nil
}proto.RegisterGreeterHandler(service.Server(), &Greeter{})
```### Client
Create a service client with your micro client
```go
client := proto.NewGreeterService("greeter", service.Client())
```### Errors
If you see an error about `protoc-gen-micro` not being found or executable, it's likely your environment may not be configured correctly. If you've already installed `protoc`, `protoc-gen-go`, and `protoc-gen-micro` ensure you've included `$GOPATH/bin` in your `PATH`.
Alternative specify the Go plugin paths as arguments to the `protoc` command
```
protoc --plugin=protoc-gen-go=$GOPATH/bin/protoc-gen-go --plugin=protoc-gen-micro=$GOPATH/bin/protoc-gen-micro --proto_path=$GOPATH/src:. --micro_out=. --go_out=. greeter.proto
```## LICENSE
protoc-gen-micro is a liberal reuse of protoc-gen-go hence we maintain the original license