Skip to content

Commit

Permalink
update grpc golang poc formula
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeFalourd committed Aug 25, 2021
1 parent 39c9602 commit ef03cd2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
4 changes: 1 addition & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
[submodule "grpc/golang/poc/proto"]
path = grpc/golang/poc/proto
url = https://github.com/GuillaumeFalourd/poc-proto.git

[submodule "grpc/golang/poc/protos"]
path = grpc/golang/poc/protos
url = https://github.com/GuillaumeFalourd/poc-proto.git
20 changes: 20 additions & 0 deletions grpc/golang/poc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,32 @@ rit grpc golang poc

![Formula](https://user-images.githubusercontent.com/22433243/128401370-a2ba5cd3-d92f-4c44-9950-c67baac24ff5.png)

* * *

## How does it work?

Reference: [Go generated code](https://developers.google.com/protocol-buffers/docs/reference/go-generated#package)

1 - The `protos` submodules as been added at the formula `root` repository based on [this Github repository](https://github.com/GuillaumeFalourd/poc-proto).

- To add a submodule: _e.g `git submodule add https://github.com/GuillaumeFalourd/poc-proto.git protos`_
- To update a submodule: _e.g `git submodule update --remote --recursive`_
- To init a submodule after a clone: _e.g `git submodule init` then `git submodule update`_

2 - To generate files from the `protos/user/user.proto` file, use the `make gen-go-proto` at the formula root directory (here `grpc/golang/poc`).

_Add a method like this to the Makefile:_

```shell
gen-go-proto:
mkdir -p src/pkg/formula
protoc --go_out=src/pkg/formula \
--go_opt=paths=source_relative \
--go-grpc_out=src/pkg/formula \
--go-grpc_opt=paths=source_relative \
protos/user/user.proto
```

_Note that the proto file currently needs to have a `go_package` informed to work._

3 - Files will be generated inside the `./poc/src/pkg/formula` folder on a new `protos/user` directory.
Expand All @@ -41,6 +59,8 @@ _Note that the proto file currently needs to have a `go_package` informed to wor

5 - Then, implement the formula to call the gRPC service you wish.

* * *

_**Note**: If the `user.proto` is updated from the remote repository. You can update it on the formula using the followigin command_

```bash
Expand Down
9 changes: 4 additions & 5 deletions grpc/golang/poc/src/pkg/formula/formula.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@ type Formula struct {
}

func (f Formula) Run() {

// Set up a connection to the server.
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
ctx, cancel := context.WithTimeout(context.Background(), 5000*time.Millisecond) // 5000 Millisecond is the timeout allowed
conn, err := grpc.DialContext(ctx, address, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
log.Fatalf("Couldn't connect: %v", err)
}
defer conn.Close()
c := pb.NewUserClient(conn)
defer cancel()

// Call server Login method through gRPC.
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.Login(ctx, &pb.LoginRequest{Username: f.Username, Password: f.Password})
if err != nil {
log.Fatalf("Unexpected Error: %v", err)
Expand Down

0 comments on commit ef03cd2

Please sign in to comment.