https://github.com/iljapavlovs/grpc-pact-go-sample
Example of GRPC in GO and Pact tests for GRPC
https://github.com/iljapavlovs/grpc-pact-go-sample
contract-testing go grpc pact protobuf
Last synced: 30 days ago
JSON representation
Example of GRPC in GO and Pact tests for GRPC
- Host: GitHub
- URL: https://github.com/iljapavlovs/grpc-pact-go-sample
- Owner: iljapavlovs
- Created: 2023-09-15T11:56:22.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-15T12:07:27.000Z (over 1 year ago)
- Last Synced: 2025-02-09T02:25:32.286Z (3 months ago)
- Topics: contract-testing, go, grpc, pact, protobuf
- Language: Go
- Homepage:
- Size: 75.2 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## Installation
* Protocol Buffer Compiler Installation `protoc`
* The protocol buffer compiler, `protoc`, is used to compile .proto files, which contain service and message definitions.```bash
$ brew install protobuf
$ protoc --version # Ensure compiler version is 3+
```* Go Plugins
```bash
$ go install google.golang.org/protobuf/cmd/[email protected]
$ go install google.golang.org/grpc/cmd/[email protected]
```Update your PATH so that the protoc compiler can find the plugins:
```$ export PATH="$PATH:$(go env GOPATH)/bin"```
## Run the example
From the examples/helloworld directory:1. Compile and execute the server code:
```
$ go run greeter_server/main.go
```2. From a different terminal, compile and execute the client code to see the client output:
```
$ go run greeter_client/main.go
Greeting: Hello world
```Congratulations! You’ve just run a client-server application with gRPC.
## Update the gRPC service
1. Open `helloworld/helloworld.proto` and add a new `SayHelloAgain()` method, with the same request and response types:## Regenerate gRPC code
Before you can use the new service method, you need to recompile the updated .proto file.While still in the `helloworld` directory, run the following command:
```bash
protoc --go_out=. --go_opt=paths=source_relative \
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
helloworld/helloworld.proto
```## Update and run the application
You have regenerated server and client code, but you still need to implement and call the new method in the human-written parts of the example application.### Update the server
Open greeter_server/main.go and add the following function to it:```
func (s *server) SayHelloAgain(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "Hello again " + in.GetName()}, nil
}
```
### Update the client
Open greeter_client/main.go to add the following code to the end of the main() function body:```
r, err = c.SayHelloAgain(ctx, &pb.HelloRequest{Name: *name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.GetMessage())
```
##